NPOI创建批注
不知道大家有没有到,用NPOI创建单元格批注时,创建多行批注,只有最有一格创建成功,其它的批注莫名其妙的不见了,如代码:
HSSFPatriarch patr1 = sheet.CreateDrawingPatriarch() as HSSFPatriarch;
HSSFComment comment1 = patr1.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 3, 3, 7)) as HSSFComment;
comment1.String = new HSSFRichTextString("作者:\n0");
HSSFCell hssfcell1 = sheet.CreateRow(5).CreateCell(1) as HSSFCell;
hssfcell1.CellComment=comment1;
HSSFPatriarch patr2 = sheet.CreateDrawingPatriarch() as HSSFPatriarch;
HSSFComment comment2 = patr2.CreateCellComment(new HSSFClientAnchor(5, 0, 5, 0, 2, 3, 4,8)) as HSSFComment;
comment2.String = new HSSFRichTextString("\n1");
comment1.Author = "Wicrosoft";
HSSFCell Hssfcell2 = sheet.CreateRow(5).CreateCell(2) as HSSFCell;
Hssfcell2.CellComment = comment2;
[解决办法]
好吧,没人回答。我自己回答
using (Workbook wkBook = new HSSFWorkbook())
{
using (Sheet sheet = wkBook.CreateSheet("Sheet"))
{
#region MyRegion
Row row = sheet.CreateRow(5);
//Cell cell1 = row.CreateCell(0);
//cell1.SetCellValue(0);
//Cell cell2 = row.CreateCell(1);
//cell2.SetCellValue(1);
//Cell cell3 = row.CreateCell(2);
//cell3.SetCellValue(2);
//Cell cell4 = row.CreateCell(3);
//cell4.SetCellValue(3);
//Cell cell5 = row.CreateCell(4);
//cell5.SetCellValue(4);
#endregion
//创建批注(关键点在这儿,一个sheet中只能有一个HSSFPatriarch实例、一个Comment实例和一个CreationHelper)
HSSFPatriarch patr = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
CreationHelper facktory = wkBook.GetCreationHelper();
Comment comment = null;
ClientAnchor anchor = null;
for (int i = 0; i < 5; i++)
{
Cell cell = row.CreateCell(i);
cell.SetCellValue(i);
anchor = facktory.CreateClientAnchor();
anchor.Col1 = cell.ColumnIndex;
anchor.Col2 = cell.ColumnIndex + 1;
anchor.Row1 = row.RowNum;
anchor.Row2 = row.RowNum + 3;
comment = patr.CreateCellComment(anchor);
comment.String = new HSSFRichTextString(i.ToString());
comment.Author = ("dongbo");
cell.CellComment = (comment);
}
SaveExcel(filePath, wkBook);
}
}
OK