public void Test44916() { HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = wb.CreateSheet(); // 1. Create drawing patriarch IDrawing patr = sheet.CreateDrawingPatriarch(); // 2. Try to re-get the patriarch IDrawing existingPatr; try { existingPatr = sheet.DrawingPatriarch; } catch (NullReferenceException) { throw new AssertionException("Identified bug 44916"); } // 3. Use patriarch HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 600, 245, (short)1, 1, (short)1, 2); anchor.AnchorType = (3); byte[] pictureData = HSSFTestDataSamples.GetTestDataFileContent("logoKarmokar4.png"); int idx1 = wb.AddPicture(pictureData, PictureType.PNG); patr.CreatePicture(anchor, idx1); // 4. Try to re-use patriarch later existingPatr = sheet.DrawingPatriarch; Assert.IsNotNull(existingPatr); }
/// <summary> /// 向excel添加图片 /// </summary> /// <param name="XlsFilePath"></param> /// <param name="pictureData"></param> /// <param name="sheetName"></param> public static void AddPicture(string XlsFilePath, byte[] pictureData, bool isNewSheet, string sheetName = "PicTure") { FileStream fs = File.OpenRead(XlsFilePath); //IWorkbook workbook = new XSSFWorkbook(fs); NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook(fs); // (NPOI.XSSF.UserModel.XSSFWorkbook)WorkbookFactory.Create(XlsFilePath); fs.Close(); int pictureIdx = book.AddPicture(pictureData, PictureType.JPEG); NPOI.SS.UserModel.ISheet sheet = null; if (!isNewSheet) { sheet = book.GetSheetAt(0); } else { sheet = book.CreateSheet(sheetName); } IDrawing patriarch = sheet.CreateDrawingPatriarch(); NPOI.XSSF.UserModel.XSSFClientAnchor anchor = new NPOI.XSSF.UserModel.XSSFClientAnchor(0, 0, 1023, 0, 0, 0, 1, 3); IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); pict.Resize(); using (FileStream fileStream = File.Open(XlsFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { book.Write(fileStream); fileStream.Close(); } }
public void TestBasic() { HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = wb.CreateSheet(); IDrawing patr = sheet.CreateDrawingPatriarch(); Assert.IsNotNull(patr); // assert something more interesting }
public void SetUp() { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); workbook = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("Test"); patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); escherGroupA = patriarch.CreateGroup(new HSSFClientAnchor(0, 0, 1022, 255, (short)0, 0, (short)0, 0)); escherGroupB = patriarch.CreateGroup(new HSSFClientAnchor(20, 30, 500, 200, (short)0, 0, (short)0, 0)); // escherGroup = new HSSFShapeGroup(null, new HSSFChildAnchor()); graphics = new EscherGraphics(this.escherGroupA, workbook, System.Drawing.Color.Black, 1.0f); }
public void Test45829() { HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sh1 = wb.CreateSheet(); IDrawing p1 = sh1.CreateDrawingPatriarch(); byte[] pictureData = HSSFTestDataSamples.GetTestDataFileContent("45829.png"); int idx1 = wb.AddPicture(pictureData, PictureType.PNG); IPicture pic = p1.CreatePicture(new HSSFClientAnchor(), idx1); pic.Resize(); }
public void TestAlignment() { HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sh1 = wb.CreateSheet(); HSSFPatriarch patriarch = sh1.CreateDrawingPatriarch() as HSSFPatriarch; HSSFTextbox textbox = patriarch.CreateTextbox(new HSSFClientAnchor(0, 0, 0, 0, 1, 1, 6, 4)) as HSSFTextbox; HSSFRichTextString str = new HSSFRichTextString("Hello, World"); textbox.String = (str); textbox.HorizontalAlignment = HorizontalAlignment.Center; textbox.VerticalAlignment = VerticalAlignment.Center; Assert.AreEqual(HorizontalAlignment.Center, textbox.HorizontalAlignment); Assert.AreEqual(VerticalAlignment.Center, textbox.VerticalAlignment); }
public void TestAlignment() { HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sh1 = wb.CreateSheet(); HSSFPatriarch patriarch = sh1.CreateDrawingPatriarch() as HSSFPatriarch; HSSFTextbox textbox = patriarch.CreateTextbox(new HSSFClientAnchor(0, 0, 0, 0, 1, 1, 6, 4)) as HSSFTextbox; HSSFRichTextString str = new HSSFRichTextString("Hello, World"); textbox.String = (str); textbox.HorizontalAlignment = (HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED); textbox.VerticalAlignment = (HSSFTextbox.VERTICAL_ALIGNMENT_CENTER); Assert.AreEqual(HSSFTextbox.HORIZONTAL_ALIGNMENT_CENTERED, textbox.HorizontalAlignment); Assert.AreEqual(HSSFTextbox.VERTICAL_ALIGNMENT_CENTER, textbox.VerticalAlignment); }
public void TestSetDeafultTextFormat() { HSSFWorkbook wb = new HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = wb.CreateSheet(); HSSFPatriarch patriarch = sheet.CreateDrawingPatriarch() as HSSFPatriarch; HSSFTextbox textbox1 = patriarch.CreateTextbox(new HSSFClientAnchor(0, 0, 0, 0, 1, 1, 3, 3)) as HSSFTextbox; HSSFRichTextString rt1 = new HSSFRichTextString("Hello, World!"); Assert.AreEqual(0, rt1.NumFormattingRuns); textbox1.String = (rt1); HSSFRichTextString rt2 = (HSSFRichTextString)textbox1.String; Assert.AreEqual(1, rt2.NumFormattingRuns); Assert.AreEqual(HSSFRichTextString.NO_FONT, rt2.GetFontOfFormattingRun(0)); }
/// <summary> /// 添加图片 /// </summary> /// <param name="sheet">NPOI工作表</param> /// <param name="picInfo">图片信息</param> public static void AddPicture(this NPOI.SS.UserModel.ISheet sheet, PictureInfo picInfo) { var pictureIdx = sheet.Workbook.AddPicture(picInfo.PictureData, PictureType.PNG); var anchor = sheet.Workbook.GetCreationHelper().CreateClientAnchor(); anchor.Col1 = picInfo.MinCol; anchor.Col2 = picInfo.MaxCol; anchor.Row1 = picInfo.MinRow; anchor.Row2 = picInfo.MaxRow; anchor.Dx1 = picInfo.PictureStyle.AnchorDx1; anchor.Dx2 = picInfo.PictureStyle.AnchorDx2; anchor.Dy1 = picInfo.PictureStyle.AnchorDy1; anchor.Dy2 = picInfo.PictureStyle.AnchorDy2; anchor.AnchorType = AnchorType.MoveDontResize; var drawing = sheet.CreateDrawingPatriarch(); var pic = drawing.CreatePicture(anchor, pictureIdx); if (sheet is HSSFSheet) { var shape = pic as HSSFShape; shape.FillColor = picInfo.PictureStyle.FillColor; shape.IsNoFill = picInfo.PictureStyle.IsNoFill; //shape.LineStyle = picInfo.PictureStyle.LineStyle; shape.LineStyleColor = picInfo.PictureStyle.LineStyleColor; shape.LineWidth = (int)picInfo.PictureStyle.LineWidth; } else if (sheet is XSSFSheet) { var shape = pic as XSSFShape; shape.FillColor = picInfo.PictureStyle.FillColor; shape.IsNoFill = picInfo.PictureStyle.IsNoFill; //shape.LineStyle = picInfo.PictureStyle.LineStyle; //shape.LineStyleColor = picInfo.PictureStyle.LineStyleColor; shape.LineWidth = picInfo.PictureStyle.LineWidth; } }