Exemplo n.º 1
0
        private static void DoAddTable(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;
                XWPFTable tb;
                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);
                        //paragraph.
                        tb = newRun.Document.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());
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void DoAddTable(XWPFTable table, XWPFParagraph paragraph, AddTableOptions TableOptions)
        {
            string          runText;
            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);
                        int j = 0;
                        foreach (DataColumn c in TableOptions.dataTable.Columns)
                        {
                            //table.GetRow(0).GetCell(j).SetColor("#AEAAAA");
                            table.GetRow(0).GetCell(j).SetText(c.ColumnName);
                            //XWPFParagraph pIO = table.GetRow(0).GetCell(j).AddParagraph();
                            //XWPFRun rIO = pIO.CreateRun();
                            //rIO.FontFamily = "宋体";
                            //rIO.FontSize = 11;
                            //rIO.IsBold = true;
                            //rIO.SetText(c.ColumnName);
                            j++;
                        }
                        j = 0;
                        for (int n = 0; n < nRows; n++)
                        {
                            var          dr = TableOptions.dataTable.Rows[n];
                            CT_Row       nr = new CT_Row();
                            XWPFTableRow mr = new XWPFTableRow(nr, table);//创建行
                            //mr.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000;//设置行高
                            //nr.AddNewTrPr().AddNewTrHeight().val = (ulong)1000;//设置行高(这两行都得有)
                            table.AddRow(mr);//将行添加到table中
                            for (int m = 0; m < nCls; m++)
                            {
                                mr.CreateCell().SetText(dr[m].ToString());
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private static List <AddTableOptions> GetTablePlaceHolderAndValueDict <T>(T wordData)
            where T : class, new()
        {
            List <AddTableOptions> placeHolderAndValueDict = new List <AddTableOptions>();
            Type type = typeof(T);

            PropertyInfo[] props = type.GetProperties();

            foreach (PropertyInfo prop in props)
            {
                if (prop.IsDefined(typeof(TablePlaceHolderAttribute)))
                {
                    AddTableOptions ao = new AddTableOptions();
                    ao.PlaceHolder = prop.GetCustomAttribute <TablePlaceHolderAttribute>().PlaceHolder;
                    ao.dataTable   = (DataTable)prop.GetValue(wordData);
                    placeHolderAndValueDict.Add(ao);
                }
            }

            return(placeHolderAndValueDict);
        }