Exemple #1
0
        static void Main(string[] args)
        {
            XWPFDocument doc        = new XWPFDocument();
            XWPFTable    table1     = doc.CreateTable(3, 3);
            var          tblLayout1 = table1.GetCTTbl().tblPr.AddNewTblLayout();

            tblLayout1.type = ST_TblLayoutType.@fixed;
            table1.SetColumnWidth(0, 1200);
            table1.SetColumnWidth(1, 1200);
            table1.SetColumnWidth(2, 1200);

            XWPFTable table2     = doc.CreateTable(2, 3);
            var       tblLayout2 = table2.GetCTTbl().tblPr.AddNewTblLayout();

            tblLayout2.type = ST_TblLayoutType.@fixed;
            table2.SetColumnWidth(0, 1500);
            table2.SetColumnWidth(1, 1500);
            table2.SetColumnWidth(2, 1500);
            table2.GetCTTbl().tblPr.AddNewTblPPr(); //tblPr.AddNewTblPPr is available since NPOI 2.5.5
            var tblpPr = table2.GetCTTbl().tblPr.tblpPr;

            tblpPr.leftFromText = 180;
            tblpPr.topFromText  = 180;
            tblpPr.vertAnchor   = ST_VAnchor.text;
            tblpPr.horzAnchor   = ST_HAnchor.page;
            tblpPr.tblpY        = "-840"; //this value is tricky, you have to calculate the vertical offset by the row number of the first table byyourself
            tblpPr.tblpX        = "5800";

            using (FileStream fs = new FileStream("complexTable.docx", FileMode.Create))
            {
                doc.Write(fs);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            XWPFDocument doc = new XWPFDocument();

            XWPFTable table = doc.CreateTable(3, 3);

            table.GetRow(1).GetCell(1).SetText("EXAMPLE OF TABLE");


            XWPFParagraph p1 = doc.CreateParagraph();

            XWPFRun r1 = p1.CreateRun();

            r1.SetBold(true);
            r1.SetText("The quick brown fox");
            r1.SetBold(true);
            r1.SetFontFamily("Courier");
            r1.SetUnderline(UnderlinePatterns.DotDotDash);
            r1.SetTextPosition(100);

            table.GetRow(0).GetCell(0).SetParagraph(p1);


            table.GetRow(2).GetCell(2).SetText("only text");

            FileStream out1 = new FileStream("simpleTable.docx", FileMode.Create);

            doc.Write(out1);
            out1.Close();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            XWPFDocument document     = new XWPFDocument();
            XWPFTable    tableOne     = document.CreateTable();
            XWPFTableRow tableOneRow1 = tableOne.GetRow(0);
            XWPFTableRow tableOneRow2 = tableOne.CreateRow();

            tableOneRow1.GetCell(0).SetText("Test11");
            tableOneRow1.AddNewTableCell();
            tableOneRow1.GetCell(1).SetText("Test12");
            tableOneRow2.GetCell(0).SetText("Test21");
            tableOneRow2.AddNewTableCell();

            XWPFTableCell cell  = tableOneRow2.GetCell(1);
            var           ctTbl = cell.GetCTTc().AddNewTbl();

            //to remove the line from the cell, you can call cell.removeParagraph(0) instead
            cell.SetText("line1");
            cell.GetCTTc().AddNewP();

            XWPFTable    tableTwo     = new XWPFTable(ctTbl, cell);
            XWPFTableRow tableTwoRow1 = tableTwo.GetRow(0);

            tableTwoRow1.GetCell(0).SetText("nestedTable11");
            tableTwoRow1.AddNewTableCell();
            tableTwoRow1.GetCell(1).SetText("nestedTable12");

            using (FileStream fs = new FileStream("nestedTable.docx", FileMode.Create))
            {
                document.Write(fs);
            }
        }
Exemple #4
0
        private static void CreateTable(XWPFDocument doc, List <EvalutationDataModule> data, Dictionary <int, BasicFourModule> fourModules)
        {
            XWPFTable table = doc.CreateTable(data.Count + 1, 5);

            table.SetCellMargins(50, 50, 50, 50);
            table.SetColumnWidth(0, 256 * 10);
            table.SetColumnWidth(1, 256 * 4);
            table.SetColumnWidth(2, 256 * 10);
            table.SetColumnWidth(3, 256 * 4);
            table.SetColumnWidth(4, 256 * 4);
            CreateText(table.GetRow(0).GetCell(0), "评价内容", true);
            CreateText(table.GetRow(0).GetCell(1), "标准分值", true);
            CreateText(table.GetRow(0).GetCell(2), "评价依据", true);
            CreateText(table.GetRow(0).GetCell(3), "得分", true);
            CreateText(table.GetRow(0).GetCell(4), "附件", true);

            for (int i = 0; i < data.Count; i++)
            {
                CreateText(table.GetRow(i + 1).GetCell(0), fourModules[data[i].IndicatorFour].Name, false);
                CreateText(table.GetRow(i + 1).GetCell(1), fourModules[data[i].IndicatorFour].BasicScore.ToString(), false);
                CreateText(table.GetRow(i + 1).GetCell(2), data[i].Description, false);
                CreateText(table.GetRow(i + 1).GetCell(3), data[i].Grade.ToString(), false);
                string source = string.Join("\r\n", data[i].DataSource);
                if (string.IsNullOrEmpty(source))
                {
                    source = " ";//乱码
                }
                CreateText(table.GetRow(i + 1).GetCell(4), source, false);
            }

            XWPFParagraph paragraph = doc.CreateParagraph();

            paragraph.IsPageBreak = true;//分页符
        }
Exemple #5
0
        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();
        }
