static void Main(string[] args) { XWPFDocument doc = new XWPFDocument(); XWPFParagraph para = doc.CreateParagraph(); XWPFRun r0 = para.CreateRun(); r0.SetText("Title1"); para.BorderTop = Borders.Thick; para.FillBackgroundColor = "EEEEEE"; para.FillPattern = NPOI.OpenXmlFormats.Wordprocessing.ST_Shd.diagStripe; XWPFTable table = doc.CreateTable(3, 3); table.GetRow(1).GetCell(1).SetText("EXAMPLE OF TABLE"); XWPFTableCell c1 = table.GetRow(0).GetCell(0); XWPFParagraph p1 = c1.AddParagraph(); // Use this instead of doc.CreateParagraph. XWPFRun r1 = p1.CreateRun(); r1.SetText("The quick brown fox"); r1.IsBold = true; r1.FontFamily = "Courier"; r1.SetUnderline(UnderlinePatterns.DotDotDash); r1.TextPosition = 100; c1.SetColor("FF0000"); table.GetRow(2).GetCell(2).SetText("only text"); FileStream out1 = new FileStream("SimpleTableNPOI.docx", FileMode.Create); doc.Write(out1); out1.Close(); }
static void Main(string[] args) { // Create a new document from scratch XWPFDocument doc = new XWPFDocument(); XWPFTable table = doc.CreateTable(3, 3); table.GetRow(1).GetCell(1).SetText("EXAMPLE OF TABLE"); XWPFTableCell c1 = table.GetRow(0).GetCell(0); XWPFParagraph p1 = c1.AddParagraph(); //don't use doc.CreateParagraph XWPFRun r1 = p1.CreateRun(); r1.SetText("This is test table contents"); r1.IsBold = true; r1.FontFamily = "Courier"; r1.SetUnderline(UnderlinePatterns.DotDotDash); r1.SetTextPosition(100); c1.SetColor("FF0000"); table.GetRow(2).GetCell(2).SetText("only text"); FileStream out1 = new FileStream("data/Format Table in Document.docx", FileMode.Create); doc.Write(out1); out1.Close(); }
public static XWPFTableCell GetCell(XWPFTableRow row, int line, XWPFTableRow modelRow) { XWPFTableCell cell = row.GetCell(line); if (cell == null) { cell = row.CreateCell(); if (modelRow != null) { var modelcell = modelRow.GetCell(line); cell.SetVerticalAlignment(modelcell.GetVerticalAlignment()); cell.SetColor(modelcell.GetColor()); } } return(cell); }
public void TestSetGetColor() { // create a table XWPFDocument doc = new XWPFDocument(); CT_Tbl ctTable = new CT_Tbl(); XWPFTable table = new XWPFTable(ctTable, doc); // table has a single row by default; grab it XWPFTableRow tr = table.GetRow(0); Assert.IsNotNull(tr); // row has a single cell by default; grab it XWPFTableCell cell = tr.GetCell(0); cell.SetColor("F0000F"); String clr = cell.GetColor(); Assert.AreEqual("F0000F", clr); }
private static XWPFTableCell SetCell(XWPFTableCell cell, string text, int width, string color = "", bool isCenter = true, int fontSize = 0, bool isBold = false) { CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctpr = cttc.AddNewTcPr(); //垂直居中 ctpr.AddNewVAlign().val = ST_VerticalJc.center; //设置单元格列宽 ctpr.tcW = new CT_TblWidth(); ctpr.tcW.w = width.ToString(); ctpr.tcW.type = ST_TblWidth.dxa; //设置单元格背景色 if (!string.IsNullOrEmpty(color)) { cell.SetColor(color); } //设置文本 text = text == "PRI" ? "Y" : text; text = text == "YES" ? "Y" : text; text = text == "NO" ? "N" : text; text = text == "CURRENT_TIMESTAMP" ? "now()" : text; cell.RemoveParagraph(0); XWPFParagraph gp = cell.AddParagraph(); if (isCenter) { //水平居中 gp.Alignment = ParagraphAlignment.CENTER; } //文字格式 gp.Style = "NoSpacing"; XWPFRun gr = gp.CreateRun(); if (fontSize > 0) { gr.FontSize = fontSize; } gr.IsBold = isBold; gr.SetText(text); return(cell); }
static void Main(string[] args) { XWPFDocument doc = new XWPFDocument(); XWPFParagraph para = doc.CreateParagraph(); XWPFRun r0 = para.CreateRun(); r0.SetText("Title1"); para.BorderTop = Borders.Thick; para.FillBackgroundColor = "EEEEEE"; para.FillPattern = NPOI.OpenXmlFormats.Wordprocessing.ST_Shd.diagStripe; XWPFTable table = doc.CreateTable(3, 3); table.GetRow(1).GetCell(1).SetText("EXAMPLE OF TABLE"); XWPFTableCell c1 = table.GetRow(0).GetCell(0); XWPFParagraph p1 = c1.AddParagraph(); //don't use doc.CreateParagraph XWPFRun r1 = p1.CreateRun(); r1.SetText("The quick brown fox"); r1.IsBold = true; r1.FontFamily = "Courier"; r1.SetUnderline(UnderlinePatterns.DotDotDash); r1.TextPosition = 100; c1.SetColor("FF0000"); table.GetRow(2).GetCell(2).SetText("only text"); var r2 = table.GetRow(2).GetCell(0).AddParagraph().CreateRun(); var widthEmus = (int)(400.0 * 9525); var heightEmus = (int)(300.0 * 9525); using (FileStream picData = new FileStream("image/HumpbackWhale.jpg", FileMode.Open, FileAccess.Read)) { r2.AddPicture(picData, (int)PictureType.JPEG, "image1", widthEmus, heightEmus); } using (FileStream fs = new FileStream("simpleTable.docx", FileMode.Create)) { doc.Write(fs); } }
private static void CopyCell(XWPFTableCell templateCell, XWPFTableCell cell) { cell.SetColor(templateCell.GetColor()); CT_TcBorders templateBorders = templateCell.GetCTTc().tcPr.tcBorders; if (templateBorders != null) { CT_TcBorders borders = cell.GetCTTc().AddNewTcPr().AddNewTcBorders(); WordGenerator.CopyBorders(templateBorders, borders); } for (int i = 0; i < cell.Paragraphs.Count; i++) { cell.RemoveParagraph(0); } foreach (XWPFParagraph templateph in templateCell.Paragraphs) { XWPFParagraph ph = cell.AddParagraph(); WordGenerator.CopyParagraph(templateph, ph); } }
private static void SaveWordDoc(string pFfName) { try { XWPFDocument doc = new XWPFDocument(); XWPFParagraph para = doc.CreateParagraph(); XWPFRun r0 = para.CreateRun(); r0.SetText("FileFormat"); para.FillBackgroundColor = "EEEEEE"; para.FillPattern = NPOI.OpenXmlFormats.Wordprocessing.ST_Shd.diagStripe; XWPFTable table = doc.CreateTable(3, 3); table.GetRow(1).GetCell(1).SetText("File Format Developer Guide"); XWPFTableCell c1 = table.GetRow(0).GetCell(0); XWPFParagraph p1 = c1.AddParagraph(); XWPFRun r1 = p1.CreateRun(); r1.SetText("NPOI - DOCX"); r1.IsBold = true; r1.FontFamily = "Courier"; r1.SetUnderline(UnderlinePatterns.DotDotDash); // r1.SetTextPosition(100); c1.SetColor("FF0000"); table.GetRow(2).GetCell(2).SetText("only text"); FileStream out1 = new FileStream(pFfName, FileMode.Create); doc.Write(out1); out1.Close(); } catch (Exception) { throw; } }
public void Write() { XWPFDocument mydoc = new XWPFDocument(); //设置页面大小 CT_SectPr size = new CT_SectPr(); size.pgSz.w = 11900; size.pgSz.h = 16830; mydoc.Document.body.sectPr = size; //创建段落 XWPFParagraph p1 = mydoc.CreateParagraph(); //设置段落样式 p1.Alignment = ParagraphAlignment.CENTER;//居中 XWPFRun run1 = p1.CreateRun(); XWPFRun run3 = p1.CreateRun(); run3.SetText("测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"); run1.FontSize = 16; //设置字体大小 run1.SetColor("#808080"); //设置字体颜色 run1.SetText("标题文本标题文本标题文本标题文本标题文本"); //创建段落 XWPFParagraph p2 = mydoc.CreateParagraph(); p2.Alignment = ParagraphAlignment.CENTER;//居中 XWPFRun run2 = p2.CreateRun(); run2.SetColor("#123456"); run2.FontSize = 8; run2.SetText("时间:" + DateTime.Now.ToString()); //获取数据 DataSet set = GetData(); DataTable table = set.Tables[0]; //创建表单 XWPFTable table1 = mydoc.CreateTable(table.Rows.Count + 1, table.Columns.Count); //设置第一行表格 XWPFTableCell firstcell1 = table1.GetRow(0).GetCell(0); //XWPFRun firstcell1 = t1.CreateRun(); firstcell1.SetColor("#FF0000"); firstcell1.SetText("name1"); XWPFTableCell firstcell2 = table1.GetRow(0).GetCell(1); //XWPFRun firstcell2 = t2.CreateRun(); firstcell2.SetColor("#FF0000"); firstcell2.SetText("name2"); XWPFTableCell firstcell3 = table1.GetRow(0).GetCell(2); //XWPFRun firstcell3 = t3.CreateRun(); firstcell3.SetColor("#FF0000"); firstcell3.SetText("id"); /** * 这些单元格默认换行 */ for (int row = 0; row < table.Rows.Count; row++) { for (int colum = 0; colum < table.Columns.Count; colum++) { table1.GetRow(row + 1).Height = 10; XWPFParagraph t = table1.GetRow(row + 1).GetCell(colum).AddParagraph(); XWPFRun rIO = t.CreateRun(); rIO.FontSize = 5; rIO.IsBold = true; rIO.SetText(table.Rows[row][colum].ToString()); } } //行合并 //table1.GetRow(2).MergeCells(0, 1); FileStream stream = new FileStream(filename, FileMode.Create); mydoc.Write(stream); stream.Close(); mydoc.Close(); }