如何在 C# 中使用 NPOI Excel 添加单元格注释?
How to add a cell comment using NPOI Excel in C#?
我想在 C# 中使用 NPOI Excel 创建单元格评论。我还没有找到任何明确的文件。下面是我自己写的。
NPOI.HSSF.Record.NoteRecord nr = new NPOI.HSSF.Record.NoteRecord();
nr.Author = "Some Author";
NPOI.HSSF.Record.TextObjectRecord tor = new NPOI.HSSF.Record.TextObjectRecord();
tor.Str = new HSSFRichTextString("something");
HSSFComment cm = new HSSFComment(nr, tor);
cm.Visible = true;
sheet.GetRow(i).Cells[k + 8].CellComment = cm;
该代码无法正常工作。我在生成的 excel 文件中看不到关于该单元格的任何评论。有人知道如何在特定单元格中添加评论吗?
您需要使用绘图族长来创建单元格评论。然后你可以定义你的作者和文本。您还可以应用一些字体自定义。
请试试这段代码,我评论了不同的步骤:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("Sheet1");
HSSFRow row = (HSSFRow)sheet.CreateRow(0);
HSSFCell cell = (HSSFCell)row.CreateCell(0);
cell.SetCellValue("Cell1");
// Create the drawing patriarch (top level container for all shapes including cell comments)
IDrawing patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
// Client anchor defines size and position of the comment in the worksheet
IComment comment = patriarch.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 2, 1, 4, 4));
// Set comment author
comment.Author = "Author";
// Set text in the comment
comment.String = new HSSFRichTextString($"{comment.Author}:{Environment.NewLine}A comment");
// If you want the author displayed in bold on top like in Excel
// The author will be displayed in the status bar when on mouse over the commented cell
IFont font = workbook.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold;
comment.String.ApplyFont(0, comment.Author.Length, font);
// Set comment visible
comment.Visible = true;
// Assign comment to a cell
cell.CellComment = comment;
using (MemoryStream exportData = new MemoryStream())
{
workbook.Write(exportData);
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = Encoding.UTF8.EncodingName;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", $"attachment; filename=test.xls");
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
参考文献:
出于某种原因,在我的例子中是这一行
comment.String.ApplyFont(0, comment.Author.Length, font);
没有按预期工作 - 如果我只想加粗 comment.Author,它会使整个评论字符串加粗。
这是我的解决方案:
IWorkbook workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("sheet name");
var patriarch = sheet.CreateDrawingPatriarch();
var commentAuthor = "ABC:";
comment = patriarch.CreateCellComment(new XSSFClientAnchor(0, 0, 0, 0, 2, 4, 13, 26));
IFont boldFont = (XSSFFont)workbook.CreateFont();
boldFont.IsBold = true;
IFont regularFont = (XSSFFont)workbook.CreateFont();
var str = new XSSFRichTextString();
str.Append(commentAuthor, boldFont);
str.Append("some text", regularFont);
comment.String = str;
row.GetCell(k).CellComment = comment;
我想在 C# 中使用 NPOI Excel 创建单元格评论。我还没有找到任何明确的文件。下面是我自己写的。
NPOI.HSSF.Record.NoteRecord nr = new NPOI.HSSF.Record.NoteRecord();
nr.Author = "Some Author";
NPOI.HSSF.Record.TextObjectRecord tor = new NPOI.HSSF.Record.TextObjectRecord();
tor.Str = new HSSFRichTextString("something");
HSSFComment cm = new HSSFComment(nr, tor);
cm.Visible = true;
sheet.GetRow(i).Cells[k + 8].CellComment = cm;
该代码无法正常工作。我在生成的 excel 文件中看不到关于该单元格的任何评论。有人知道如何在特定单元格中添加评论吗?
您需要使用绘图族长来创建单元格评论。然后你可以定义你的作者和文本。您还可以应用一些字体自定义。
请试试这段代码,我评论了不同的步骤:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("Sheet1");
HSSFRow row = (HSSFRow)sheet.CreateRow(0);
HSSFCell cell = (HSSFCell)row.CreateCell(0);
cell.SetCellValue("Cell1");
// Create the drawing patriarch (top level container for all shapes including cell comments)
IDrawing patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
// Client anchor defines size and position of the comment in the worksheet
IComment comment = patriarch.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 2, 1, 4, 4));
// Set comment author
comment.Author = "Author";
// Set text in the comment
comment.String = new HSSFRichTextString($"{comment.Author}:{Environment.NewLine}A comment");
// If you want the author displayed in bold on top like in Excel
// The author will be displayed in the status bar when on mouse over the commented cell
IFont font = workbook.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold;
comment.String.ApplyFont(0, comment.Author.Length, font);
// Set comment visible
comment.Visible = true;
// Assign comment to a cell
cell.CellComment = comment;
using (MemoryStream exportData = new MemoryStream())
{
workbook.Write(exportData);
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = Encoding.UTF8.EncodingName;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", $"attachment; filename=test.xls");
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
参考文献:
出于某种原因,在我的例子中是这一行
comment.String.ApplyFont(0, comment.Author.Length, font);
没有按预期工作 - 如果我只想加粗 comment.Author,它会使整个评论字符串加粗。
这是我的解决方案:
IWorkbook workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("sheet name");
var patriarch = sheet.CreateDrawingPatriarch();
var commentAuthor = "ABC:";
comment = patriarch.CreateCellComment(new XSSFClientAnchor(0, 0, 0, 0, 2, 4, 13, 26));
IFont boldFont = (XSSFFont)workbook.CreateFont();
boldFont.IsBold = true;
IFont regularFont = (XSSFFont)workbook.CreateFont();
var str = new XSSFRichTextString();
str.Append(commentAuthor, boldFont);
str.Append("some text", regularFont);
comment.String = str;
row.GetCell(k).CellComment = comment;