Exemple #6
0
        /// <summary>
        /// 导出Docx
        /// </summary>
        /// <param name="localFilePath">文件保存路径</param>
        /// <param name="dtSource">数据源</param>
        public static void ExportDocx(string localFilePath, System.Data.DataTable dtSource)
        {
            XWPFDocument doc = new XWPFDocument();

            XWPFTable table = doc.CreateTable(dtSource.Rows.Count + 1, dtSource.Columns.Count);

            for (int i = 0; i < dtSource.Rows.Count + 1; i++)
            {
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    if (i == 0)
                    {
                        table.GetRow(i).GetCell(j).SetText(dtSource.Columns[j].ColumnName);
                    }
                    else
                    {
                        table.GetRow(i).GetCell(j).SetText(dtSource.Rows[i - 1][j].ToString());
                    }
                }
            }

            using (FileStream fs = new FileStream(localFilePath, FileMode.Create, FileAccess.Write))
            {
                doc.Write(fs);
            }
        }
Exemple #7
0
        private static void ExportToDocx(string json, [DisallowNull] string outputPath)
        {
            outputPath        = AmendOutputPath(outputPath, ".docx");
            using var jobject = JsonDocument.Parse(json);
            var data = jobject.RootElement.GetProperty("data");
            var rows = data.EnumerateArray().Count();
            var cols = 0;

            if (rows > 0)
            {
                cols = data.EnumerateArray().First().EnumerateObject().Count();
            }
            var document = new XWPFDocument();
            var table    = document.CreateTable(rows, cols);
            //for (int col = 0; col < cols; col++) { // not work
            //    table.SetColumnWidth(col, 100);
            //}

            var row = 0;

            foreach (var token in data.EnumerateArray())
            {
                int pos = 0;
                foreach (var text in token.EnumerateObject())
                {
                    table.GetRow(row).GetCell(pos++).SetText(text.Value.GetString());
                }
                row++;
            }
            using (var fs = File.Create(outputPath))
            {
                document.Write(fs);
            }
        }
Exemple #8
0
        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();
        }
Exemple #9
0
        public void TestCreateTable()
        {
            // open an empty document
            XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx");

            // create a table with 5 rows and 7 coloumns
            int       noRows = 5;
            int       noCols = 7;
            XWPFTable table  = doc.CreateTable(noRows, noCols);

            // assert the table is empty
            List <XWPFTableRow> rows = table.Rows;

            Assert.AreEqual(noRows, rows.Count, "Table has less rows than requested.");
            foreach (XWPFTableRow xwpfRow in rows)
            {
                Assert.IsNotNull(xwpfRow);
                for (int i = 0; i < 7; i++)
                {
                    XWPFTableCell xwpfCell = xwpfRow.GetCell(i);
                    Assert.IsNotNull(xwpfCell);
                    Assert.AreEqual(1, xwpfCell.Paragraphs.Count, "Empty cells should not have one paragraph.");
                    xwpfCell = xwpfRow.GetCell(i);
                    Assert.AreEqual(1, xwpfCell.Paragraphs.Count, "Calling 'getCell' must not modify cells content.");
                }
            }
            doc.Package.Revert();
        }
Exemple #10
0
        private static void ExportToDocx(string json, string outputPath)
        {
            outputPath = AmendOutputPath(outputPath, ".docx");
            JObject jobject = JObject.Parse(json);
            JToken  data    = jobject["data"];
            int     rows    = data.Count();
            int     cols    = 0;

            if (rows > 0)
            {
                cols = data.First().Values().Count();
            }
            XWPFDocument document = new XWPFDocument();
            XWPFTable    table    = document.CreateTable(rows, cols);
            //for (int col = 0; col < cols; col++) { // not work
            //    table.SetColumnWidth(col, 100);
            //}

            int row = 0;

            foreach (JToken token in data)
            {
                int pos = 0;
                foreach (JToken text in token.Values())
                {
                    table.GetRow(row).GetCell(pos++).SetText(text.ToString());
                }
                row++;
            }
            using (FileStream fs = File.Create(outputPath)) {
                document.Write(fs);
            }
        }
