Example #1
0
        public void TestSetGetVertAlignment()
        {
            // instantiate the following classes so they'll Get picked up by
            // the XmlBean process and Added to the jar file. they are required
            // for the following XWPFTableCell methods.
            CT_Shd ctShd = new CT_Shd();

            Assert.IsNotNull(ctShd);
            CT_VerticalJc ctVjc = new CT_VerticalJc();

            Assert.IsNotNull(ctVjc);
            ST_Shd stShd = ST_Shd.nil;

            Assert.IsNotNull(stShd);
            ST_VerticalJc stVjc = ST_VerticalJc.top;

            Assert.IsNotNull(stVjc);

            // 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.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTH);
            XWPFTableCell.XWPFVertAlign al = cell.GetVerticalAlignment().Value;
            Assert.AreEqual(XWPFTableCell.XWPFVertAlign.BOTH, al);
        }
Example #2
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();
        }
Example #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);
            }
        }
Example #4
0
        /// <summary>
        /// 应用表格模板
        /// </summary>
        /// <param name="tableContent"></param>
        /// <param name="tableTemplate"></param>
        /// <param name="colNames"></param>
        private void ApplyToTableTemplate(XWPFTable tableContent, TableTemplateModel tableTemplate, Dictionary <int, string> colNames)
        {
            int startRowNum = tableTemplate.StartRowNumber;

            for (var rowIndex = 0; rowIndex < tableTemplate.Value.Rows.Count; rowIndex++)
            {
                int          documentRowIndex = rowIndex + startRowNum;
                XWPFTableRow row       = rowIndex == 0 ? tableContent.GetRow(documentRowIndex) : tableContent.InsertNewTableRow(documentRowIndex - 1);
                int          cellCount = row.GetTableCells().Count;
                int          count     = colNames.Max(m => m.Key);
                for (int i = cellCount - 1; i < count; i++)
                {
                    row.CreateCell();
                }
                foreach (KeyValuePair <int, string> colName in colNames)
                {
                    string[]      colValues = colName.Value.Split(',');
                    string        value     = tableTemplate.Value.Rows[rowIndex][colValues[0]].ToString();
                    XWPFParagraph paragraph = GetCellContent(rowIndex, colName.Key, tableContent, value, tableTemplate.OnSetCellText);
                    XWPFTableCell cell      = row.GetCell(colName.Key);
                    cell.SetParagraph(paragraph);
                    for (var i = 1; i < colValues.Length; i++)
                    {
                        ApplyCommand(colValues[i], colName.Key, row);
                    }
                }
            }
        }
Example #5
0
        public static XWPFParagraph CreateParagraph(this XWPFTableCell aCell)
        {
            XWPFParagraph pIO = aCell.Paragraphs.Count > 0 ? aCell.Paragraphs[0] : aCell.AddParagraph();

            pIO.Alignment = ParagraphAlignment.LEFT;
            return(pIO);
        }
Example #6
0
        /// <summary>
        /// 合并行、垂直合并列单元格
        /// </summary>
        /// <param name="table"></param>
        /// <param name="fromRow"></param>
        /// <param name="toRow"></param>
        /// <param name="colIndex"></param>
        public void MYMergeRows(XWPFTable table, int fromRow, int toRow, int colIndex)
        {
            for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
            {
                XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(colIndex);
                rowcell.SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                CT_Tc   cttc   = rowcell.GetCTTc();
                CT_TcPr ctTcPr = cttc.tcPr;
                if (ctTcPr == null)
                {
                    ctTcPr = cttc.AddNewTcPr();
                }

                if (rowIndex == fromRow)
                {
                    // The first merged cell is set with RESTART merge value
                    ctTcPr.AddNewVMerge().val = ST_Merge.restart;
                }
                else
                {
                    // Cells which join (merge) the first one, are set with CONTINUE
                    ctTcPr.AddNewVMerge().val = ST_Merge.@continue; //继续合并行
                }
                ctTcPr.AddNewVAlign().val = ST_VerticalJc.center;   //垂直
            }
        }
Example #7
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();
        }
Example #8
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();
        }
        public static XWPFParagraph AddParagraph(this XWPFTableCell cell, StyleConfig styles)
        {
            XWPFParagraph para = cell.AddParagraph();

            para.Alignment = (styles.isAlignRight ? ParagraphAlignment.RIGHT : ParagraphAlignment.LEFT);
            return(para);
        }
