Пример #1
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);
                    }
                }
            }
        }
Пример #2
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);
        }
Пример #3
0
        // todo

        // word 添加表格, 表格的行宽怎么确定
        public static void AddTable(XWPFDocument doc, DataTable dt, /* Dictionary<int, string> coloWidth,  */ Func <XWPFDocument, XWPFTable, string, XWPFParagraph> SetCellText)
        {
            // XWPFDocument doc = new XWPFDocument();
            //创建表格-提前创建好表格后填数
            XWPFTable table = doc.CreateTable();//4行5列

            int rowCount = dt.Rows.Count + 1;
            int colCount = dt.Columns.Count;

            XWPFTableRow titles = table.GetRow(0);

            // table.AddRow(titles);
            for (int i = 0; i < colCount; i++)
            {
                string        title = dt.Columns[i].ColumnName;
                XWPFTableCell cell  = titles.GetCell(i);
                if (cell == null)
                {
                    cell = titles.CreateCell();
                }
                cell.SetParagraph(SetCellText(doc, table, title));
                // cell.SetText(title);
            }

            for (int i = 1; i < rowCount; i++)//有3个数组
            {
                DataRow      dr       = dt.Rows[i - 1];
                XWPFTableRow tablerow = table.CreateRow();
                // XWPFTableRow tablerow = table.GetRow(i);
                for (int j = 0; j < colCount; j++)
                {
                    string content = dr[j].ToString();
                    tablerow.GetCell(j).SetParagraph(SetCellText(doc, table, content));

                    // tablerow.CreateCell().SetText(content);
                }
                //    table.AddRow(tablerow);
            }
        }