示例#1
0
        private static void AdjustGridRowCount(XWPFTable table, GridDefine gridDefine, int newRowCount)
        {
            for (int i = 0; i < newRowCount; i++)
            {
                XWPFTableRow row         = table.InsertNewTableRow(gridDefine.StartRowIndex);
                XWPFTableRow templateRow = gridDefine.Templates[i == newRowCount - 1 ? gridDefine.Templates.Count - 1 : 0];
                WordGenerator.CopyRow(templateRow, row);
            }

            foreach (XWPFTableRow templateRow in gridDefine.Templates)
            {
                table.RemoveRow(table.Rows.IndexOf(templateRow));
            }
        }
示例#2
0
        private static GridDefine ParseGridDefine(XWPFTable table)
        {
            GridDefine gridDefine = null;

            for (int i = 0; i < table.Rows.Count; i++)
            {
                XWPFTableRow row        = table.Rows[i];
                MergeField   mergeField = WordGenerator.GetFirstArrayMergeField(row);
                if (mergeField == null)
                {
                    if (gridDefine == null)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    if (gridDefine == null)
                    {
                        gridDefine               = new GridDefine();
                        gridDefine.TableName     = mergeField.TableName;
                        gridDefine.StartRowIndex = i;
                        gridDefine.EndRowIndex   = i;
                        gridDefine.Templates.Add(row);
                    }
                    else
                    {
                        gridDefine.EndRowIndex = i;
                        gridDefine.Templates.Add(row);
                    }
                }
            }

            return(gridDefine);
        }
示例#3
0
 private static void FillGrid(XWPFTable doctable, GridDefine gridDefine, DataTable table)
 {
     for (int i = 0; i < table.Rows.Count; i++)
     {
         DataRow      row    = table.Rows[i];
         XWPFTableRow docrow = doctable.GetRow(gridDefine.StartRowIndex + i);
         foreach (XWPFTableCell cell in docrow.GetTableCells())
         {
             foreach (XWPFParagraph ph in cell.Paragraphs)
             {
                 foreach (XWPFRun run in ph.Runs)
                 {
                     MergeField field = WordGenerator.ParesMergeField(run);
                     if (field != null && table.Columns.Contains(field.ColumnName))
                     {
                         WordGenerator.Fill(run, field, row[field.ColumnName], row);
                     }
                 }
             }
         }
     }
 }
示例#4
0
 private static DataTable TryGetGridData(DataSet dataset, GridDefine gridDefine)
 {
     return(dataset.Tables[gridDefine.TableName]);
 }
示例#5
0
        public static void Fill(XWPFDocument doc, DataSet dataset)
        {
            //为table添加RowNum/No列
            foreach (DataTable table in dataset.Tables)
            {
                if (!table.Columns.Contains("RowNum"))
                {
                    DataColumn rowNumColumn = new DataColumn("RowNum", typeof(int));
                    table.Columns.Add(rowNumColumn);

                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        table.Rows[i]["RowNum"] = i + 1;
                    }
                }

                if (!table.Columns.Contains("No"))
                {
                    DataColumn rowNumColumn = new DataColumn("No", typeof(int));
                    table.Columns.Add(rowNumColumn);

                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        table.Rows[i]["No"] = i + 1;
                    }
                }
            }

            //替换grid
            foreach (XWPFTable table in GetAllTables(doc))
            {
                GridDefine gridDefine = WordGenerator.ParseGridDefine(table);
                if (gridDefine != null)
                {
                    DataTable gridTable = WordGenerator.TryGetGridData(dataset, gridDefine);
                    if (gridTable != null)
                    {
                        WordGenerator.AdjustGridRowCount(table, gridDefine, gridTable.Rows.Count);
                        WordGenerator.FillGrid(table, gridDefine, gridTable);
                    }
                }
            }

            //替换表格中的全局变量
            foreach (XWPFTable table in GetAllTables(doc))
            {
                foreach (XWPFTableRow row in table.Rows)
                {
                    foreach (XWPFTableCell cell in row.GetTableCells())
                    {
                        //不能调用以下代码,SOP文档,作者,状态栏目输出空
                        //MergeField(cell.Paragraphs, dataset);

                        if (!HasMergeField(cell.Paragraphs))
                        {
                            continue;
                        }

                        int count = cell.Paragraphs.Count;

                        //复制段落
                        for (int i = 0; i < count; i++)
                        {
                            XWPFParagraph ph = cell.AddParagraph();
                            WordGenerator.CopyParagraph(cell.Paragraphs[i], ph);
                        }

                        //移除原段落
                        for (int i = 0; i < count; i++)
                        {
                            cell.RemoveParagraph(0);
                        }

                        //替换
                        foreach (XWPFParagraph ph in cell.Paragraphs)
                        {
                            foreach (XWPFRun run in ph.Runs)
                            {
                                WordGenerator.MergeField(run, dataset);
                            }
                        }
                    }
                }
            }

            //替换循环段落
            List <LoopDefine> loops = ParseLoops(doc);

            foreach (LoopDefine loop in loops)
            {
                DataTable table = dataset.Tables[loop.TableName];
                if (table != null)
                {
                    FillLoop(doc, loop, table);
                }
            }

            //替换段落中的全局变量
            List <XWPFParagraph> phs = GetAllParagraphs(doc);

            MergeField(phs, dataset);
        }