Example #10
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);
        }
Example #11
0
        /// <summary>
        /// 单元格居中对齐
        /// </summary>
        /// <param name="cell"></param>
        void SetAlign(XWPFTableCell cell)
        {
            var cttc = cell.GetCTTc();
            var ctPr = cttc.AddNewTcPr();

            ctPr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
        }
Example #12
0
 /// <summary>
 /// 设置表格中字体颜色
 /// </summary>
 /// <param name="cell"></param>
 /// <param name="strRRGGBB"></param>
 /// <returns></returns>
 public static XWPFTableCell SetCellFontColor(this XWPFTableCell cell, string strRRGGBB)
 {
     if (cell != null && cell.Paragraphs.Any() && cell.Paragraphs[0].Runs.Any())
     {
         cell.Paragraphs[0].Runs[0].SetColor(strRRGGBB);
     }
     return(cell);
 }
Example #13
0
        private void AddCell(XWPFTableCell cell, string text)
        {
            XWPFParagraph paragraph = cell.AddParagraph();
            XWPFRun       run       = paragraph.CreateRun();

            run.SetText(text);
            run.FontFamily = "Times New Roman";
            run.FontSize   = 12;
        }
Example #14
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());
                            }
                        }
                    }
                }
            }
        }
Example #15
0
        private static string GetAllTextForCell(XWPFTableCell cell)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var run in cell.Paragraphs.SelectMany(paragraph => paragraph.Runs))
            {
                sb.Append(run.Text);
            }
            return(sb.ToString());
        }
Example #16
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);
        }
    private void setCell(XWPFTableCell cell, string cellText)
    {
        XWPFParagraph pIO = cell.AddParagraph();

        pIO.Alignment = ParagraphAlignment.CENTER;
        XWPFRun rIO = pIO.CreateRun();

        rIO.FontFamily = "宋体";
        rIO.FontSize   = 12;
        rIO.SetText(cellText);
    }
Example #18
0
        private static void CreateConverText(XWPFTableCell cell, string text, int fontSize)
        {
            cell.RemoveParagraph(0);
            XWPFParagraph paragraph = cell.AddParagraph();

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

            run.SetText(text);
            run.SetFontFamily("黑体", FontCharRange.None);
            run.FontSize = fontSize;
        }
Example #19
0
        void SetRedFontCell(XWPFTableCell cell, string value)
        {
            XWPFParagraph pIO = cell.AddParagraph();

            pIO.Alignment = ParagraphAlignment.CENTER;
            XWPFRun rIO = pIO.CreateRun();

            rIO.SetColor("red");
            rIO.SetText(value);
            SetAlign(cell);
            cell.RemoveParagraph(0);
        }
Example #20
0
        private XWPFTableCell _createTableCell(XWPFTableCell cell, string text, ParagraphAlignment alignment, bool isBold)
        {
            var paragraph = cell.Paragraphs[0];

            paragraph.Alignment         = alignment;
            paragraph.SpacingAfterLines = 0;
            var run = paragraph.CreateRun();

            run.SetText(text);
            run.IsBold = isBold;
            return(cell);
        }
Example #21
0
        private void AppendParagraph(XWPFTableCell cell, String cellText)
        {
            XWPFParagraph paragraph = cell.AddParagraph();
            XWPFRun       run       = paragraph.CreateRun();

            run.FontFamily = "宋体";
            //设置字体大小
            run.FontSize = 11;
            //字体是否加粗,这里加粗了
            run.SetBold(true);
            run.SetText(cellText);
        }
        public void getRenderedCell(Marker input, StyleConfig config, XWPFTableRow parentRow)
        {
            StyleConfig   markerStyle        = (StyleConfig)config.Clone();
            XWPFTableCell tableCellContainer = parentRow.CreateCell();
            XWPFParagraph cellContents;

            switch (input)
            {
            case THMarker tHMarker:
                markerStyle.isBold = true;
                cellContents       = tableCellContainer.AddParagraph(markerStyle);
                cellContents.SetBidi(configDocx.rightToLeft);
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, cellContents);
                }
                break;

            case THRMarker tHRMarker:
                markerStyle.isAlignRight = true;
                markerStyle.isBold       = true;
                cellContents             = tableCellContainer.AddParagraph(markerStyle);
                cellContents.SetBidi(configDocx.rightToLeft);
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, cellContents);
                }
                break;

            case TCMarker tCMarker:
                cellContents = tableCellContainer.AddParagraph(markerStyle);
                cellContents.SetBidi(configDocx.rightToLeft);
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, cellContents);
                }
                break;

            case TCRMarker tCRMarker:
                markerStyle.isAlignRight = true;
                cellContents             = tableCellContainer.AddParagraph(markerStyle);
                cellContents.SetBidi(configDocx.rightToLeft);
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, cellContents);
                }
                break;

            default:
                break;
            }
        }
