public void TestRemoveShapes() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.CreateSheet() as HSSFSheet; HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch() as HSSFPatriarch; HSSFSimpleShape rectangle = patriarch.CreateSimpleShape(new HSSFClientAnchor()); rectangle.ShapeType = HSSFSimpleShape.OBJECT_TYPE_RECTANGLE; int idx = wb.AddPicture(new byte[] { 1, 2, 3 }, PictureType.JPEG); patriarch.CreatePicture(new HSSFClientAnchor(), idx); patriarch.CreateCellComment(new HSSFClientAnchor()); HSSFPolygon polygon = patriarch.CreatePolygon(new HSSFClientAnchor()); polygon.SetPoints(new int[] { 1, 2 }, new int[] { 2, 3 }); patriarch.CreateTextbox(new HSSFClientAnchor()); HSSFShapeGroup group = patriarch.CreateGroup(new HSSFClientAnchor()); group.CreateTextbox(new HSSFChildAnchor()); group.CreatePicture(new HSSFChildAnchor(), idx); Assert.AreEqual(patriarch.Children.Count, 6); Assert.AreEqual(group.Children.Count, 2); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 12); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 12); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); Assert.AreEqual(patriarch.Children.Count, 6); group = (HSSFShapeGroup)patriarch.Children[5]; group.RemoveShape(group.Children[0]); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 10); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 10); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); group = (HSSFShapeGroup)patriarch.Children[(5)]; patriarch.RemoveShape(group); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 8); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 8); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); Assert.AreEqual(patriarch.Children.Count, 5); HSSFShape shape = patriarch.Children[0]; patriarch.RemoveShape(shape); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 6); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); Assert.AreEqual(patriarch.Children.Count, 4); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 6); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); Assert.AreEqual(patriarch.Children.Count, 4); HSSFPicture picture = (HSSFPicture)patriarch.Children[0]; patriarch.RemoveShape(picture); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 5); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); Assert.AreEqual(patriarch.Children.Count, 3); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 5); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 1); Assert.AreEqual(patriarch.Children.Count, 3); HSSFComment comment = (HSSFComment)patriarch.Children[0]; patriarch.RemoveShape(comment); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 3); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 0); Assert.AreEqual(patriarch.Children.Count, 2); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 3); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 0); Assert.AreEqual(patriarch.Children.Count, 2); polygon = (HSSFPolygon)patriarch.Children[0]; patriarch.RemoveShape(polygon); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 2); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 0); Assert.AreEqual(patriarch.Children.Count, 1); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 2); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 0); Assert.AreEqual(patriarch.Children.Count, 1); HSSFTextbox textbox = (HSSFTextbox)patriarch.Children[0]; patriarch.RemoveShape(textbox); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 0); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 0); Assert.AreEqual(patriarch.Children.Count, 0); wb = HSSFTestDataSamples.WriteOutAndReadBack(wb); sheet = wb.GetSheetAt(0) as HSSFSheet; patriarch = sheet.DrawingPatriarch as HSSFPatriarch; Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).GetShapeToObjMapping().Count, 0); Assert.AreEqual(HSSFTestHelper.GetEscherAggregate(patriarch).TailRecords.Count, 0); Assert.AreEqual(patriarch.Children.Count, 0); }
public void TestDefaultShapeType() { HSSFComment comment = new HSSFComment((HSSFShape)null, (HSSFAnchor)null); Assert.AreEqual(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.ShapeType); }
public void Main() { InitializeWorkbook(); ISheet sheet = hssfworkbook.CreateSheet("ICell comments in POI HSSF"); // Create the drawing patriarch. This is the top level container for all shapes including cell comments. IDrawing patr = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); //Create a cell in row 3 ICell cell1 = sheet.CreateRow(3).CreateCell(1); cell1.SetCellValue(new HSSFRichTextString("Hello, World")); //anchor defines size and position of the comment in worksheet IComment comment1 = patr.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 4, 2, 6, 5)); // set text in the comment comment1.String = (new HSSFRichTextString("We can set comments in POI")); //set comment author. //you can see it in the status bar when moving mouse over the commented cell comment1.Author = ("Apache Software Foundation"); // The first way to assign comment to a cell is via HSSFCell.SetCellComment method cell1.CellComment = (comment1); //Create another cell in row 6 ICell cell2 = sheet.CreateRow(6).CreateCell(1); cell2.SetCellValue(36.6); HSSFComment comment2 = (HSSFComment)patr.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 4, 8, 6, 11)); //modify background color of the comment comment2.SetFillColor(204, 236, 255); HSSFRichTextString str = new HSSFRichTextString("Normal body temperature"); //apply custom font to the text in the comment IFont font = hssfworkbook.CreateFont(); font.FontName = ("Arial"); font.FontHeightInPoints = 10; font.Boldweight = (short)FontBoldWeight.Bold; font.Color = HSSFColor.Red.Index; str.ApplyFont(font); comment2.String = str; comment2.Visible = true; //by default comments are hidden. This one is always visible. comment2.Author = "Bill Gates"; /** * The second way to assign comment to a cell is to implicitly specify its row and column. * Note, it is possible to set row and column of a non-existing cell. * It works, the commnet is visible. */ comment2.Row = 6; comment2.Column = 1; WriteToFile(); }
public void TestResultEqualsToAbstractShape() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sh = wb.CreateSheet() as HSSFSheet; HSSFPatriarch patriarch = sh.CreateDrawingPatriarch() as HSSFPatriarch; HSSFComment comment = patriarch.CreateCellComment(new HSSFClientAnchor()) as HSSFComment; HSSFRow row = sh.CreateRow(0) as HSSFRow; HSSFCell cell = row.CreateCell(0) as HSSFCell; cell.CellComment = (comment); CommentShape commentShape = HSSFTestModelHelper.CreateCommentShape(1025, comment); Assert.AreEqual(comment.GetEscherContainer().ChildRecords.Count, 5); Assert.AreEqual(commentShape.SpContainer.ChildRecords.Count, 5); //sp record byte[] expected = commentShape.SpContainer.GetChild(0).Serialize(); byte[] actual = comment.GetEscherContainer().GetChild(0).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); expected = commentShape.SpContainer.GetChild(2).Serialize(); actual = comment.GetEscherContainer().GetChild(2).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); expected = commentShape.SpContainer.GetChild(3).Serialize(); actual = comment.GetEscherContainer().GetChild(3).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); expected = commentShape.SpContainer.GetChild(4).Serialize(); actual = comment.GetEscherContainer().GetChild(4).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); ObjRecord obj = comment.GetObjRecord(); ObjRecord objShape = commentShape.ObjRecord; /**shapeId = 1025 % 1024**/ ((CommonObjectDataSubRecord)objShape.SubRecords[0]).ObjectId = (1); expected = obj.Serialize(); actual = objShape.Serialize(); TextObjectRecord tor = comment.GetTextObjectRecord(); TextObjectRecord torShape = commentShape.TextObjectRecord; expected = tor.Serialize(); actual = torShape.Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); NoteRecord note = comment.NoteRecord; NoteRecord noteShape = commentShape.NoteRecord; noteShape.ShapeId = 1; expected = note.Serialize(); actual = noteShape.Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); }
/// <summary> /// 生成导入模版Excel信息 /// </summary> /// <param name="templateModels">模板定义信息</param> /// <param name="title">Sheet名称</param> /// <returns></returns> public static HSSFWorkbook ExportTemplate(List <ExcelTemplate> templateModels, string title) { HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); workbook.SetSheetName(0, title); DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = Company; workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Author = Author; si.ApplicationName = ApplicationName; si.Title = title; si.CreateDateTime = DateTime.Now; workbook.SummaryInformation = si; //取得列宽 int[] arrColWidth = new int[templateModels.Count]; int columnIndex = 0; foreach (var templateModel in templateModels) { arrColWidth[columnIndex] = templateModel.CellLength > 0 ? templateModel.CellLength * 2 : Encoding.UTF8.GetBytes(templateModel.Name.ToString()).Length; columnIndex++; } var headerRow = sheet.CreateRow(0); columnIndex = 0; foreach (var templateModel in templateModels) { var cell = headerRow.CreateCell(columnIndex); if (!string.IsNullOrEmpty(templateModel.ExportComments)) { HSSFPatriarch patr = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFComment comment = (HSSFComment)patr.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 1, 2, 4, 16)); comment.String = new HSSFRichTextString(templateModel.ExportComments); comment.Author = ApplicationName; cell.CellComment = comment; } if (templateModel.DictionaryItems != null && templateModel.DictionaryItems.Count > 0) { DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(templateModel.DictionaryItems.ToArray()); CellRangeAddressList regions = new CellRangeAddressList(1, 65535, columnIndex, columnIndex); IDataValidation validation = new HSSFDataValidation(regions, constraint); sheet.AddValidationData(validation); } cell.SetCellValue(templateModel.Name); if (templateModel.IsRequred) { var headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; var font = workbook.CreateFont(); font.Color = HSSFColor.Red.Index; font.FontHeightInPoints = 10; font.IsBold = true; headStyle.SetFont(font); cell.CellStyle = headStyle; } else { var headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; var font = workbook.CreateFont(); font.Color = HSSFColor.Black.Index; font.FontHeightInPoints = 10; font.IsBold = true; headStyle.SetFont(font); cell.CellStyle = headStyle; } //设置列宽 sheet.SetColumnWidth(columnIndex, (arrColWidth[columnIndex] + 1) * 256); columnIndex++; } return(workbook); }
public void ResultEqualsToNonExistingAbstractShape() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sh = wb.CreateSheet() as HSSFSheet; HSSFPatriarch patriarch = sh.CreateDrawingPatriarch() as HSSFPatriarch; HSSFComment comment = patriarch.CreateCellComment(new HSSFClientAnchor()) as HSSFComment; HSSFRow row = sh.CreateRow(0) as HSSFRow; HSSFCell cell = row.CreateCell(0) as HSSFCell; cell.CellComment = (comment); Assert.AreEqual(comment.GetEscherContainer().ChildRecords.Count, 5); //sp record byte[] expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAAFvEw/WBg4GBgZEFSHAxMAAA9gX7nhAAAAA="); byte[] actual = comment.GetEscherContainer().GetChild(0).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAAGNgEPggxIANAABK4+laGgAAAA=="); actual = comment.GetEscherContainer().GetChild(2).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAAGNgEPzAAAQACl6c5QgAAAA="); actual = comment.GetEscherContainer().GetChild(3).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAAGNg4P3AAAQA6pyIkQgAAAA="); actual = comment.GetEscherContainer().GetChild(4).Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); ObjRecord obj = comment.GetObjRecord(); expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAAItlMGEQZRBikGRgZBF0YEACvAxiDLgBAJZsuoU4AAAA"); actual = obj.Serialize(); Assert.AreEqual(expected.Length, actual.Length); //assertArrayEquals(expected, actual); TextObjectRecord tor = comment.GetTextObjectRecord(); expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAANvGKMQgxMSABgBGi8T+FgAAAA=="); actual = tor.Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); NoteRecord note = comment.NoteRecord; expected = TestDrawingAggregate.decompress("H4sIAAAAAAAAAJNh4GGAAEYWEAkAS0KXuRAAAAA="); actual = note.Serialize(); Assert.AreEqual(expected.Length, actual.Length); Assert.IsTrue(Arrays.Equals(expected, actual)); wb.Close(); }
public static CommentShape CreateCommentShape(int shapeId, HSSFComment comment) { return(new CommentShape(comment, shapeId)); }
/// <summary> /// 获取Excel内存数据 /// </summary> /// <param name="dataSource">数据源</param> /// <param name="sheetName">Excel名字</param> /// <returns></returns> private static MemoryStream GetExcelData(DataTable dataSource, string sheetName) { if (dataSource == null) { throw new ArgumentException("dataSource is null"); } if (string.IsNullOrEmpty(sheetName)) { sheetName = "UnKown"; } //打开Excel对象 HSSFWorkbook _book = new HSSFWorkbook(); DocumentSummaryInformation _dom_summary_info = PropertySetFactory.CreateDocumentSummaryInformation(); _dom_summary_info.Company = ""; _book.DocumentSummaryInformation = _dom_summary_info; SummaryInformation _summary_info = PropertySetFactory.CreateSummaryInformation(); _summary_info.Subject = ""; _book.SummaryInformation = _summary_info; //Excel的Sheet对象 ISheet _sheet = _book.CreateSheet(sheetName); DataColumnCollection _columns = dataSource.Columns; DataRowCollection _rows = dataSource.Rows; IRow _row = _sheet.CreateRow(0); // 用于格式化单元格的数据 HSSFDataFormat format = (HSSFDataFormat)_book.CreateDataFormat(); // 设置列头字体 HSSFFont font = (HSSFFont)_book.CreateFont(); font.FontHeightInPoints = 18; //字体高度 //font.Color = (short)FontColor.RED; font.FontName = "隶书"; //字体 font.Boldweight = (short)FontBoldWeight.Bold; //font.IsItalic = true; //是否使用斜体 //font.IsStrikeout = true; //是否使用划线 //设置文本字体 HSSFFont fontContent = (HSSFFont)_book.CreateFont(); fontContent.FontHeightInPoints = 10; fontContent.FontName = "宋体"; // 设置单元格类型 HSSFCellStyle cellStyle = (HSSFCellStyle)_book.CreateCellStyle(); cellStyle.SetFont(font); cellStyle.Alignment = HorizontalAlignment.Center; //水平布局:居中 cellStyle.WrapText = false; // 添加单元格注释 // 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器. HSSFPatriarch patr = (HSSFPatriarch)_sheet.CreateDrawingPatriarch(); // 定义注释的大小和位置,详见文档 HSSFComment comment = patr.CreateComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5)); // 设置注释内容 comment.String = new HSSFRichTextString("列标题"); // 设置注释作者. 当鼠标移动到单元格上是可以在状态栏中看到该内容. comment.Author = "天蓝海"; //set date format ICellStyle cellStyleDate = _book.CreateCellStyle(); cellStyleDate.SetFont(fontContent); cellStyleDate.Alignment = HorizontalAlignment.Center; //水平布局:居中 cellStyleDate.DataFormat = format.GetFormat("yyyy-m-d HH:MM:SS"); //数据格式 ICellStyle cellStyleNumber = _book.CreateCellStyle(); cellStyleNumber.SetFont(fontContent); cellStyleNumber.Alignment = HorizontalAlignment.Center; //水平布局:居中 cellStyleNumber.DataFormat = format.GetFormat("0.00"); //生成列名,根据DataTable的列名 for (int i = 0; i < _columns.Count; i++) { // 创建单元格 ICell cell = _row.CreateCell(i); HSSFRichTextString hssfString = new HSSFRichTextString(_columns[i].ColumnName); cell.SetCellValue(hssfString); //设置单元格内容 cell.CellStyle = cellStyle; //设置单元格样式 cell.SetCellType(CellType.String); //指定单元格格式:数值、公式或字符串 cell.CellComment = comment; //添加注释 } //填充数据 for (int j = 0; j < _rows.Count; j++) { _row = _sheet.CreateRow(j + 1); for (int k = 0; k < _columns.Count; k++) { ICell cell = _row.CreateCell(k); switch (_columns[k].DataType.ToString()) { case "System.String": HSSFRichTextString hssfString = new HSSFRichTextString(_rows[j][k].ToString()); cell.SetCellValue(hssfString); cell.SetCellType(CellType.String); break; case "System.Decimal": cell.SetCellValue((double)(decimal)_rows[j][k]); cell.CellStyle = cellStyleNumber; break; case "System.DateTime": cell.SetCellValue((DateTime)_rows[j][k]); cell.CellStyle = cellStyleDate; break; } //自动换行 if (_rows[j][k].ToString().Contains("\r\n")) { cell.CellStyle.WrapText = true; } } } //设置自动列宽 for (int i = 0; i < _columns.Count; i++) { _sheet.AutoSizeColumn(i); } //保存excel文档 _sheet.ForceFormulaRecalculation = true; MemoryStream _stream = new MemoryStream(); _book.Write(_stream); return(_stream); }
public VTComment(ref HSSFWorkbook workbook, string sheetName) { IDrawing patr = (HSSFPatriarch)workbook.GetSheet(sheetName).CreateDrawingPatriarch(); Comment = (HSSFComment)patr.CreateCellComment(new HSSFClientAnchor()); }
public VTComment(HSSFComment comment) { Comment = comment; }