Exemple #11
0
        /// <summary>
        /// 为XWPFDocument文档复制指定索引的表
        /// </summary>
        /// <param name="readDoc">模板文件</param>
        /// <param name="tableIndex">需要复制模板的table的索引</param>
        /// <param name="targetIndex">复制到目标位置的table索引(如果目标位置原来有表格,会被覆盖)</param>
        /// <param name="myDoc">新创建的文件</param>
        public static void CopyTable(XWPFDocument readDoc, int tableIndex, int targetIndex, XWPFDocument myDoc)
        {
            var    sourceTable = readDoc.Tables[tableIndex];
            CT_Tbl sourceCTTbl = readDoc.Document.body.GetTblArray(8);

            var targetTable = myDoc.CreateTable();

            myDoc.SetTable(targetIndex, targetTable);
            var targetCTTbl = myDoc.Document.body.GetTblArray()[myDoc.Document.body.GetTblArray().Length - 1];

            targetCTTbl.tblPr        = sourceCTTbl.tblPr;
            targetCTTbl.tblPr.jc.val = ST_Jc.left;//表格在页面水平位置
            //targetCTTbl.tblGrid = sourceCTTbl.tblGrid;

            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                var tbRow     = targetTable.CreateRow();
                var targetRow = tbRow.GetCTRow();
                tbRow.RemoveCell(0);
                XWPFTableRow row = sourceTable.Rows[i];
                targetRow.trPr = row.GetCTRow().trPr;
                for (int c = 0; c < row.GetTableCells().Count; c++)
                {
                    var tbCell = tbRow.CreateCell();
                    tbCell.RemoveParagraph(0);
                    var targetCell = tbCell.GetCTTc();

                    XWPFTableCell cell = row.GetTableCells()[c];
                    targetCell.tcPr = cell.GetCTTc().tcPr;
                    for (int p = 0; p < cell.Paragraphs.Count; p++)
                    {
                        var           tbPhs     = tbCell.AddParagraph();
                        CT_P          targetPhs = tbPhs.GetCTP();
                        XWPFParagraph para      = cell.Paragraphs[p];
                        var           paraCTP   = para.GetCTP();
                        targetPhs.pPr          = paraCTP.pPr;
                        targetPhs.rsidR        = paraCTP.rsidR;
                        targetPhs.rsidRPr      = paraCTP.rsidRPr;
                        targetPhs.rsidRDefault = paraCTP.rsidRDefault;
                        targetPhs.rsidP        = paraCTP.rsidP;

                        for (int r = 0; r < para.Runs.Count; r++)
                        {
                            var  tbRun     = tbPhs.CreateRun();
                            CT_R targetRun = tbRun.GetCTR();

                            XWPFRun run    = para.Runs[r];
                            var     runCTR = run.GetCTR();
                            targetRun.rPr     = runCTR.rPr;
                            targetRun.rsidRPr = runCTR.rsidRPr;
                            targetRun.rsidR   = runCTR.rsidR;
                            CT_Text text = targetRun.AddNewT();
                            text.Value = run.Text;
                        }
                    }
                }
            }
            targetTable.RemoveRow(0);
        }
Exemple #12
0
        /// <summary>
        /// 设置数据文档的表
        /// </summary>
        /// <param name="document">文档</param>
        /// <param name="table">当前表</param>
        /// <param name="no">当前表编号,从1开始</param>
        public static void SetTableWord(XWPFDocument document, TableMeta table, Int32 no)
        {
            //表名
            XWPFParagraph p = document.CreateParagraph();

            p.Alignment = ParagraphAlignment.LEFT;
            XWPFRun r = p.CreateRun();

            r.SetText($"{no}.{table.TableName}");
            r.FontSize = 14;
            r.IsBold   = true;

            if (!string.IsNullOrEmpty(table.Comment))
            {
                //表注释
                p           = document.CreateParagraph();
                p.Alignment = ParagraphAlignment.LEFT;
                r           = p.CreateRun();
                r.SetText(table.Comment);
                r.FontSize = 14;
                r.IsBold   = true;
            }


            //表格
            XWPFTable grid = document.CreateTable(table.Columns.Count + 1, 5);



            grid.Width = 2500;
            grid.SetColumnWidth(0, 256 * 2);
            grid.SetColumnWidth(1, 256 * 2);
            grid.SetColumnWidth(2, 256 * 1);
            grid.SetColumnWidth(3, 256 * 1);
            grid.SetColumnWidth(4, 256 * 4);



            //设置表头
            XWPFTableRow row = grid.GetRow(0);

            row.GetCell(0).SetParagraph(SetCellText(document, grid, "字段名"));
            row.GetCell(1).SetParagraph(SetCellText(document, grid, "类型"));
            row.GetCell(2).SetParagraph(SetCellText(document, grid, "是否主键"));
            row.GetCell(3).SetParagraph(SetCellText(document, grid, "可为空"));
            row.GetCell(4).SetParagraph(SetCellText(document, grid, "说明"));

            for (int i = 0; i < table.Columns.Count; i++)
            {
                ColumnMeta col = table.Columns[i];
                row = grid.GetRow(i + 1);
                row.GetCell(0).SetParagraph(SetCellText(document, grid, col.ColumnName));
                row.GetCell(1).SetParagraph(SetCellText(document, grid, col.FieldTypeName));
                row.GetCell(2).SetParagraph(SetCellText(document, grid, col.IsKey ? "是" : "否"));
                row.GetCell(3).SetParagraph(SetCellText(document, grid, col.AllowDBNull ? "是" : "否"));
                row.GetCell(4).SetParagraph(SetCellText(document, grid, string.IsNullOrEmpty(col.Comment)?string.Empty:col.Comment));
            }
        }