Example #23
0
        static void Cell_NOPI_memo(String memo_str, String ReplaceTxt, XWPFTableCell cell)
        {
            string[] li       = memo_str.Split("\n");
            int      max_line = cell.Paragraphs.Count;

            for (int mi = 0; mi < max_line; mi++)
            {
                if (cell.Paragraphs[mi].Text.Equals(ReplaceTxt))
                {
                    cell.Paragraphs[mi].ReplaceText(ReplaceTxt, mi < li.Length ? li[mi] : "");
                }
            }
        }
Example #24
0
 private void MergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell)
 {
     for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++)
     {
         XWPFTableCell cell = table.GetRow(row).GetCell(cellIndex);
         if (cellIndex == fromCell)
         {
             cell.GetCTTc().AddNewTcPr().AddNewHMerge().val = ST_Merge.restart;
         }
         else
         {
             cell.GetCTTc().AddNewTcPr().AddNewHMerge().val = ST_Merge.@continue;
         }
     }
 }
Example #25
0
        private static void CopyRow(XWPFTableRow templateRow, XWPFTableRow row)
        {
            if (row.Height != templateRow.Height)
            {
                row.Height = templateRow.Height;
            }

            List <XWPFTableCell> templateCells = templateRow.GetTableCells();

            foreach (XWPFTableCell templateCell in templateCells)
            {
                XWPFTableCell cell = row.CreateCell();
                WordGenerator.CopyCell(templateCell, cell);
            }
        }
Example #26
0
        private void SetParagraph(XWPFTableCell cell, String cellText)
        {
            XWPFParagraph p0 = new XWPFParagraph(new CT_P(), cell); //创建段落

            p0.Alignment = ParagraphAlignment.LEFT;                 //居中显示
            XWPFRun r0 = p0.CreateRun();

            //设置字体
            r0.FontFamily = "宋体";
            //设置字体大小
            r0.FontSize = 11;
            //字体是否加粗,这里加粗了
            r0.SetBold(true);
            r0.SetText(cellText);
            cell.SetParagraph(p0);
        }
        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);
        }
Example #28
0
        private static void CreateText(XWPFTableCell cell, string text, bool isBold)
        {
            cell.RemoveParagraph(0);
            XWPFParagraph paragraph = cell.AddParagraph();

            paragraph.Alignment = ParagraphAlignment.LEFT;
            XWPFRun run = paragraph.CreateRun();

            run.SetText(text);
            run.SetFontFamily("宋体", FontCharRange.None);
            if (isBold)
            {
                run.IsBold = true;
            }
            run.FontSize = 11;
        }
Example #29
0
        public void Test54099()
        {
            XWPFDocument  doc     = new XWPFDocument();
            CT_Tbl        ctTable = new CT_Tbl();
            XWPFTable     table   = new XWPFTable(ctTable, doc);
            XWPFTableRow  tr      = table.GetRow(0);
            XWPFTableCell cell    = tr.GetCell(0);

            CT_Tc     ctTc   = cell.GetCTTc();
            CT_TcPr   tcPr   = ctTc.AddNewTcPr();
            CT_HMerge hMerge = tcPr.AddNewHMerge();

            hMerge.val = (ST_Merge.restart);

            CT_TcBorders tblBorders = tcPr.AddNewTcBorders();
            CT_VMerge    vMerge     = tcPr.AddNewVMerge();
        }
 // word跨行并单元格
 public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow)
 {
     for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
     {
         XWPFTableCell cell = table.GetRow(rowIndex).GetCell(col);
         if (rowIndex == fromRow)
         {
             // The first merged cell is set with RESTART merge value
             cell.GetCTTc().AddNewTcPr().AddNewVMerge().val = ST_Merge.restart;
         }
         else
         {
             // Cells which join (merge) the first one, are set with CONTINUE
             cell.GetCTTc().AddNewTcPr().AddNewVMerge().val = ST_Merge.@continue;
         }
     }
 }