コード例 #1
0
        public static void ExportDocumentWithDataTable(XWPFDocument docx, DocumentSetting setting, WordTable wordTable)
        {
            if (wordTable == null || wordTable.Rows == null || wordTable.Rows.Count == 0)
            {
                return;
            }

            CT_P p = docx.Document.body.AddNewP();

            p.AddNewPPr().AddNewJc().val = ST_Jc.center;

            var Rows = wordTable.Rows;

            XWPFTable table = docx.CreateTable(1, wordTable.ColumnCount);

            table.RemoveRow(0);//remove first blank row
            table.Width = wordTable.Width;

            for (var i = 0; i < Rows.Count; i++)
            {
                var row = Rows[i];
                if (row == null || row.Count == 0)
                {
                    continue;
                }

                CT_Row       nr = new CT_Row();
                XWPFTableRow mr = new XWPFTableRow(nr, table);
                table.AddRow(mr);

                for (var j = 0; j < row.Count; j++)
                {
                    var cell = row[j];

                    var c1 = mr.CreateCell();
                    var ct = c1.GetCTTc();
                    var cp = ct.AddNewTcPr();
                    cp.gridSpan     = new CT_DecimalNumber();
                    cp.gridSpan.val = Convert.ToString(cell.MergeColumnNumber);

                    var tblW = cp.AddNewTcW();
                    tblW.type = ST_TblWidth.dxa;
                    tblW.w    = cell.Width.ToString();

                    c1.SetParagraph(SetCellText(docx, table, cell));

                    c1.SetBorderTop(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                    c1.SetBorderRight(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                    c1.SetBorderLeft(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                    c1.SetBorderBottom(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFF");
                }
            }
        }
コード例 #2
0
        public void Generate()
        {
            GlobalHelper.CreateFolderIfNotExists(OutputFolder);

            FileInfo newFile = new FileInfo(DocumentFullName);

            if (newFile.Exists)
            {
                newFile.Delete();
                newFile = new FileInfo(DocumentFullName);
            }

            DocumentSetting setting = new DocumentSetting()
            {
                PaperType    = PaperType.A4_V,
                PaperMarType = PaperMarType.MarType1,
                SavePath     = DocumentFullName,
                Footer       = DocumentName
            };

            var wordTables = new List <WordTable>();

            foreach (var ws in DocumentDataSets)
            {
                if (ws.WorkSheetType == WorkSheetTypeEnum.Summary)
                {
                    var wordTable = WordTableGenerator.GetSummaryWordTable(ws);
                    wordTables.Add(wordTable);
                }
                else if (ws.WorkSheetType == WorkSheetTypeEnum.ITD)
                {
                    var wordTable = WordTableGenerator.GetITDWordTable(ws);
                    wordTables.Add(wordTable);
                }
                else if (ws.WorkSheetType == WorkSheetTypeEnum.UnderwritingYear)
                {
                    var wordTable = WordTableGenerator.GetUWYearWordTable(ws);
                    wordTables.Add(wordTable);
                }
            }


            WordHelper.ExportDocumentWithDataTables(setting, wordTables);
        }
コード例 #3
0
        public static void ExportDocumentWithDataTables(DocumentSetting setting, List <WordTable> wordTables)
        {
            XWPFDocument docx = new XWPFDocument();
            MemoryStream ms   = new MemoryStream();

            #region Page Setting
            //Set Document
            docx.Document.body.sectPr = new CT_SectPr();
            CT_SectPr setPr = docx.Document.body.sectPr;
            //get page size
            Tuple <int, int> size = GetPaperSize(setting.PaperType);
            setPr.pgSz.w = (ulong)size.Item1;
            setPr.pgSz.h = (ulong)size.Item2;

            var mar = GetPaperMar(setting.PaperMarType);

            setPr.pgMar.left   = (ulong)mar.Item3;
            setPr.pgMar.right  = (ulong)mar.Item4;
            setPr.pgMar.top    = mar.Item1.ToString();
            setPr.pgMar.bottom = mar.Item2.ToString();

            #region Footer
            CT_Ftr m_ftr = new CT_Ftr();

            m_ftr.AddNewP().AddNewR().AddNewT().Value = setting.Footer;

            XWPFRelation Frelation = XWPFRelation.FOOTER;
            XWPFFooter   m_f       = (XWPFFooter)docx.CreateRelationship(Frelation, XWPFFactory.GetInstance(), docx.FooterList.Count + 1);

            m_f.SetHeaderFooter(m_ftr);
            CT_HdrFtrRef m_HdrFtr1 = setPr.AddNewFooterReference();

            m_HdrFtr1.type = ST_HdrFtr.@default;
            m_HdrFtr1.id   = m_f.GetPackageRelationship().Id;
            #endregion

            #endregion Page Setting

            var i = 0;
            foreach (var wordTable in wordTables)
            {
                i++;
                ExportDocumentWithDataTable(docx, setting, wordTable);

                if (i < wordTables.Count)
                {
                    XWPFParagraph m_xp = docx.CreateParagraph();

                    m_xp.CreateRun().AddBreak();
                }
            }

            docx.Write(ms);

            using (FileStream fs = new FileStream(setting.SavePath, FileMode.Create, FileAccess.Write))
            {
                byte[] data = ms.ToArray();
                fs.Write(data, 0, data.Length);
                fs.Flush();
            }
            ms.Close();
        }