Exemple #13
0
        private static void DoAddTable(XWPFDocument doc, XWPFTableCell cell, XWPFParagraph paragraph, AddTableOptions TableOptions)
        {
            string          runText;
            XWPFRun         newRun;
            IList <XWPFRun> listRun = paragraph.Runs;

            if (TableOptions != null && TableOptions.dataTable != null && TableOptions.dataTable.Rows.Count > 0)
            {
                var nCls  = TableOptions.dataTable.Columns.Count;
                var nRows = TableOptions.dataTable.Rows.Count;

                for (int i = 0; i < listRun.Count; i++)
                {
                    XWPFRun run = listRun[i];
                    runText = run.Text;
                    if (run.Text == TableOptions.PlaceHolder.ToString())
                    {
                        paragraph.RemoveRun(i);
                        //newRun = paragraph.InsertNewRun(i);
                        //CT_Tbl cT_Tbl = doc.Document.body.AddNewTbl();
                        //XWPFTable tb = new XWPFTable(cT_Tbl, cell, nRows + 2, nCls);
                        var tb = doc.CreateTable(nRows + 2, nCls);
                        int j  = 0;
                        //tb.
                        foreach (DataColumn c in TableOptions.dataTable.Columns)
                        {
                            //var tbw=tb.GetRow(0).GetCell(j).GetCTTc();
                            //CT_TcPr ctPr = tbw.AddNewTcPr();                       //添加TcPr
                            //ctPr.tcW = new CT_TblWidth();
                            //ctPr.tcW.w = "200";//单元格宽
                            //ctPr.tcW.type = ST_TblWidth.dxa;
                            tb.GetRow(0).GetCell(j).SetColor("#AEAAAA");
                            XWPFParagraph pIO = tb.GetRow(0).GetCell(j).AddParagraph();
                            XWPFRun       rIO = pIO.CreateRun();
                            rIO.FontFamily = "宋体";
                            rIO.FontSize   = 11;
                            rIO.IsBold     = true;
                            //rIO.SetColor("#AEAAAA");
                            rIO.SetText(c.ColumnName);

                            //tb.GetRow(0).GetCell(j).SetColor("#AEAAAA");
                            //tb.GetRow(0).GetCell(j).SetText(c.ColumnName);
                            j++;
                        }
                        j = 0;
                        for (int n = 0; n < nRows; n++)
                        {
                            var dr = TableOptions.dataTable.Rows[n];
                            for (int m = 0; m < nCls; m++)
                            {
                                tb.GetRow(n + 1).GetCell(m).SetText(dr[m].ToString());
                            }
                        }
                    }
                }
            }
        }
        private XWPFDocument _addNotesBlock(XWPFDocument document)
        {
            var table = document.CreateTable(4, 1);

            table.Width = _pageWidth;

            _createTableCell(table.GetRow(0).GetCell(0), "Notes", ParagraphAlignment.CENTER, true);

            return(document);
        }
