2017-10-13 16 views
1

C#에서 NPOI Excel을 사용하여 셀 주석을 만들고 싶습니다. 나는 그 어떤 명확한 문서도 찾지 못했다. 나는 아래에 나와 같이 혼자서 뭔가를 썼다.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 파일에서 해당 셀에 대한 주석을 볼 수 없습니다. 특정 셀에 주석을 추가하는 방법을 아는 사람이 있습니까?

답변

2

셀 주석을 만들려면 도면 축복을 사용해야합니다. 그런 다음 작성자와 텍스트를 정의 할 수 있습니다. 일부 글꼴 사용자 정의를 적용 할 수도 있습니다.

내가 여러 단계 댓글을 달았습니다,이 코드를 시도하십시오 :

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(); 
} 

참고 :

+0

덕분에 krlzlx. 나는 그것을 축 복사로 취급했다. – aanilapaydin

+0

당신을 진심으로 환영합니다. 언제나 기꺼이 도와 드리겠습니다. – krlzlx