你的位置:首页 > ASP.net教程

[ASP.net教程]C#winform解析marc显示在datagridview中以及marc卡片显示

结果显示:marc显示:

卡片显示:

程序一个类:

public class MARC
{
#region 界面上要显示的元素

public string ztm = "";//正题名
public string ftm = "";//副题名
public string fcm = "";//分册(辑)名
public string fch = "";//分册(辑)号
public string dyzrsm = "";//第一责任说明

public string zyzrz_1_lb = "";//主要责任者1,类别,个人/团体/会议
public string zyzrz_1_mc = "";//主要责任者1,名称
public string zyzrz_1_zzfs = "";//主要责任者1,著作方式

public string zyzrz_2_lb = "";//主要责任者2,类别,个人/团体/会议
public string zyzrz_2_mc = "";//主要责任者2,名称
public string zyzrz_2_zzfs = "";//主要责任者2,著作方式

public string cyzrz_1_lb = "";//次要责任者1,类别,个人/团体/会议
public string cyzrz_1_mc = "";//次要责任者1,名称
public string cyzrz_1_zzfs = "";//次要责任者1,著作方式

public string cyzrz_2_lb = "";//次要责任者2,类别,个人/团体/会议
public string cyzrz_2_mc = "";//次要责任者2,名称
public string cyzrz_2_zzfs = "";//次要责任者2,著作方式

public string bc = "";//版次
public string isbn = "";//ISBN
public string zz = "";//装帧
public string dj = "";//单价
public string yzdm = "";//语种代码
public string csm = "";//丛书名/丛编题名
public string cb_jcbs = "";//丛编_卷册标识
public string fcbmc = "";//分丛编名称
public string cbd = "";//出版地
public string cbz = "";//出版者
public string cbrq = "";//出版日期
public string ys = "";//页数
public string tu = "";//图
public string cc = "";//尺寸
public string fj = "";//附件
public string fzx = "";//附注项
public string flh = "";//分类号,可能有多个,文本框中只显示第一个
public string zch = "";//种次(著者)号
public string jqh = "";//卷期号
public string ndh = "";//年代号
public string pjqf = "";//排架区分
public string zy = "";//摘要
public string ztc = "";//主题词,可能有多个
public string bgdm = "";//本馆代码
public string rdrq = "";//入档日期
public string sbmy = "";//首编目员
//这个要传回来
public string dlh = "";//登录号,可能有多个

#endregion 显示MARC


private string GetIndexChar(int index)
{
switch (index)
{
case 0:
return "①";
case 1:
return "②";
case 2:
return "③";
case 3:
return "④";
case 4:
return "⑤";
case 5:
return "⑥";
case 6:
return "⑦";
case 7:
return "⑧";
case 8:
return "⑨";
case 9:
return "⑩";
}
return "";
}
/// <summary>
/// 卡片显示
/// </summary>
/// <param name="marcStr"></param>
/// <returns></returns>
public Bitmap GetMarcPic()
{

//this.pic_KP.Image = null;
Bitmap bmpTemp = new Bitmap(474, 283 * 2);
Graphics g = Graphics.FromImage(bmpTemp);
//画背景
g.FillRectangle(Brushes.White, new Rectangle(0, 0, bmpTemp.Width, bmpTemp.Height));
//分类号
g.DrawString(flh.Split(' ')[0], new Font("宋体", 10, FontStyle.Bold), Brushes.Black, new Point(20, 40));//只画第一个分卷号
//种次号
g.DrawString(zch, new Font("宋体", 10, FontStyle.Bold), Brushes.Black, new Point(20, 56));
//卷期号
g.DrawString(jqh, new Font("宋体", 9), Brushes.Black, new Point(20, 72));
//段落1
string strToDraw = " " + ztm;//正题名
strToDraw += ftm == "" ? "" : ("." + ftm);//副题名
strToDraw += fcm == "" ? "" : ("." + fcm);//分册名
strToDraw += fch == "" ? "" : ("." + fch);//分册号
strToDraw += dyzrsm == "" ? "" : ("/" + dyzrsm);//第一责任说明
strToDraw += bc + cbd == "" ? "" : (".—" + bc);//出版地
strToDraw += bc + cbd == "" ? "" : ("." + cbd);//出版地
strToDraw += cbz == "" ? "" : (":" + cbz);//出版者
strToDraw += cbrq == "" ? "" : ("." + cbrq);//出版日期
//段落2
strToDraw += "\r\n " + ys;//页数
strToDraw += tu == "" ? "" : (":" + tu);//图
strToDraw += cc == "" ? "" : (";" + cc);//尺寸
strToDraw += fj == "" ? "" : (";" + fj);//附件
if (csm != "")
{
strToDraw += ".—(" + csm;//丛编题名
strToDraw += cb_jcbs == "" ? "" : ("." + cb_jcbs);//丛编_卷册标识
strToDraw += fcbmc == "" ? "" : ("." + fcbmc);//分丛编名称
strToDraw += ")";
}
//段落3
strToDraw += "\r\n\r\n\r\n\r\n";
if (zy != "")
{
strToDraw += " " + zy + "\r\n\r\n";//摘要
}
if (isbn != "")
{
strToDraw += " ISBN: " + isbn;//ISBN
if (dj != "")
{
strToDraw += ":" + dj;//单价
}
strToDraw += "\r\n\r\n";
}
//段落4
strToDraw += " ";
if (ztm != "")//主题名
{
strToDraw += "Ⅰ." + ztm.Replace("(", "").Replace("(", "").Replace("《", "").Substring(0, 1) + "... ";
}
//作者
List<string> allWriter = new List<string>();
if (zyzrz_1_mc != "")
{
allWriter.Add(zyzrz_1_mc);
}
if (zyzrz_2_mc != "")
{
allWriter.Add(zyzrz_2_mc);
}
if (cyzrz_1_mc != "")
{
allWriter.Add(cyzrz_1_mc);
}
if (cyzrz_2_mc != "")
{
allWriter.Add(cyzrz_2_mc);
}
if (allWriter.Count == 1)
{
strToDraw += "Ⅱ.";
strToDraw += allWriter[0].Replace("(", "").Replace("(", "").Replace("《", "").Substring(0, 1) + "... ";//只有1个作者
}
else if (allWriter.Count > 1)
{
strToDraw += "Ⅱ.";
//多个作者,给出编号
for (int i = 0; i < allWriter.Count; i++)
{
strToDraw += GetIndexChar(i) + allWriter[i].Replace("(", "").Replace("(", "").Replace("《", "").Substring(0, 1) + "... ";
}
}
//主题词
List<string> ztcList = ztc.Split(new char[] { (char)29 }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (ztcList.Count == 1)
{
strToDraw += "Ⅲ.";
strToDraw += ztcList[0].ToString() + " ";//只有1个主题词
}
else if (ztcList.Count > 1)
{
strToDraw += "Ⅲ.";
//多个主题词,给出编号
for (int i = 0; i < ztcList.Count; i++)
{
strToDraw += GetIndexChar(i) + ztcList[i] + " ";
}
}
//分类号
List<string> flhList = flh.Split(new char[] { (char)29 }, StringSplitOptions.RemoveEmptyEntries).ToList();
if (flhList.Count == 1)
{
strToDraw += "Ⅳ.";
strToDraw += flhList[0].ToString();//只有分类号
}
else if (flhList.Count > 1)
{
strToDraw += "Ⅳ.";
//多个分类号,给出编号
for (int i = 0; i < flhList.Count; i++)
{
strToDraw += GetIndexChar(i) + flhList[i] + " ";
}
}
//登录号
int dlcount = 0;
Font font = new Font("新宋体", 9, FontStyle.Bold);
foreach (string dl in dlh.Split(new char[] { (char)29 }, StringSplitOptions.RemoveEmptyEntries))
{
if (dl.Trim() == "")
{
continue;
}
g.DrawString(dl, font, Brushes.Black, new Point(20, 124 + dlcount * 16));
dlcount++;
}
//底部圆圈
g.DrawEllipse(Pens.Black, new Rectangle(bmpTemp.Width / 2 - 4, bmpTemp.Height / 2 - 24, 16, 16));
//画卡片详细
StringFormat sf = new StringFormat();
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DisplayFormatControl;
sf.Trimming = StringTrimming.EllipsisCharacter;
//sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
//sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DisplayFormatControl;
//SizeF sizef = g.MeasureString(strToDraw, this.Font, 300, sf);
int charactersFitted = 0;//能容纳的字符数
int linesFilled = 0;//能容纳的行数,最多16行
SizeF sizef = g.MeasureString(strToDraw, font, new SizeF(300, 220), sf, out charactersFitted, out linesFilled);

if (charactersFitted < strToDraw.Length)
{
//需要分页
g.DrawString("1/2", font, Brushes.Black, new Point(360, 13));
g.DrawString("2/2", font, Brushes.Black, new Point(360, 13 + bmpTemp.Height / 2));
g.DrawString("(见次卡)", font, Brushes.Black, new Point(325, 260));

g.DrawEllipse(Pens.Black, new Rectangle(bmpTemp.Width / 2 - 4, bmpTemp.Height - 24, 16, 16));
g.DrawLine(Pens.Black, new Point(0, bmpTemp.Height / 2), new Point(bmpTemp.Width, bmpTemp.Height / 2));

g.DrawString(strToDraw.Substring(0, charactersFitted + 1), font, Brushes.Black, new RectangleF(new Point(120, 40), g.MeasureString(strToDraw, font, 300, sf)), sf);
g.DrawString(strToDraw.Substring(charactersFitted + 1), font, Brushes.Black, new RectangleF(new Point(120, 40 + bmpTemp.Height / 2), g.MeasureString(strToDraw, font, 300, sf)), sf);


//this.pic_KP.Image = bmpTemp;
return bmpTemp;
}
else
{
g.DrawString(strToDraw, font, Brushes.Black, new RectangleF(new Point(120, 40), sizef), sf);
Bitmap picToShow = new Bitmap(bmpTemp.Width, bmpTemp.Height / 2);
Graphics gp = Graphics.FromImage(picToShow);
gp.DrawImageUnscaled(bmpTemp, 0, 0, bmpTemp.Width, bmpTemp.Height / 2);
return picToShow;
//this.pic_KP.Image = picToShow;
}
}

 

/// <summary>
/// MARC显示
/// </summary>
/// <param name="marcStr"></param>
/// <param name="todgv"></param>
public void ShowMarcTable(string marcStr, DataGridView todgv)
{
if (string.IsNullOrWhiteSpace(marcStr))
{
return;
}
DBUtility.SQuery sq = DBUtility.SQuery.Create("SELECT * FROM MARC数据字典 WHERE MARC类型=1 ORDER BY 标识符");//MARC类型=1为CNMARC,0为USMARC
DataTable dt = DBUtility.DbHelper.ExecuteDataTable(sq.ToString());
DataTable marcTable = new DataTable();
marcTable.Columns.Add("字段名称");
marcTable.Columns.Add("标识");
marcTable.Columns.Add("指");
marcTable.Columns.Add("字段内容");
List<string> arr = marcStr.Split((char)30).ToList();
if (arr.Count == 0)
{
throw new Exception("MARC无效");
}
marcTable.Rows.Add("头标区", "", "", arr[0].Trim());
arr.RemoveAt(0);
//这里要进行筛选,把空的元素直接删除,因为在进行直接操作删除时候会报错
if (arr[arr.Count - 1].ToString() == "")
{
arr.RemoveAt(arr.Count-1);
}
foreach (string str in arr)
{
if (str == "")
{
continue;
}
string key = str.Substring(0, 3);
DataRow[] dr = dt.Select(string.Format("标识符='{0}'", key));
if (dr.Length > 0)
{
int len = int.Parse(dr[0]["固定长度"].ToString());
if (len > 0)
{
//判断是否包含特殊字符
if (str.Contains((char)31))
{
marcTable.Rows.Add(dr[0]["名称缩写"].ToString(), key,
"", str.Substring(str.IndexOf((char)31)).Trim().Replace((char)31, '■'));
}
else
{
marcTable.Rows.Add(dr[0]["名称缩写"].ToString(), key, "", str.Substring(3).Trim());//原来的没把特殊字符转化为■
}
if (key == "005")//处理日期特殊处理
{
rdrq = str.Substring(3, 8);//国定长度,无子项
}
}
else
{
if (str.IndexOf((char)31) == -1)
{
marcTable.Rows.Add(dr[0]["名称缩写"].ToString(), key, "", str.Substring(3).Trim());
continue;
}
marcTable.Rows.Add(dr[0]["名称缩写"].ToString(), key,
str.Split((char)31)[0].Substring(3).Trim(), str.Substring(str.IndexOf((char)31)).Trim().Replace((char)31, '■'));

string[] subArray = str.Substring(str.IndexOf((char)31)).Trim().Split(new char[] { (char)31 }, StringSplitOptions.RemoveEmptyEntries);
switch (key)
{
//case "005"://处理时间,固定长度,特殊处理
// rdrq = subArray[0].Substring(0, 8);//国定长度,无子项
// break;
case "010"://ISBN
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://ISBN
isbn = subStr.Substring(1);
break;
case "b"://装帧
zz = subStr.Substring(1);
break;
case "d"://单价
dj = subStr.Substring(1);
break;
}
}
break;
case "101"://作品语种
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://语种代码
yzdm = subStr.Substring(1);
break;
}
}
break;
case "200"://题名责任
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://正题名
ztm = subStr.Substring(1);
break;
case "e"://副题名
ftm = subStr.Substring(1);
break;
case "i"://分册(辑)名
fcm = subStr.Substring(1);
break;
case "h"://分册(辑)号
fch = subStr.Substring(1);
break;
case "f"://第一责任说明
dyzrsm = subStr.Substring(1);
break;
}
}
break;
case "205"://版次
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://版本说明
bc = subStr.Substring(1);
break;
}
}
break;
case "210"://出版发行
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://出版地
cbd = subStr.Substring(1);
break;
case "c"://出版者
cbz = subStr.Substring(1);
break;
case "d"://出版日期
cbrq = subStr.Substring(1);
break;
}
}
break;
case "215"://载体形态
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://页数
ys = subStr.Substring(1);
break;
case "c"://图
tu = subStr.Substring(1);
break;
case "d"://尺寸
cc = subStr.Substring(1);
break;
case "e"://附件
fj = subStr.Substring(1);
break;
}
}
break;
case "225"://丛编项
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://丛书名
csm = subStr.Substring(1);
break;
case "v"://丛编_卷册标识
cb_jcbs = subStr.Substring(1);
break;
case "i"://分丛编名称
fcbmc = subStr.Substring(1);
break;
}
}
break;
case "300"://一般附注
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://附注
fzx = subStr.Substring(1);
break;
}
}
break;
case "330"://提要文摘
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://摘要
zy = subStr.Substring(1);
break;
}
}
break;
case "606"://普通主题
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://主题词,可能有多个,用空格隔开
ztc += subStr.Substring(1) + (char)29;
break;
}
}
break;
case "690"://中图分类
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://分类号,可能有多个,用空格隔开
flh += subStr.Substring(1) + (char)29;
break;
}
}
break;
case "701"://人名等同
case "711"://团体等同
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a":
if (zyzrz_1_mc == "")
{
if (key == "701")
{
zyzrz_1_lb = "个人";
}
else
{
string zhi = str.Split((char)31)[0].Substring(3).Trim();
zyzrz_1_lb = zhi.Length > 0 && zhi[0].ToString() == "1" ? "会议" : "团体";//"指"的第一位为1表示会议,否则为团体
}
zyzrz_1_mc = subStr.Substring(1);
}
else
{
if (key == "701")
{
zyzrz_2_lb = "个人";
}
else
{
string zhi = str.Split((char)31)[0].Substring(3).Trim();
zyzrz_2_lb = zhi.Length > 0 && zhi[0].ToString() == "1" ? "会议" : "团体";//"指"的第一位为1表示会议,否则为团体
}
zyzrz_2_mc = subStr.Substring(1);
}
break;
case "4":
if (zyzrz_1_mc != "" && zyzrz_1_zzfs == "")
{
zyzrz_1_zzfs = subStr.Substring(1);
}
else
{
zyzrz_2_zzfs = subStr.Substring(1);
}
break;
}
}
break;
case "702"://人名次要
case "712"://团体次要
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a":
if (cyzrz_1_mc == "")
{
if (key == "702")
{
cyzrz_1_lb = "个人";
}
else
{
string zhi = str.Split((char)31)[0].Substring(3).Trim();
cyzrz_1_lb = zhi.Length > 0 && zhi[0].ToString() == "1" ? "会议" : "团体";//"指"的第一位为1表示会议,否则为团体
}
cyzrz_1_mc = subStr.Substring(1);
}
else
{
if (key == "702")
{
cyzrz_2_lb = "个人";
}
else
{
string zhi = str.Split((char)31)[0].Substring(3).Trim();
cyzrz_2_lb = zhi.Length > 0 && zhi[0].ToString() == "1" ? "会议" : "团体";//"指"的第一位为1表示会议,否则为团体
}
cyzrz_2_mc = subStr.Substring(1);
}
break;
case "4":
if (cyzrz_1_mc != "" && cyzrz_1_zzfs == "")
{
cyzrz_1_zzfs = subStr.Substring(1);
}
else
{
cyzrz_2_zzfs = subStr.Substring(1);
}
break;
}
}
break;
case "830"://编目员注释
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://此项不确定//??
sbmy = subStr.Substring(1);
break;
}
}
break;
case "905"://馆藏信息
foreach (string subStr in subArray)
{
switch (subStr.Substring(0, 1))
{
case "a"://馆藏代码
bgdm = subStr.Substring(1);
break;
case "b"://登录号
dlh += subStr.Substring(1) + (char)29;
break;
case "c"://排架区分
pjqf = subStr.Substring(1);
break;
//case "d"://分类号
// flh += subStr.Substring(1) + (char)29; //等同于 690-a
// break;
case "e"://分种次号
zch = subStr.Substring(1);
break;
case "v"://卷期号
jqh = subStr.Substring(1);
break;
case "y"://年代号
ndh = subStr.Substring(1);
break;
}
}
break;
}
}
}
else
{
//099未识别
//throw new Exception(string.Format("未识别的MARC标记:{0}", str.Substring(0, 3)));
}
}
todgv.DataSource = marcTable;
todgv.Columns["字段名称"].Width = 80;
todgv.Columns["标识"].Width = 40;
todgv.Columns["指"].Width = 40;
todgv.Columns["字段内容"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
todgv.Columns["字段内容"].Width = Math.Max(50, todgv.Width - 80 - 40 - 40 - 20);
todgv.Columns["字段名称"].DefaultCellStyle.BackColor = Color.LightGray;
todgv.Columns["字段名称"].DefaultCellStyle.ForeColor = Color.SteelBlue;
todgv.Columns["字段名称"].DefaultCellStyle.Font = new Font("宋体", 9);

todgv.Columns["标识"].DefaultCellStyle.ForeColor = Color.Red;
todgv.Columns["指"].DefaultCellStyle.ForeColor = Color.Red;

todgv.Columns["字段内容"].DefaultCellStyle.Font = new Font("新宋体", 13);
//FontStyle fs = this.dgv_MARC.Font.Style;
//fs |= FontStyle.Bold;
//Font ft = new Font("宋体", 12, fs);

//this.dgv_MARC.Columns[3].DefaultCellStyle.Font = ft;
todgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
}

internal void ShowMarcTable(object marc_fz, DataGridView dgv_MARC)
{
throw new NotImplementedException();
}
}