public void TestBug56835CellComment() { XSSFWorkbook wb = new XSSFWorkbook(); try { XSSFSheet sheet = wb.CreateSheet() as XSSFSheet; XSSFDrawing Drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing; // first comment works IClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4); XSSFComment comment = Drawing.CreateCellComment(anchor) as XSSFComment; Assert.IsNotNull(comment); try { Drawing.CreateCellComment(anchor); Assert.Fail("Should fail if we try to add the same comment for the same cell"); } catch (ArgumentException) { // expected } } finally { wb.Close(); } }
/// <summary> /// 样表 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> private ActionResult ImportDownload(BF_IMPORT.Entity entity) { try { List <string> commentList = new List <string>(); DataTable dt = BF_IMPORT.GetSampleTable(entity, out commentList); string filename = HttpUtility.UrlEncode(string.Format("{0}_{1}.xlsx", entity.NAME, DateTime.Now.ToString("yyyyMMddHHmmss")), Encoding.UTF8); string path = System.Web.HttpContext.Current.Server.MapPath("~/tmp/"); Library.Export.ExcelFile export = new Library.Export.ExcelFile(path); string fullName = export.ToExcel(dt); if (string.IsNullOrWhiteSpace(fullName) == true || System.IO.File.Exists(fullName) == false) { return(ShowAlert("未生成Excel文件")); } IWorkbook workBook; using (FileStream fs = System.IO.File.OpenRead(fullName)) { workBook = new XSSFWorkbook(fs); ISheet sheet = workBook.GetSheetAt(0); IRow row = sheet.GetRow(0); XSSFDrawing patriarch = (XSSFDrawing)sheet.CreateDrawingPatriarch(); for (int c = 0; c < row.Cells.Count; c++) { if (string.IsNullOrWhiteSpace(commentList[c]) == false) { IComment comment = patriarch.CreateCellComment(new XSSFClientAnchor(0, 50, 0, 50, c, 0, c + 3, 5)); comment.Author = "编辑提示"; comment.String = new XSSFRichTextString("【编辑提示】\r\n" + commentList[c]); row.Cells[c].CellComment = comment; } } } //重写文件 using (FileStream fs = System.IO.File.Create(fullName)) { workBook.Write(fs); fs.Close(); } System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.Clear();//清除缓冲区所有内容 System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); System.Web.HttpContext.Current.Response.WriteFile(fullName); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); //删除文件 export.Delete(fullName); } catch (Exception ex) { ShowAlert("生成样表出错:" + ex.Message); } return(View()); }
private NPOI.SS.UserModel.IComment GetComment(ISheet sheet, string result) { XSSFDrawing drawing = sheet.CreateDrawingPatriarch() as XSSFDrawing; NPOI.SS.UserModel.IComment comment1 = drawing.CreateCellComment(new XSSFClientAnchor(0, 0, 0, 0, 10, 3, 12, 6)); // set text in the comment comment1.String = (new XSSFRichTextString(result)); //set comment author. //you can see it in the status bar when moving mouse over the commented cell comment1.Author = ("IVM"); return(comment1); }
/// <summary> /// 创建标题批注 /// </summary> /// <param name="sheet">表单</param> /// <param name="titleRow">行</param> /// <param name="header"></param> /// <param name="commnet"></param> private void createTitleComment(ISheet sheet, IRow titleRow) { ICell newCellHeader = titleRow.CreateCell(0); newCellHeader.SetCellValue(header); newCellHeader.CellStyle = TitleStyle; if (string.IsNullOrEmpty(this.header) || string.IsNullOrEmpty(this.commnet)) { return; } XSSFDrawing xdraw = (XSSFDrawing)sheet.CreateDrawingPatriarch(); XSSFComment commnet = (XSSFComment)xdraw.CreateCellComment(new XSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 4)); commnet.SetString(this.commnet); newCellHeader.CellComment = commnet; }
public void TestCommentShowsBox() { XSSFWorkbook wb = new XSSFWorkbook(); wb.CreateSheet(); XSSFSheet sheet = (XSSFSheet)wb.GetSheetAt(0); XSSFCell cell = (XSSFCell)sheet.CreateRow(0).CreateCell(0); XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch(); XSSFCreationHelper factory = (XSSFCreationHelper)wb.GetCreationHelper(); XSSFClientAnchor anchor = (XSSFClientAnchor)factory.CreateClientAnchor(); anchor.Col1 = cell.ColumnIndex; anchor.Col2 = cell.ColumnIndex + 3; anchor.Row1 = cell.RowIndex; anchor.Row2 = cell.RowIndex + 5; XSSFComment comment = (XSSFComment)drawing.CreateCellComment(anchor); XSSFRichTextString str = (XSSFRichTextString)factory.CreateRichTextString("this is a comment"); comment.String = str; cell.CellComment = comment; XSSFVMLDrawing vml = sheet.GetVMLDrawing(false); CT_Shapetype shapetype = null; ArrayList items = vml.GetItems(); foreach (object o in items) { if (o is CT_Shapetype) { shapetype = (CT_Shapetype)o; } } Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.stroked); Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.filled); using (MemoryStream ws = new MemoryStream()) { wb.Write(ws); using (MemoryStream rs = new MemoryStream(ws.GetBuffer())) { wb = new XSSFWorkbook(rs); sheet = (XSSFSheet)wb.GetSheetAt(0); vml = sheet.GetVMLDrawing(false); shapetype = null; items = vml.GetItems(); foreach (object o in items) { if (o is CT_Shapetype) { shapetype = (CT_Shapetype)o; } } //wb.Write(new FileStream("comments.xlsx", FileMode.Create)); //using (MemoryStream ws2 = new MemoryStream()) //{ // vml.Write(ws2); // throw new System.Exception(System.Text.Encoding.UTF8.GetString(ws2.GetBuffer())); //} Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.stroked); Assert.AreEqual(NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t, shapetype.filled); } } }