Exemple #15
0
        /// <summary>
        /// word 插入表格功能(13行2列)
        /// </summary>
        /// <param name="m_Docx">根文档</param>
        /// <param name="device_type">设备类型</param>
        /// <param name="kilometer_mark">公里标</param>
        /// <param name="side_direction">下行侧向</param>
        /// <param name="longitude">经度</param>
        /// <param name="latitude">纬度</param>
        private static void word_inster_table(XWPFDocument m_Docx, DbBean bean, int i = 1)
        {
            XWPFTable table  = m_Docx.CreateTable(12, 2);
            CT_Tbl    ctbl   = m_Docx.Document.body.GetTblArray()[i];
            CT_TblPr  ctblpr = ctbl.AddNewTblPr();

            ctblpr.jc     = new CT_Jc();
            ctblpr.jc.val = ST_Jc.center;

            table.Width = 3500;
            table.GetRow(0).GetCell(0).SetText("设备类型");
            table.GetRow(0).GetCell(1).SetText(bean.DeviceType);
            table.GetRow(1).GetCell(0).SetText("公里标");
            table.GetRow(1).GetCell(1).SetText(bean.KilometerMark);
            table.GetRow(2).GetCell(0).SetText("下行侧向");
            table.GetRow(2).GetCell(1).SetText(bean.SideDirection);
            table.GetRow(3).GetCell(0).SetText("距线路中心距离(m)");
            table.GetRow(4).GetCell(0).SetText("经度");
            table.GetRow(4).GetCell(1).SetText(bean.Longitude);
            table.GetRow(5).GetCell(0).SetText("纬度");
            table.GetRow(5).GetCell(1).SetText(bean.Latitude);
            table.GetRow(6).GetCell(0).SetText("杆塔类型");
            table.GetRow(6).GetCell(1).SetText(bean.TowerType);
            table.GetRow(7).GetCell(0).SetText("杆塔高度");
            table.GetRow(7).GetCell(1).SetText(bean.TowerHeight);
            table.GetRow(8).GetCell(0).SetText("天线1方向角");
            table.GetRow(8).GetCell(1).SetText(bean.AntennaDirection1);
            table.GetRow(9).GetCell(0).SetText("天线2方向角");
            table.GetRow(9).GetCell(1).SetText(bean.AntennaDirection2);
            table.GetRow(10).GetCell(0).SetText("天线3方向角");
            table.GetRow(10).GetCell(1).SetText(bean.AntennaDirection3);
            table.GetRow(11).GetCell(0).SetText("天线4方向角");
            table.GetRow(11).GetCell(1).SetText(bean.AntennaDirection4);
            CT_TcPr m_Pr = table.GetRow(2).GetCell(1).GetCTTc().AddNewTcPr();

            m_Pr.tcW      = new CT_TblWidth();
            m_Pr.tcW.w    = "3500";
            m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度

            XWPFTableRow  m_Row = table.InsertNewTableRow(0);
            XWPFTableCell cell  = m_Row.CreateCell();
            CT_Tc         cttc  = cell.GetCTTc();
            CT_TcPr       ctPr  = cttc.AddNewTcPr();

            ctPr.gridSpan     = new CT_DecimalNumber();
            ctPr.gridSpan.val = "2";
            cttc.GetPList()[0].AddNewR().AddNewT().Value = "SITE 【序号】";

            word_insert_space(1, m_Docx, 100);
            word_insert_text(m_Docx, "宋体", 11, "SITE 【序号】勘站照片");
            word_insert_text(m_Docx, "宋体", 11, "(3-10张照片)");

            word_insert_space(1, m_Docx, 100);
        }
        public static void ExportDocumentWithDataTable(XWPFDocument docx, DocumentSetting setting, WordTable wordTable)
        {
            if (wordTable == null || wordTable.Rows == null || wordTable.Rows.Count == 0)
            {
                return;
            }

            CT_P p = docx.Document.body.AddNewP();

            p.AddNewPPr().AddNewJc().val = ST_Jc.center;

            var Rows = wordTable.Rows;

            XWPFTable table = docx.CreateTable(1, wordTable.ColumnCount);

            table.RemoveRow(0);//remove first blank row
            table.Width = wordTable.Width;

            for (var i = 0; i < Rows.Count; i++)
            {
                var row = Rows[i];
                if (row == null || row.Count == 0)
                {
                    continue;
                }

                CT_Row       nr = new CT_Row();
                XWPFTableRow mr = new XWPFTableRow(nr, table);
                table.AddRow(mr);

                for (var j = 0; j < row.Count; j++)
                {
                    var cell = row[j];

                    var c1 = mr.CreateCell();
                    var ct = c1.GetCTTc();
                    var cp = ct.AddNewTcPr();
                    cp.gridSpan     = new CT_DecimalNumber();
                    cp.gridSpan.val = Convert.ToString(cell.MergeColumnNumber);

                    var tblW = cp.AddNewTcW();
                    tblW.type = ST_TblWidth.dxa;
                    tblW.w    = cell.Width.ToString();

                    c1.SetParagraph(SetCellText(docx, table, cell));

                    c1.SetBorderTop(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                    c1.SetBorderRight(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                    c1.SetBorderLeft(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                    c1.SetBorderBottom(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                }
            }
        }
Exemple #17
0
        private XWPFDocument InsertTable(XWPFDocument doc, Table t)
        {
            var maxColCount = t.Rows.Max(x => x.Cells.Count);

            if (t == null)
            {
                return(doc);
            }

            var table = doc.CreateTable();

            table.Width = t.Width;

            int index = 0;

            t.Rows?.ForEach(r =>
            {
                XWPFTableRow tableRow = index == 0 ? table.GetRow(0) : table.CreateRow();

                for (int i = 0; i < r.Cells.Count; i++)
                {
                    var cell     = r.Cells[i];
                    var xwpfCell = i == 0 ? tableRow.GetCell(0) : tableRow.AddNewTableCell();
                    foreach (var para in cell.Paragraphs)
                    {
                        xwpfCell.AddParagraph().Set(para);
                    }

                    if (!string.IsNullOrWhiteSpace(cell.Color))
                    {
                        tableRow.GetCell(i).SetColor(cell.Color);
                    }
                }

                //补全单元格,并合并
                var rowColsCount = tableRow.GetTableICells().Count;
                if (rowColsCount < maxColCount)
                {
                    for (int i = rowColsCount - 1; i < maxColCount; i++)
                    {
                        tableRow.CreateCell();
                    }
                    tableRow.MergeCells(rowColsCount - 1, maxColCount);
                }

                index++;
            });

            return(doc);
        }
Exemple #18
0
        public void Test()
        {
            var document = new XWPFDocument();
            var p        = document.CreateParagraph();
            var r0       = p.CreateRun();

            r0.SetText("FileFormat");
            r0.AddCarriageReturn();
            //r0.SetText("test");
            //r0.AddBreak(BreakClear.ALL);

            var table = document.CreateTable(rows: 5, cols: 2);
            // Row and cell start at index 0

            var col       = table.GetRow(0).GetCell(0);
            var paragraph = col.AddParagraph();
            var run       = paragraph.CreateRun();

            run.IsBold     = true;
            run.FontFamily = "Courier";
            run.FontSize   = 12;
            run.SetText("ชื่อและที่อยู่ผู้ส่ง");
            run.AddBreak();

            var run2 = paragraph.CreateRun();

            run2.IsBold     = true;
            run2.FontFamily = "Courier";
            run2.FontSize   = 12;
            run2.SetText("ชื่อและที่อยู่ผู้ส่ง");
            run2.AddBreak();

//            //run.SetText(
//@"
//นายธีรานิตย์ พงค์ทองเมือง โทร 089 668 6365
//เลขที่ 1055/1170
//อาคาร State tower (RCK)
//ถ.สีลม แขวงสีลม
//เขตบางรัก กทม.

//10500"
//);
//            //run.SetUnderline(UnderlinePatterns.DotDotDash);
            //run.TextPosition = 100;

            var outputFile = new FileStream("table.docx", FileMode.Create);

            document.Write(outputFile);
            outputFile.Close();
        }
Exemple #19
0
        private static void word_init(XWPFDocument m_Docx, String prjName)
        {
            //设置页面 将页面设置为A4 纵向
            //参考网站  http://www.aiuxian.com/article/p-1970779.html
            CT_SectPr m_SectPr = new CT_SectPr();

            m_SectPr.pgSz.w             = (ulong)11906;
            m_SectPr.pgSz.h             = (ulong)16838;
            m_Docx.Document.body.sectPr = m_SectPr;

            //第一页 封面
            word_insert_space(5, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, prjName);
            word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统");
            word_insert_text(m_Docx, "宋体", 22, "现场勘查报告");
            word_insert_space(7, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, DateTime.Now.ToString("yyyy年MM月dd日"));
            word_insert_space(7, m_Docx);


            //创建表并获取该表
            XWPFTable table = m_Docx.CreateTable(4, 2);
            CT_Tbl    ctbl  = m_Docx.Document.body.GetTblArray()[0];

            //表居中
            ctbl.AddNewTblPr().jc = new CT_Jc();
            ctbl.AddNewTblPr().jc.val = ST_Jc.center;
            //表宽度为8000
            ctbl.AddNewTblPr().AddNewTblW().w    = "8000";
            ctbl.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa;

            //列宽设置
            CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();

            m_Pr.tcW      = new CT_TblWidth();
            m_Pr.tcW.w    = "3000";
            m_Pr.tcW.type = ST_TblWidth.dxa;

            //行高设置


            //设置表中文本
            table.GetRow(0).GetCell(0).SetText("项目");
            table.GetRow(1).GetCell(0).SetText("勘察日期");
            table.GetRow(2).GetCell(0).SetText("现场勘查人员");
            table.GetRow(3).GetCell(0).SetText("报告修正人员");

            word_insert_space(4, m_Docx);
        }
Exemple #20
0
        public void Bug57495_getParagraphArrayInTableCell()
        {
            XWPFDocument doc = new XWPFDocument();
            //let's create a table for the test
            XWPFTable table = doc.CreateTable(2, 2);

            Assert.IsNotNull(table);
            XWPFParagraph p = table.GetRow(0).GetCell(0).GetParagraphArray(0);

            Assert.IsNotNull(p);
            //let's check also that returns the correct paragraph
            XWPFParagraph same = table.GetRow(0).GetCell(0).Paragraphs[0];

            Assert.AreEqual(p, same);
        }
Exemple #21
0
        protected XWPFTable InsertTable(XWPFDocument document, int rowCount, int colCount)
        {
            XWPFTable table1     = document.CreateTable(rowCount, colCount);
            var       tblLayout1 = table1.GetCTTbl().tblPr.AddNewTblLayout();

            tblLayout1.type = ST_TblLayoutType.@fixed;

            const int width = 10000;

            for (int i = 0; i < colCount; i++)
            {
                table1.SetColumnWidth(i, (ulong)(width / colCount));
            }

            return(table1);
        }
Exemple #22
0
        /// <summary>
        /// 生成模板文件
        /// </summary>
        /// <param name="m_Docx">根文档</param>
        private static void word_init(XWPFDocument m_Docx)
        {
            //1‘=1440twip=25.4mm=72pt(磅point)=96px(像素pixel)
            //1px(像素pixel)=0.75pt(磅point)
            // A4:W=11906 twip=8.269''=210mm,h=16838twip=11.693''=297mm
            //A5:W=8390 twip=5.827''=148mm,h=11906 twip=8.269''=210mm
            //A6:W=5953 twip=4.134''=105mm,h=8390twip=5.827''=1148mm
            //16k195mmX270mm:
            //16k184mmX260mm:
            //16k197mmX273mm:
            CT_SectPr m_SectPr = new CT_SectPr();

            //页面设置A4纵向
            m_SectPr.pgSz.w             = (ulong)11906;
            m_SectPr.pgSz.h             = (ulong)16838;
            m_Docx.Document.body.sectPr = m_SectPr;
            //第一页
            word_insert_space(4, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, "【项目名称】");
            word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统");
            word_insert_text(m_Docx, "宋体", 22, "现场勘查报告");

            word_insert_space(8, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, "【日期】");

            word_insert_space(7, m_Docx);
            //第二页
            //表1
            XWPFTable table  = m_Docx.CreateTable(4, 2);
            CT_Tbl    ctbl   = m_Docx.Document.body.GetTblArray()[0];
            CT_TblPr  ctblpr = ctbl.AddNewTblPr();

            ctblpr.jc     = new CT_Jc();
            ctblpr.jc.val = ST_Jc.center;
            table.Width   = 4000;
            table.GetRow(0).GetCell(0).SetText("项目");
            table.GetRow(1).GetCell(0).SetText("日期");
            table.GetRow(2).GetCell(0).SetText("现场勘查人员");
            table.GetRow(3).GetCell(0).SetText("报告编制人员");
            CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();

            m_Pr.tcW      = new CT_TblWidth();
            m_Pr.tcW.w    = "4000";
            m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度
            word_insert_space(2, m_Docx);
            word_insert_text(m_Docx, "宋体", 12, "基站勘察表");
        }
        private XWPFDocument _addSingleInvoiceAccountsTable(XWPFDocument document, Invoice invoice)
        {
            var table = document.CreateTable(1, 4);

            table.Width = _pageWidth;

            table.SetColumnWidth(0, 5);
            table.SetColumnWidth(1, 6);
            table.SetColumnWidth(2, 3);
            table.SetColumnWidth(3, 3);

            table.SetInsideHBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");
            table.SetInsideVBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");
            table.SetTopBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");
            table.SetLeftBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");
            table.SetRightBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");
            table.SetBottomBorder(XWPFTable.XWPFBorderType.NIL, 0, 0, "white");

            var header = table.GetRow(0);

            _createTableCell(header.GetCell(0), "Project or Program", ParagraphAlignment.LEFT, true);
            _createTableCell(header.GetCell(1), "City Account Code", ParagraphAlignment.LEFT, true);
            _createTableCell(header.GetCell(2), "PPRTA Account Code", ParagraphAlignment.LEFT, true);
            _createTableCell(header.GetCell(3), "Amount", ParagraphAlignment.RIGHT, true);

            decimal total = 0;

            foreach (var account in invoice.AccountTotals)
            {
                foreach (var cityExpense in account.CityExpenses)
                {
                    var row = table.CreateRow();
                    _createTableCell(row.GetCell(0), account.Account.ProjectDescription, ParagraphAlignment.LEFT, false);
                    _createTableCell(row.GetCell(1), $"{cityExpense.CityAccount.CityAccountNumber}-{account.Account.FundNumber}-{account.Account.DeptartmentNumber}-{account.Account.ProjectNumber}", ParagraphAlignment.LEFT, false);
                    _createTableCell(row.GetCell(2), $"{account.Account.AccountPrefix}-{account.Account.AccountNumber}", ParagraphAlignment.LEFT, false);
                    _createTableCell(row.GetCell(3), string.Format("{0:C}", cityExpense.Expense), ParagraphAlignment.RIGHT, false);
                    total += cityExpense.Expense;
                }
            }

            var totalRow = table.CreateRow();

            _createTableCell(totalRow.GetCell(2), "Total", ParagraphAlignment.RIGHT, true);
            _createTableCell(totalRow.GetCell(3), string.Format("{0:C}", total), ParagraphAlignment.RIGHT, true);

            return(document);
        }
Exemple #24
0
        public void Execute(object parameter)
        {
            List <ReportData> reportData = (List <ReportData>)parameter;
            XWPFDocument      doc        = new XWPFDocument();

            XWPFParagraph paragraph = doc.CreateParagraph();

            paragraph.Alignment = ParagraphAlignment.CENTER;
            XWPFRun r0 = paragraph.CreateRun();

            r0.SetText($"Отчёт об успешности прохождения тестов: {reportData[0].firstDate.ToString("dd/MM/yyyy")} - {reportData[0].secondDate.ToString("dd/MM/yyyy")}");

            XWPFTable table = doc.CreateTable(reportData.Count + 1, 4);

            table.GetRow(0).GetCell(0).SetText("Должность");
            table.GetRow(0).GetCell(1).SetText("Выполнили на ⩾ 50% (чел.)");
            table.GetRow(0).GetCell(2).SetText("Выполнили на ⩾ 75% (чел.)");
            table.GetRow(0).GetCell(3).SetText("Выполнили на 100% (чел.)");

            for (int i = 1; i < 3; i++)
            {
                table.GetRow(i).GetCell(0).SetText(reportData[i - 1].Job.Name);
            }
            for (int i = 1; i < 3; i++)
            {
                table.GetRow(i).GetCell(1).SetText(reportData[i - 1].minimalPercent.ToString());
            }
            for (int i = 1; i < 3; i++)
            {
                table.GetRow(i).GetCell(2).SetText(reportData[i - 1].highPercent.ToString());
            }
            for (int i = 1; i < 3; i++)
            {
                table.GetRow(i).GetCell(3).SetText(reportData[i - 1].fullPercent.ToString());
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "Документ Word (*.docx)|*.docx";
            if (saveFileDialog.ShowDialog() == true)
            {
                FileStream Out = new FileStream(saveFileDialog.FileName, FileMode.Create);
                doc.Write(Out);
                Out.Close();
            }
        }
Exemple #25
0
        public void Bug57495_getTableArrayInDoc()
        {
            XWPFDocument doc = new XWPFDocument();

            //let's create a few tables for the test
            for (int i = 0; i < 3; i++)
            {
                doc.CreateTable(2, 2);
            }
            XWPFTable table = doc.GetTableArray(0);

            Assert.IsNotNull(table);
            //let's check also that returns the correct table
            XWPFTable same = doc.Tables[0];

            Assert.AreEqual(table, same);
        }
        XWPFTable _table;//创建6行4列表,表头

        protected override void BuildHeader()
        {
            XWPFParagraph p0 = _docx.CreateParagraph();

            p0.Alignment = ParagraphAlignment.CENTER;
            XWPFRun r0 = p0.CreateRun();

            r0.SetText("一级安全评估报告");
            r0.SetTextPosition(1);
            r0.FontFamily = "宋体";
            r0.FontSize   = 24;
            r0.IsBold     = true;
            _table        = _docx.CreateTable(2, 5);
            SetReportTableHeaderValue(_table);
            //给报告表头赋值
            SetReportHeaderValue(_source, _table);
        }
        public static bool Create(string fileName, Dictionary <string, Tuple <string, List <Fields> > > dic)
        {
            var doc = new XWPFDocument();//创建document对象

            foreach (var item in dic)
            {
                var p1  = doc.CreateParagraph();//创建段落对象
                var tbl = p1.CreateRun();
                tbl.FontSize = 15;
                tbl.SetBold(true);
                tbl.SetText(item.Key);

                var       fields = item.Value.Item2;
                XWPFTable table  = doc.CreateTable(fields.Count + 1, 5);
                table.Width = 1000 * 5;
                table.SetColumnWidth(0, 100);
                table.SetColumnWidth(1, 100);
                table.SetColumnWidth(2, 100);
                table.SetColumnWidth(3, 300);
                table.SetColumnWidth(4, 100);

                //表头
                table.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, table, "名称"));
                table.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc, table, "类型"));
                table.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc, table, "必须"));
                table.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc, table, "描述"));
                table.GetRow(0).GetCell(4).SetParagraph(SetCellText(doc, table, "示例"));

                for (int i = 0; i < fields.Count; i++)
                {
                    var index = i + 1;
                    table.GetRow(index).GetCell(0).SetParagraph(SetCellText(doc, table, fields[i].Name));
                    table.GetRow(index).GetCell(1).SetParagraph(SetCellText(doc, table, fields[i].NetType));
                    table.GetRow(index).GetCell(2).SetParagraph(SetCellText(doc, table, fields[i].IsNull ? "否" : "是"));
                    table.GetRow(index).GetCell(3).SetParagraph(SetCellText(doc, table, fields[i].Note));
                    table.GetRow(index).GetCell(4).SetParagraph(SetCellText(doc, table, ""));
                }
            }
            using (FileStream sw = File.Create(fileName))
            {
                doc.Write(sw);
                sw.Close();
            }
            return(true);
        }
