NPOI,同一行创建批注提示found multiple cell comments for cell AN5。

大刘0417 2014-08-31 03:01:23
代码如下:

 //创建绘图对象,用于创建批注
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();


AllResult:是所有错误信息的集合,是一个自定义结构的集合;
AllResult[i].errorinfo:单行错误的所有错误信息,一行中可能有多个错误;
AllResult[i].errorinfo[j].columnindex[k]:单个错误所涉及的列,一个错误可能产生于多个单元格;

具体结构定义的代码如下:
#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


在第一段代码的 hw.Write(fs);产生如题的错误信息,如何解决,请求帮助。
...全文
1047 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大刘0417 2014-09-01
  • 打赏
  • 举报
回复
感谢您的帮助。 又解决了一个问题,虽然是个小问题,那也感觉很爽,呵呵。
大刘0417 2014-09-01
  • 打赏
  • 举报
回复
引用 2 楼 Z65443344 的回复:
hw是什么,完全没看出跟AllResult有任何关系
哦,少写了一句: HSSFWorkbook hw = new HSSFWorkbook(fs) 你说的对,是在同一个单元格建立了2个批注,问题已经解决,解决方法如下: 此处代码:
 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);
                        }
於黾 2014-09-01
  • 打赏
  • 举报
回复
还有,既然报AN5有问题,看看AN5到底怎么了 是否在同一单元格建立了2个批注?
於黾 2014-09-01
  • 打赏
  • 举报
回复
hw是什么,完全没看出跟AllResult有任何关系
大刘0417 2014-09-01
  • 打赏
  • 举报
回复
等 了一天,没有等到高人,自己顶一下。 也不知道问题说明白没?

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