111,125
社区成员
发帖
与我相关
我的任务
分享 //创建绘图对象,用于创建批注
HSSFPatriarch patr = (HSSFPatriarch)sht.CreateDrawingPatriarch();
NPOI.SS.UserModel.ICreationHelper facktory = hw.GetCreationHelper();
HSSFComment comment1;
// HSSFAnchor an = new HSSFClientAnchor();
NPOI.SS.UserModel.IClientAnchor anchor;
anchor = facktory.CreateClientAnchor();
anchor.Col1 = 0; anchor.Col2 = 5;
anchor.Row1 = 0; anchor.Row2 = 5;
for (int i = 0; i < AllResult.Count; i++) //按错误行数循环
{
HSSFRow row = (HSSFRow)sht.GetRow(i + 1); //读取表中的行,因为0为标题列,从1开始
for (int j = 0; j < AllResult[i].errorinfo.Count; j++) //一行中所有错误数循环
{
for (int k = 0; k < AllResult[i].errorinfo[j].columnindex.Count; k++) //按单个错误所涉及的单元格数循环,设置错误包含单元格的格式
{
//建立批注
comment1 = patr.CreateCellComment(anchor) as HSSFComment;
comment1.String = new HSSFRichTextString(AllResult[i].errorinfo[j].errorInfo);
comment1.Author = "XXXX设计";
//错误所在的列号
int coli = (int)AllResult[i].errorinfo[j].columnindex[k];
HSSFCell cell = (HSSFCell)row.GetCell(coli);//包含错误的单元格
cell.CellComment = comment1;
cell.CellStyle = hfctyle;
}
}
}
fs = new FileStream(filename + "\\检测结果0.xls", FileMode.Open, FileAccess.ReadWrite);
hw.Write(fs); //在此产生错误信息
fs.Close();#region 保存检测结果的结构的定义,原则上不需要修改
/// <summary>
/// 保存单项错误信息
/// </summary>
public struct ErrorInfo
{
/// <summary>
/// 错误所在列号
/// </summary>
public ArrayList columnindex;
/// <summary>
/// 错误描述信息
/// </summary>
public string errorInfo;
}
/// <summary>
/// 保存一条数据包含的错误信息
/// </summary>
private struct GetResult
{
/// <summary>
/// 错误行所在行号
/// </summary>
public int rowindex;
/// <summary>
/// 该行所包含的所有错误
/// </summary>
public List<ErrorInfo> errorinfo;
}
/// <summary>
/// 保存检测结果的数据集
/// </summary>
private static List<GetResult> AllResult;
#endregion for (int k = 0; k < AllResult[i].errorinfo[j].columnindex.Count; k++) //按单个错误所涉及的单元格数循环,设置错误包含单元格的格式
{
//建立批注
comment1 = patr.CreateCellComment(anchor) as HSSFComment;
comment1.String = new HSSFRichTextString(AllResult[i].errorinfo[j].errorInfo);
comment1.Author = "XXXX设计";
//错误所在的列号
int coli = (int)AllResult[i].errorinfo[j].columnindex[k];
HSSFCell cell = (HSSFCell)row.GetCell(coli);//包含错误的单元格
cell.CellComment = comment1;
cell.CellStyle = hfctyle;
}
改为:
for (int k = 0; k < AllResult[i].errorinfo[j].columnindex.Count; k++) //按单个错误所涉及的单元格数循环,设置错误包含单元格的格式
{
//错误所在的列号
int coli = (int)AllResult[i].errorinfo[j].columnindex[k];
HSSFCell cell = (HSSFCell)row.GetCell(coli);//包含错误的单元格
if (cell.CellComment == null)
{
//建立批注
comment1 = patr.CreateCellComment(anchor) as HSSFComment;
comment1.String = new HSSFRichTextString(AllResult[i].errorinfo[j].errorInfo);
comment1.Author = "数据比对助手";
cell.CellComment = comment1;
}
else
{
//原批注文本
string commentstr = cell.CellComment.String.String;
cell.CellComment.String = new HSSFRichTextString(commentstr + "\r\n" + AllResult[i].errorinfo[j].errorInfo);
}