Exemple #28
0
        private static void AddTable(XWPFDocument doc)
        {
            // XWPFDocument doc = new XWPFDocument();
            //创建表格-提前创建好表格后填数
            XWPFTable tableContent = doc.CreateTable(4, 5);//4行5列

            tableContent.Width = 1000 * 5;
            tableContent.SetColumnWidth(0, 1000);/* 设置列宽 */
            tableContent.SetColumnWidth(1, 1500);
            tableContent.SetColumnWidth(2, 1500);
            tableContent.SetColumnWidth(3, 1000);

            tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "地点"));
            tableContent.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc, tableContent, "日期"));
            tableContent.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "男性"));
            tableContent.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc, tableContent, "女性"));
            tableContent.GetRow(0).GetCell(4).SetParagraph(SetCellText(doc, tableContent, "合计"));

            //测试数据格式
            List <ArrayList> list = new List <ArrayList>()
            {
                new ArrayList()
                {
                    "航天桥", "-", "0", "0", "0"
                },
                new ArrayList()
                {
                    "马甸", "-", "0", "0", "0"
                },
                new ArrayList()
                {
                    "洋桥", "04月16日 - 05月31日", "0", "0", "0"
                }
            };

            //  List<ArrayList> list = Common.PubVars.listTable;
            for (int i = 0; i < list.Count; i++)//有3个数组
            {
                ArrayList ls = list[i];
                for (int j = 0; j < ls.Count; j++)
                {
                    tableContent.GetRow(i + 1).GetCell(j).SetParagraph(SetCellText(doc, tableContent, ls[j].ToString()));
                }
            }
        }
Exemple #29
0
        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);
            }
        }
Exemple #30
0
        public Word CreateFromMasterTable <T>(string templateUrl, IEnumerable <T> datas) where T : class, new()
        {
            var template = NPOIHelper.GetXWPFDocument(templateUrl);

            var result = new Word()
            {
                Type = SolutionEnum.NPOI
            };

            var tables = template.GetTablesEnumerator();

            if (tables == null)
            {
                result.WordBytes = template.ToBytes();
                return(result);
            }

            var masterTables = new List <XWPFTable>();

            while (tables.MoveNext())
            {
                masterTables.Add(tables.Current);
            }

            var doc = new XWPFDocument();

            foreach (var data in datas)
            {
                foreach (var masterTable in masterTables)
                {
                    var cloneTable = doc.CreateTable();
                    NPOIHelper.CopyTable(masterTable, cloneTable);
                }

                ReplacePlaceholders(doc, data);
            }

            result.WordBytes = doc.ToBytes();

            return(result);
        }