Example #1
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 #2
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);
        }
Example #3
0
        /// <summary>
        /// Fill table row with data
        /// </summary>
        /// <param name="row">Row to write into.</param>
        /// <param name="cellData">Cell data to write into the row.</param>
        /// <returns>The row which the data has been written into.</returns>
        public static XWPFTableRow FillRow <T>(this XWPFTableRow row, IEnumerable <T> cellData)
        {
            if (row == null)
            {
                throw new ArgumentNullException(nameof(row));
            }

            int ii = 0;

            foreach (var val in cellData)
            {
                var cell = row.GetCell(ii) ?? row.AddNewTableCell();
                cell.SetText(Convert.ToString(val));

                ii++;
            }

            return(row);
        }
Example #4
0
        /// <summary>
        /// 复制一行到指定位置
        /// 样式信息也复制了,但需要完善。
        /// </summary>
        /// <param name="sourceRow"></param>
        /// <param name="table"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        public static XWPFTableRow CopyRow(XWPFTableRow sourceRow, XWPFTable table, int rowIndex)
        {
            //在表格指定位置新增一行
            var needRemove = false;

            if (table.NumberOfRows <= rowIndex)
            {
                table.CreateRow();
                needRemove = true;
            }
            XWPFTableRow targetRow = table.InsertNewTableRow(rowIndex);

            if (needRemove)
            {
                table.RemoveRow(rowIndex + 1);
            }

            //复制行属性
            targetRow.GetCTRow().trPr        = sourceRow.GetCTRow().trPr;
            List <XWPFTableCell> sourceCells = sourceRow.GetTableCells();

            if (null == sourceCells)
            {
                return(targetRow);
            }
            //复制列及其属性和内容
            foreach (var sourceCell in sourceCells)
            {
                XWPFTableCell targetCell = targetRow.AddNewTableCell();
                targetCell.RemoveParagraph(0);//新建cell会自动创建paragraph,将其删除,下面代码循环添加

                //列属性
                targetCell.GetCTTc().tcPr = sourceCell.GetCTTc().tcPr;

                //段落属性
                if (sourceCell.Paragraphs != null && sourceCell.Paragraphs.Count > 0)
                {
                    foreach (var sourcePa in sourceCell.Paragraphs)
                    {
                        if (sourcePa.Runs != null && sourcePa.Runs.Count > 0)
                        {
                            var targetPa = targetCell.AddParagraph();
                            targetPa.Alignment = sourcePa.Alignment;
                            foreach (var srcR in sourcePa.Runs)
                            {
                                XWPFRun tarR = targetPa.CreateRun();
                                tarR.SetText(srcR.Text);
                                tarR.SetTextPosition(srcR.GetTextPosition());
                                tarR.FontFamily    = srcR.FontFamily;
                                tarR.FontSize      = srcR.FontSize <= 0 ? 12 : srcR.FontSize;
                                tarR.IsBold        = srcR.IsBold;
                                tarR.IsItalic      = srcR.IsItalic;
                                tarR.IsCapitalized = srcR.IsCapitalized;
                                tarR.SetColor(srcR.GetColor());
                                tarR.SetUnderline(srcR.Underline);
                                tarR.CharacterSpacing = srcR.CharacterSpacing;
                            }
                        }
                        else
                        {
                            targetCell.SetText(sourceCell.GetText());
                        }
                    }
                }
                else
                {
                    targetCell.SetText(sourceCell.GetText());
                }
            }
            return(targetRow);
        }