Пример #1
0
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr ctSdtPr = block.AddNewSdtPr();

            ctSdtPr.AddNewId().val = "4844945";
            ctSdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = "Table of contents";
            CT_RPr   ctRpr   = block.AddNewSdtEndPr().AddNewRPr();
            CT_Fonts ctFonts = ctRpr.AddNewRFonts();

            ctFonts.asciiTheme    = ST_Theme.minorHAnsi;
            ctFonts.eastAsiaTheme = ST_Theme.minorHAnsi;
            ctFonts.hAnsiTheme    = ST_Theme.minorHAnsi;
            ctFonts.cstheme       = ST_Theme.minorBidi;
            ctRpr.AddNewB().val     = ST_OnOff.off;
            ctRpr.AddNewBCs().val   = ST_OnOff.off;
            ctRpr.AddNewColor().val = "auto";
            ctRpr.AddNewSz().val    = 24UL;
            ctRpr.AddNewSzCs().val  = 24UL;
            CT_P ctP = block.AddNewSdtContent().AddNewP();

            byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24");
            ctP.rsidR        = bytes;
            ctP.rsidRDefault = bytes;
            ctP.AddNewPPr().AddNewPStyle().val = "TOCHeading";
            ctP.AddNewR().AddNewT().Value      = "Table of Contents";
        }
Пример #2
0
Файл: TOC.cs Проект: zzy092/npoi
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr         sdtPr = block.AddNewSdtPr();
            CT_DecimalNumber id    = sdtPr.AddNewId();

            id.val = ("4844945");
            sdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = ("Table of contents");
            CT_SdtEndPr sdtEndPr = block.AddNewSdtEndPr();
            CT_RPr      rPr      = sdtEndPr.AddNewRPr();
            CT_Fonts    fonts    = rPr.AddNewRFonts();

            fonts.asciiTheme    = (ST_Theme.minorHAnsi);
            fonts.eastAsiaTheme = (ST_Theme.minorHAnsi);
            fonts.hAnsiTheme    = (ST_Theme.minorHAnsi);
            fonts.cstheme       = (ST_Theme.minorBidi);
            rPr.AddNewB().val          = false;
            rPr.AddNewBCs().val        = false;
            rPr.AddNewColor().val      = ("auto");
            rPr.AddNewSz().val         = 24;
            rPr.AddNewSzCs().val       = 24;
            CT_SdtContentBlock content = block.AddNewSdtContent();
            CT_P p = content.AddNewP();

            byte[] b = Encoding.GetEncoding(1252).GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            p.AddNewPPr().AddNewPStyle().val = ("TOCHeading");
            p.AddNewR().AddNewT().Value      = ("Table of Contents");
        }
Пример #3
0
        /**
         * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated
         * that the XWPFFooter or XWPFHeader object returned by calls to the CreateHeader(int, XWPFParagraph[])
         * and CreateFooter(int, XWPFParagraph[]) methods or the GetXXXXXHeader/Footer methods where
         * headers or footers had been Added to a document since it had been Created/opened, returned
         * an object that Contained no XWPFParagraph objects even if the header/footer itself did contain
         * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.NewInstance();
         * Created a brand new instance of the CTHDRFtr class which was then populated with data when
         * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object
         * that had previoulsy been instantiated in the CreateHeader(int, XWPFParagraph[]) or
         * CreateFooter(int, XWPFParagraph[]) methods.
         */
        private CT_HdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper)
        {
            CT_HdrFtr ftr = wrapper._getHdrFtr();

            if (paragraphs != null)
            {
                for (int i = 0; i < paragraphs.Length; i++)
                {
                    CT_P p = ftr.AddNewP();
                    ftr.SetPArray(i, paragraphs[i].GetCTP());
                }
            }
            else
            {
                CT_P    p    = ftr.AddNewP();
                CT_Body body = doc.Document.body;
                if (body.SizeOfPArray() > 0)
                {
                    CT_P p0 = body.GetPArray(0);
                    if (p0.IsSetRsidR())
                    {
                        byte[] rsidr        = p0.rsidR;
                        byte[] rsidrdefault = p0.rsidRDefault;
                        p.rsidP        = rsidr;
                        p.rsidRDefault = rsidrdefault;
                    }
                }
                CT_PPr pPr             = p.AddNewPPr();
                pPr.AddNewPStyle().val = (pStyle);
            }
            return(ftr);
        }
Пример #4
0
        private CT_HdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs)
        {
            CT_HdrFtr ftr = new CT_HdrFtr();

            if (paragraphs != null)
            {
                for (int i = 0; i < paragraphs.Length; i++)
                {
                    CT_P p = ftr.AddNewP();
                    //ftr.PArray=(0, paragraphs[i].CTP);		// MB 23 May 2010
                    ftr.SetPArray(i, paragraphs[i].GetCTP());           // MB 23 May 2010
                }
            }
            else
            {
                CT_P   p            = ftr.AddNewP();
                byte[] rsidr        = doc.Document.body.GetPArray(0).rsidR;
                byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
                p.rsidR        = (rsidr);
                p.rsidRDefault = (rsidrdefault);
                CT_PPr pPr             = p.AddNewPPr();
                pPr.AddNewPStyle().val = (pStyle);
            }
            return(ftr);
        }
        /// <summary>
        /// 将表生成字段
        /// </summary>
        /// <param name="fileName">生成文件的名称</param>
        /// <param name="sql">获取数据库所有表 SELECT name FROM tmc..sysobjects Where xtype='U' ORDER BY name </param>
        /// <param name="WordFieldName"> Word中列的名称</param>
        public override void CreateTablesToWord(string fileName, string[] wordFieldName)
        {
            using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                var           m_Docx = CreateXWPFDocument(fileName);
                XWPFParagraph p0     = m_Docx.CreateParagraph();
                XWPFRun       r0     = p0.CreateRun();
                r0.SetText("DOCX表");

                //获取数据源
                var tables = con.Query <TablesName>(_getTablesSql).ToList();

                for (int i = 0; i < tables.Count; i++)
                {
                    //获取 数据源
                    var FieldNames = GetTableFileds(tables[i].Tables_in_Tmc);

                    XWPFTable table = m_Docx.CreateTable(2, 4);//创建一行四列表

                    //每一行的中文名称 比如 字段 数据类型 可为空 描述
                    for (int icol = 0; icol < 4; icol++)
                    {
                        table.GetRow(1).GetCell(icol).SetText(wordFieldName[icol]);
                        table.GetRow(1).GetCell(icol).SetColor("#BABABA");
                    }

                    //  数据库中的字段名称,类型备注信息
                    for (int j = 1; j < FieldNames.Count; j++)
                    {
                        XWPFTableRow m_Row = table.CreateRow();
                        m_Row.GetCell(0).SetText(FieldNames[j].COLUMN_NAME);
                        m_Row.GetCell(1).SetText(FieldNames[j].DotNet_DATA_TYPE);
                        m_Row.GetCell(2).SetText(FieldNames[j].IsNullAble);
                        m_Row.GetCell(3).SetText(FieldNames[j].COLUMN_COMMENT);
                    }

                    //标注表名,如果放在放在上面,只能生成一列,这样88行会报错
                    table.GetRow(0).MergeCells(0, 3);
                    table.GetRow(0).GetCell(0).SetText(tables[i].Tables_in_Tmc);
                    table.GetRow(0).GetCell(0).SetColor("#98FB98");
                    table.GetRow(0).GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)500;

                    //创建一个空白行分隔每个表,并添加每一行标题
                    CT_P newLine = m_Docx.Document.body.AddNewP();
                    newLine.AddNewPPr().AddNewJc().val = ST_Jc.center;           //段落水平居中
                    XWPFParagraph gp       = new XWPFParagraph(newLine, m_Docx); //创建XWPFParagraph
                    XWPFRun       runTitle = gp.CreateRun();
                    runTitle.IsBold = true;
                    runTitle.SetText(i.ToString() + "." + tables[i].Tables_in_Tmc);
                    runTitle.FontSize = 16;
                    runTitle.SetFontFamily("宋体", FontCharRange.None);//设置雅黑字体
                }

                MemoryStream ms = new MemoryStream();
                m_Docx.Write(ms);
                ms.Flush();
                SaveToFile(ms, "d:\\Tmc数据库表字段说明.docx");
            }
        }
Пример #6
0
        /// <summary>
        /// 设置段落对齐方式
        /// </summary>
        /// <param name="p"></param>
        /// <param name="align"></param>
        public static void SetAlign(this CT_P p, ST_Jc align)
        {
            CT_PPr ppr = p.pPr == null?p.AddNewPPr() : p.pPr;

            CT_Jc jc = ppr.IsSetJc() ? ppr.jc : ppr.AddNewJc();

            jc.val = align;
        }
Пример #7
0
 /**
  * SetNumID of Paragraph
  * @param numPos
  */
 public void SetNumID(string numId)
 {
     if (paragraph.pPr == null)
     {
         paragraph.AddNewPPr();
     }
     if (paragraph.pPr.numPr == null)
     {
         paragraph.pPr.AddNewNumPr();
     }
     if (paragraph.pPr.numPr.numId == null)
     {
         paragraph.pPr.numPr.AddNewNumId();
     }
     paragraph.pPr.numPr.ilvl      = new CT_DecimalNumber();
     paragraph.pPr.numPr.ilvl.val  = "0";
     paragraph.pPr.numPr.numId.val = numId;
 }
        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");
                }
            }
        }
Пример #9
0
        public void AddRow(int level, string title, int page, string bookmarkRef)
        {
            CT_P ctP = this.block.sdtContent.AddNewP();

            byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24");
            ctP.rsidR        = bytes;
            ctP.rsidRDefault = bytes;
            CT_PPr ctPpr = ctP.AddNewPPr();

            ctPpr.AddNewPStyle().val = nameof(TOC) + (object)level;
            CT_TabStop ctTabStop     = ctPpr.AddNewTabs().AddNewTab();

            ctTabStop.val    = ST_TabJc.right;
            ctTabStop.leader = ST_TabTlc.dot;
            ctTabStop.pos    = "8290";
            ctPpr.AddNewRPr().AddNewNoProof();
            CT_R ctR1 = ctP.AddNewR();

            ctR1.AddNewRPr().AddNewNoProof();
            ctR1.AddNewT().Value = title;
            CT_R ctR2            = ctP.AddNewR();

            ctR2.AddNewRPr().AddNewNoProof();
            ctR2.AddNewTab();
            CT_R ctR3 = ctP.AddNewR();

            ctR3.AddNewRPr().AddNewNoProof();
            ctR3.AddNewFldChar().fldCharType = ST_FldCharType.begin;
            CT_R ctR4 = ctP.AddNewR();

            ctR4.AddNewRPr().AddNewNoProof();
            CT_Text ctText = ctR4.AddNewInstrText();

            ctText.space = "preserve";
            ctText.Value = " PAGEREF _Toc" + bookmarkRef + " \\h ";
            ctP.AddNewR().AddNewRPr().AddNewNoProof();
            CT_R ctR5 = ctP.AddNewR();

            ctR5.AddNewRPr().AddNewNoProof();
            ctR5.AddNewFldChar().fldCharType = ST_FldCharType.separate;
            CT_R ctR6 = ctP.AddNewR();

            ctR6.AddNewRPr().AddNewNoProof();
            ctR6.AddNewT().Value = page.ToString();
            CT_R ctR7            = ctP.AddNewR();

            ctR7.AddNewRPr().AddNewNoProof();
            ctR7.AddNewFldChar().fldCharType = ST_FldCharType.end;
        }
Пример #10
0
        public void AddRow(int level, String title, int page, String bookmarkRef)
        {
            CT_SdtContentBlock contentBlock = this.block.sdtContent;
            CT_P p = contentBlock.AddNewP();

            byte[] b = Encoding.Unicode.GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            CT_PPr pPr = p.AddNewPPr();

            pPr.AddNewPStyle().val = ("TOC" + level);
            CT_Tabs    tabs        = pPr.AddNewTabs();
            CT_TabStop tab         = tabs.AddNewTab();

            tab.val    = (ST_TabJc.right);
            tab.leader = (ST_TabTlc.dot);
            tab.pos    = "8290"; //(new BigInteger("8290"));
            pPr.AddNewRPr().AddNewNoProof();
            CT_R Run = p.AddNewR();

            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewT().Value = (title);
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewTab();
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewFldChar().fldCharType = (ST_FldCharType.begin);
            // pageref run
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            CT_Text text = Run.AddNewInstrText();

            text.space = "preserve";// (Space.PRESERVE);
            // bookmark reference
            text.Value = (" PAGEREF _Toc" + bookmarkRef + " \\h ");
            p.AddNewR().AddNewRPr().AddNewNoProof();
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewFldChar().fldCharType = (ST_FldCharType.separate);
            // page number run
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewT().Value = page.ToString();
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewFldChar().fldCharType = (ST_FldCharType.end);
        }
Пример #11
0
        public void TestSetGetWordWrap()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            CT_OnOff wordWrap = ppr.AddNewWordWrap();

            wordWrap.val = false;
            Assert.AreEqual(false, p.IsWordWrap);

            p.IsWordWrap = true;
            Assert.AreEqual(true, ppr.wordWrap.val);
        }
Пример #12
0
        public void TestSetGetPageBreak()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            CT_OnOff pageBreak = ppr.AddNewPageBreakBefore();

            pageBreak.val = false;
            Assert.AreEqual(false, p.IsPageBreak);

            p.IsPageBreak = (true);
            Assert.AreEqual(true, ppr.pageBreakBefore.val);
        }
Пример #13
0
        public void TestSetGetVerticalAlignment()
        {
            //new clean instance of paragraph
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            CT_TextAlignment txtAlign = ppr.AddNewTextAlignment();

            txtAlign.val = (ST_TextAlignment.center);
            Assert.AreEqual(TextAlignment.CENTER, p.VerticalAlignment);

            p.VerticalAlignment = (TextAlignment.BOTTOM);
            Assert.AreEqual(ST_TextAlignment.bottom, ppr.textAlignment.val);
        }
Пример #14
0
        private static void CreateFooter(XWPFDocument doc, string headerText, string imageUrl = null)
        {
            if (doc.Document.body.sectPr == null)
            {
                doc.Document.body.sectPr = new CT_SectPr();
            }
            CT_SectPr m_SectPr = doc.Document.body.sectPr;

            CT_Ftr footer = new CT_Ftr();

            CT_SdtBlock   sdtBlock   = new CT_SdtBlock();
            CT_SdtPr      sdtPr      = sdtBlock.AddNewSdtPr();
            CT_SdtDocPart sdtDocPart = sdtPr.AddNewDocPartObj();

            sdtDocPart.AddNewDocPartGallery().val = "Page Numbers (Bottom of Page)";
            sdtDocPart.docPartUnique = new CT_OnOff();

            CT_SdtContentBlock sdtContentBlock = sdtBlock.AddNewSdtContent();
            CT_P p = sdtContentBlock.AddNewP();

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

            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
            p.AddNewR().AddNewInstrText().Value     = "PAGE";
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;

            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
            p.AddNewR().AddNewInstrText().Value     = "NUMPAGES";
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;


            footer.Items.Add(sdtBlock);

            XWPFRelation Hrelation  = XWPFRelation.FOOTER;
            XWPFFooter   xwpfFooter = (XWPFFooter)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.FooterList.Count);

            xwpfFooter.SetXWPFDocument(doc);
            xwpfFooter.SetHeaderFooter(footer);

            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewFooterReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = xwpfFooter.GetPackageRelationship().Id;
        }
Пример #15
0
        public void TestSetGetSpacingLineRule()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            Assert.AreEqual(LineSpacingRule.AUTO, p.SpacingLineRule);

            CT_Spacing spacing = ppr.AddNewSpacing();

            spacing.lineRule = (ST_LineSpacingRule.atLeast);
            Assert.AreEqual(LineSpacingRule.ATLEAST, p.SpacingLineRule);

            p.SpacingAfter = 100;
            Assert.AreEqual(100, (int)spacing.after);
        }
Пример #16
0
        public void TestSetGetSpacing()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            Assert.AreEqual(-1, p.SpacingAfter);

            CT_Spacing spacing = ppr.AddNewSpacing();

            spacing.after = 10;
            Assert.AreEqual(10, p.SpacingAfter);

            p.SpacingAfter = 100;
            Assert.AreEqual(100, (int)spacing.after);
        }
Пример #17
0
        public void TestSetAlignment()
        {
            //new clean instance of paragraph
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            Assert.AreEqual(ParagraphAlignment.LEFT, p.Alignment);

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            CT_Jc align = ppr.AddNewJc();

            align.val = (ST_Jc.center);
            Assert.AreEqual(ParagraphAlignment.CENTER, p.Alignment);

            p.Alignment = (ParagraphAlignment.BOTH);
            Assert.AreEqual((int)ST_Jc.both, (int)ppr.jc.val);
        }
Пример #18
0
        public void TestSetGetIndentation()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            Assert.AreEqual(-1, p.IndentationLeft);

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            Assert.AreEqual(-1, p.IndentationLeft);

            CT_Ind ind = ppr.AddNewInd();

            ind.left = "10";
            Assert.AreEqual(10, p.IndentationLeft);

            p.IndentationLeft = 100;
            Assert.AreEqual(100, int.Parse(ind.left));
        }
Пример #19
0
        public void TestSetBorderTop()
        {
            //new clean instance of paragraph
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            Assert.AreEqual(ST_Border.none, EnumConverter.ValueOf <ST_Border, Borders>(p.BorderTop));

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            //bordi
            CT_PBdr   bdr       = ppr.AddNewPBdr();
            CT_Border borderTop = bdr.AddNewTop();

            borderTop.val = (ST_Border.@double);
            bdr.top       = (borderTop);

            Assert.AreEqual(Borders.Double, p.BorderTop);
            p.BorderTop = (Borders.Single);
            Assert.AreEqual(ST_Border.single, borderTop.val);
        }
Пример #20
0
        private CT_HdrFtr buildHdrFtr(string pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper)
        {
            CT_HdrFtr hdrFtr = wrapper._getHdrFtr();

            if (paragraphs != null)
            {
                for (int i = 0; i < paragraphs.Length; ++i)
                {
                    hdrFtr.AddNewP();
                    hdrFtr.SetPArray(i, paragraphs[i].GetCTP());
                }
            }
            else
            {
                CT_P   ctP          = hdrFtr.AddNewP();
                byte[] rsidR        = this.doc.Document.body.GetPArray(0).rsidR;
                byte[] rsidRdefault = this.doc.Document.body.GetPArray(0).rsidRDefault;
                ctP.rsidP        = rsidR;
                ctP.rsidRDefault = rsidRdefault;
                ctP.AddNewPPr().AddNewPStyle().val = pStyle;
            }
            return(hdrFtr);
        }
Пример #21
0
        private CT_HdrFtr buildHdrFtr(string pStyle, XWPFParagraph[] paragraphs)
        {
            CT_HdrFtr ctHdrFtr = new CT_HdrFtr();

            if (paragraphs != null)
            {
                for (int i = 0; i < paragraphs.Length; ++i)
                {
                    ctHdrFtr.AddNewP();
                    ctHdrFtr.SetPArray(i, paragraphs[i].GetCTP());
                }
            }
            else
            {
                CT_P   ctP          = ctHdrFtr.AddNewP();
                byte[] rsidR        = this.doc.Document.body.GetPArray(0).rsidR;
                byte[] rsidRdefault = this.doc.Document.body.GetPArray(0).rsidRDefault;
                ctP.rsidR        = rsidR;
                ctP.rsidRDefault = rsidRdefault;
                ctP.AddNewPPr().AddNewPStyle().val = pStyle;
            }
            return(ctHdrFtr);
        }
Пример #22
0
        /**
         * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated
         * that the XWPFFooter or XWPFHeader object returned by calls to the CreateHeader(int, XWPFParagraph[])
         * and CreateFooter(int, XWPFParagraph[]) methods or the GetXXXXXHeader/Footer methods where
         * headers or footers had been Added to a document since it had been Created/opened, returned
         * an object that Contained no XWPFParagraph objects even if the header/footer itself did contain
         * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.NewInstance();
         * Created a brand new instance of the CTHDRFtr class which was then populated with data when
         * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object
         * that had previoulsy been instantiated in the CreateHeader(int, XWPFParagraph[]) or
         * CreateFooter(int, XWPFParagraph[]) methods.
         */

        private CT_HdrFtr buildHdrFtr(string pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper)
        {
            CT_HdrFtr ftr = wrapper._getHdrFtr();

            if (paragraphs != null)
            {
                for (int i = 0; i < paragraphs.Length; i++)
                {
                    CT_P p = ftr.AddNewP();
                    ftr.SetPArray(i, paragraphs[i].GetCTP());
                }
            }
            else
            {
                CT_P   p            = ftr.AddNewP();
                byte[] rsidr        = doc.Document.body.GetPArray(0).rsidR;
                byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
                p.rsidP        = (rsidr);
                p.rsidRDefault = (rsidrdefault);
                CT_PPr pPr             = p.AddNewPPr();
                pPr.AddNewPStyle().val = (pStyle);
            }
            return(ftr);
        }
        /// <summary>
        /// Creates a new section with the given page header.  Must be
        /// called *after* the final paragraph of the section.  In DOCX, a
        /// section definition is a child of the final paragraph of the
        /// section, except for the final section of the document, which
        /// is a direct child of the body.
        /// </summary>
        /// <param name="bookname"> The name of the book to display, usually from the \h marker </param>
        public void createBookHeaders(string bookname)
        {
            // Create page heading content for book
            CT_Hdr header          = new CT_Hdr();
            CT_P   headerParagraph = header.AddNewP();
            CT_PPr ppr             = headerParagraph.AddNewPPr();
            CT_Jc  align           = ppr.AddNewJc();

            align.val = ST_Jc.center;
            CT_R run = headerParagraph.AddNewR();

            // Show page numbers if requested
            if (configDocx.showPageNumbers)
            {
                // Page number
                run.AddNewFldChar().fldCharType = ST_FldCharType.begin;
                run.AddNewInstrText().Value     = " PAGE ";
                run.AddNewFldChar().fldCharType = ST_FldCharType.separate;
                run.AddNewInstrText().Value     = "1";
                run.AddNewFldChar().fldCharType = ST_FldCharType.end;
                run.AddNewT().Value             = "  -  ";
            }

            // Book name
            run.AddNewT().Value = bookname == null ? "" : bookname;
            // Chapter name
            if (currentChapterLabel.Length > 0)
            {
                run.AddNewT().Value = "  -  ";
                run.AddNewT().Value = currentChapterLabel;
            }


            // Create page header
            XWPFHeader documentHeader = (XWPFHeader)newDoc.CreateRelationship(XWPFRelation.HEADER, XWPFFactory.GetInstance(), pageHeaderCount);

            documentHeader.SetHeaderFooter(header);

            // Create new section and set its header
            CT_SectPr newSection = newDoc.Document.body.AddNewP().AddNewPPr().createSectPr();

            newSection.type     = new CT_SectType();
            newSection.type.val = ST_SectionMark.continuous;
            CT_HdrFtrRef headerRef = newSection.AddNewHeaderReference();

            headerRef.type = ST_HdrFtr.@default;
            headerRef.id   = documentHeader.GetPackageRelationship().Id;

            // Set number of columns
            newSection.cols.num = configDocx.columnCount.ToString();

            // Set page numbers
            CT_PageNumber pageNum = new CT_PageNumber
            {
                fmt = ST_NumberFormat.@decimal
            };

            newSection.pgNumType = pageNum;

            // Increment page header count so each one gets a unique ID
            pageHeaderCount++;
        }
Пример #24
0
        private XWPFParagraph GetWatermarkParagraph(string text, int idx)
        {
            CT_P prgrph = new CT_P();

            byte[] rsidR        = this.doc.Document.body.GetPArray(0).rsidR;
            byte[] rsidRdefault = this.doc.Document.body.GetPArray(0).rsidRDefault;
            prgrph.rsidP        = rsidR;
            prgrph.rsidRDefault = rsidRdefault;
            prgrph.AddNewPPr().AddNewPStyle().val       = "Header";
            NPOI.OpenXmlFormats.Wordprocessing.CT_R ctR = prgrph.AddNewR();
            ctR.AddNewRPr().AddNewNoProof();
            CT_Picture   ctPicture   = ctR.AddNewPict();
            CT_Group     ctGroup     = new CT_Group();
            CT_Shapetype ctShapetype = ctGroup.AddNewShapetype();

            ctShapetype.id        = "_x0000_t136";
            ctShapetype.coordsize = "1600,21600";
            ctShapetype.spt       = 136f;
            ctShapetype.adj       = "10800";
            ctShapetype.path2     = "m@7,0l@8,0m@5,21600l@6,21600e";
            CT_Formulas ctFormulas = ctShapetype.AddNewFormulas();

            ctFormulas.AddNewF().eqn = "sum #0 0 10800";
            ctFormulas.AddNewF().eqn = "prod #0 2 1";
            ctFormulas.AddNewF().eqn = "sum 21600 0 @1";
            ctFormulas.AddNewF().eqn = "sum 0 0 @2";
            ctFormulas.AddNewF().eqn = "sum 21600 0 @3";
            ctFormulas.AddNewF().eqn = "if @0 @3 0";
            ctFormulas.AddNewF().eqn = "if @0 21600 @1";
            ctFormulas.AddNewF().eqn = "if @0 0 @2";
            ctFormulas.AddNewF().eqn = "if @0 @4 21600";
            ctFormulas.AddNewF().eqn = "mid @5 @6";
            ctFormulas.AddNewF().eqn = "mid @8 @5";
            ctFormulas.AddNewF().eqn = "mid @7 @8";
            ctFormulas.AddNewF().eqn = "mid @6 @7";
            ctFormulas.AddNewF().eqn = "sum @6 0 @5";
            CT_Path ctPath           = ctShapetype.AddNewPath();

            ctPath.textpathok    = ST_TrueFalse.t;
            ctPath.connecttype   = ST_ConnectType.custom;
            ctPath.connectlocs   = "@9,0;@10,10800;@11,21600;@12,10800";
            ctPath.connectangles = "270,180,90,0";
            CT_TextPath ctTextPath1 = ctShapetype.AddNewTextpath();

            ctTextPath1.on       = ST_TrueFalse.t;
            ctTextPath1.fitshape = ST_TrueFalse.t;
            CT_H ctH = ctShapetype.AddNewHandles().AddNewH();

            ctH.position = "#0,bottomRight";
            ctH.xrange   = "6629,14971";
            ctShapetype.AddNewLock().ext = ST_Ext.edit;
            CT_Shape ctShape             = ctGroup.AddNewShape();

            ctShape.id         = "PowerPlusWaterMarkObject" + (object)idx;
            ctShape.spid       = "_x0000_s102" + (object)(4 + idx);
            ctShape.type       = "#_x0000_t136";
            ctShape.style      = "position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin";
            ctShape.wrapcoords = "616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068";
            ctShape.fillcolor  = "black";
            ctShape.stroked    = ST_TrueFalse.@false;
            CT_TextPath ctTextPath2 = ctShape.AddNewTextpath();

            ctTextPath2.style   = "font-family:&quot;Cambria&quot;;font-size:1pt";
            ctTextPath2.@string = text;
            ctPicture.Set((object)ctGroup);
            return(new XWPFParagraph(prgrph, (IBody)this.doc));
        }
Пример #25
0
        /// <summary>
        /// 创建页眉页脚
        /// </summary>
        /// <param name="doc"></param>
        private void CreatePageHeaderFooter(XWPFDocument doc, CT_SectPr m_Sectpr)
        {
            XWPFParagraph p1 = doc.CreateParagraph();

            p1.CreateRun().AddBreak();//新建页

            //创建页眉

            CT_Hdr m_hdr = new CT_Hdr();

            m_hdr.Items = new System.Collections.ArrayList();


            CT_P   m_p    = m_hdr.AddNewP();
            CT_PPr cT_PPr = m_p.AddNewPPr();

            cT_PPr.AddNewJc().val = ST_Jc.both;//两端对齐
            cT_PPr.AddNewSpacing().beforeLines = "370";


            CT_R cT_R = m_p.AddNewR();

            cT_R.AddNewT().Value = "110kV濂泉(沙河)送电线路工程                 施工图设计说明书                     S123456S-D0101-01";//页眉内容
            CT_RPr cT_RPr        = cT_R.AddNewRPr();

            cT_RPr.AddNewSz().val       = (ulong)18;
            cT_RPr.AddNewSzCs().val     = (ulong)18;
            cT_RPr.AddNewRFonts().ascii = "宋体";
            //cT_RPr.AddNewU().val=ST_Underline.single;//下划线

            //创建页眉关系(headern.xml)
            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader   m_h       = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), 3);

            //doc.CreateFootnotes();
            //设置页眉
            m_h.SetHeaderFooter(m_hdr);
            CT_HdrFtrRef m_HdrFtr = m_Sectpr.AddNewHeaderReference();

            m_HdrFtr.type = ST_HdrFtr.@default;
            //m_h.GetRelationById(m_HdrFtr.id);
            m_HdrFtr.id = m_h.GetPackageRelationship().Id;



            //创建页脚
            CT_Ftr m_ftr = new CT_Ftr();

            m_ftr.Items = new System.Collections.ArrayList();
            CT_SdtBlock   m_Sdt          = new CT_SdtBlock();
            CT_SdtPr      m_SdtPr        = m_Sdt.AddNewSdtPr();
            CT_SdtDocPart m_SdDocPartObj = m_SdtPr.AddNewDocPartObj();

            m_SdDocPartObj.AddNewDocPartGallery().val = "PageNumbers (Bottom of Page)";
            m_SdDocPartObj.docPartUnique = new CT_OnOff();
            CT_SdtContentBlock m_SdtContent = m_Sdt.AddNewSdtContent();
            CT_P   m_SdtContentP            = m_SdtContent.AddNewP();
            CT_PPr m_SdtContentPpr          = m_SdtContentP.AddNewPPr();

            m_SdtContentPpr.AddNewJc().val = ST_Jc.center;
            m_SdtContentP.Items = new System.Collections.ArrayList();
            CT_SimpleField m_fldSimple = new CT_SimpleField();

            m_fldSimple.instr = " PAGE   \\*MERGEFORMAT";

            //页码字体大小
            CT_R   m_r   = new CT_R();
            CT_RPr m_Rpr = m_r.AddNewRPr();

            m_Rpr.AddNewRFonts().ascii = "宋体";
            m_Rpr.AddNewSz().val       = (ulong)18;
            m_Rpr.AddNewSzCs().val     = (ulong)18;
            m_r.AddNewT().Value        = "1";//页数
            m_fldSimple.Items.Add(m_r);

            m_SdtContentP.Items.Add(m_fldSimple);
            m_ftr.Items.Add(m_Sdt);

            //m_ftr.AddNewP().AddNewR().AddNewT().Value = "fff";//页脚内容
            //m_ftr.AddNewP().AddNewPPr().AddNewJc().val = ST_Jc.center;
            //创建页脚关系(footern.xml)
            XWPFRelation Frelation = XWPFRelation.FOOTER;
            XWPFFooter   m_f       = (XWPFFooter)doc.CreateRelationship(Frelation, XWPFFactory.GetInstance(), doc.FooterList.Count + 1);

            //设置页脚
            m_f.SetHeaderFooter(m_ftr);
            m_HdrFtr      = m_Sectpr.AddNewFooterReference();
            m_HdrFtr.type = ST_HdrFtr.@default;
            m_HdrFtr.id   = m_f.GetPackageRelationship().Id;
        }
Пример #26
0
        public static MemoryStream Export(News model, string plsushMethodStr)
        {
            XWPFDocument doc = new XWPFDocument();

            XWPFParagraph p1 = doc.CreateParagraph();
            XWPFRun       r1 = p1.CreateRun();

            r1.FontSize   = 18;
            r1.FontFamily = "微软雅黑";
            r1.SetText(model.Title);
            r1.SetTextPosition(30);

            CT_P doc_p1 = doc.Document.body.GetPArray(0);//标题居中

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


            XWPFParagraph p2 = doc.CreateParagraph();

            //段落对其方式为居中
            p2.Alignment = ParagraphAlignment.RIGHT;
            XWPFRun r2 = p2.CreateRun(); //向该段落中添加文字

            r2.FontSize   = 8;           //设置大小
            r2.FontFamily = "微软雅黑";
            r2.SetText($"作者:{model.PenName}");


            XWPFParagraph p3 = doc.CreateParagraph();

            //段落对其方式为居中
            p3.Alignment = ParagraphAlignment.RIGHT;
            XWPFRun r3 = p3.CreateRun(); //向该段落中添加文字

            r3.FontSize   = 8;           //设置大小
            r3.FontFamily = "微软雅黑";
            r3.SetText($"投稿时间:{model.SubmitTime}");

            if (plsushMethodStr != "")
            {
                XWPFParagraph p4 = doc.CreateParagraph();
                //段落对其方式为居中
                p4.Alignment = ParagraphAlignment.RIGHT;
                XWPFRun r4 = p4.CreateRun(); //向该段落中添加文字
                r4.FontSize   = 8;           //设置大小
                r4.FontFamily = "微软雅黑";
                r4.SetText($"发布渠道:{plsushMethodStr}");
            }

            model.Content.Replace("&nbsp;", " ");
            var list = GetHtmlImageUrlList(model.Content);

            if (list.Length > 0)
            {
                foreach (var item in list)
                {
                    model.Content.Replace(item, "❶");
                }
                var ary = model.Content.Split('❶');
                if (ary.Length > 0)
                {
                    for (var i = 0; i < ary.Length; i++)
                    {
                        XWPFParagraph obj = doc.CreateParagraph();
                        //段落对其方式为居中
                        obj.Alignment = ParagraphAlignment.LEFT;
                        XWPFRun textRun = obj.CreateRun(); //向该段落中添加文字
                        textRun.FontSize   = 10;           //设置大小
                        textRun.FontFamily = "微软雅黑";
                        textRun.SetText($"{(char)9}{xxHTML(ary[i])}");

                        XWPFRun r6 = obj.CreateRun();//向该段落中添加文字

                        var    widthEmus  = (int)(400.0 * 9525);
                        var    heightEmus = (int)(300.0 * 9525);
                        string path       = Get_img(list[i]);
                        if (File.Exists(path))
                        {
                            using (FileStream picData = new FileStream(path, FileMode.Open, FileAccess.Read))
                            {
                                r6.AddPicture(picData, (int)PictureType.PNG, "图片", widthEmus, heightEmus);
                            }
                        }
                        AsyncHelper.Run(() => {
                            File.Delete(path);
                        });
                    }
                }
            }
            else
            {
                XWPFParagraph p5 = doc.CreateParagraph();
                //段落对其方式为居中
                p5.Alignment = ParagraphAlignment.LEFT;
                XWPFRun r5 = p5.CreateRun(); //向该段落中添加文字
                r5.FontSize   = 10;          //设置大小
                r5.FontFamily = "微软雅黑";
                r5.SetText($"{(char)9}{xxHTML(model.Content)}");
            }

            if (model.Paths.IsNotNullOrEmpty())
            {
                XWPFParagraph p6 = doc.CreateParagraph();
                //段落对其方式为居中
                p6.Alignment = ParagraphAlignment.LEFT;
                XWPFRun r6 = p6.CreateRun();//向该段落中添加文字

                var widthEmus  = (int)(400.0 * 9525);
                var heightEmus = (int)(300.0 * 9525);
                var index      = 1;
                foreach (var item in model.Paths.Split(','))
                {
                    using (FileStream picData = new FileStream(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + item, FileMode.Open, FileAccess.Read))
                    {
                        r6.AddPicture(picData, (int)PictureType.PNG, "图片" + index, widthEmus, heightEmus);
                    }
                    index++;
                }
            }

            return(new MemoryStream(ToByte(doc)));
        }
Пример #27
0
        private ReturnMessage WriteDoc()
        {
            ReturnMessage retMsg = new ReturnMessage(string.Empty, true);
            FileStream    fs     = null;

            try
            {
                XWPFDocument doc   = new XWPFDocument();
                XWPFTable    table = null;
                int          index = 1;
                foreach (DataRow dr in dtInfo.Rows)
                {
                    if (dr["表名"] != DBNull.Value && !string.IsNullOrEmpty(dr["表名"].ToString()))
                    {
                        //表名,以段落表示
                        CT_P ctp = doc.Document.body.AddNewP();
                        //XWPFParagraph p = doc.CreateParagraph();
                        XWPFParagraph p = new XWPFParagraph(ctp, doc);
                        XWPFRun       r = p.CreateRun();
                        //设置字体
                        r.GetCTR().AddNewRPr().AddNewRFonts().ascii    = "宋体";
                        r.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "宋体";
                        r.GetCTR().AddNewRPr().AddNewRFonts().hint     = ST_Hint.eastAsia;
                        r.GetCTR().AddNewRPr().AddNewSz().val          = (ulong)32;//3号字体;
                        r.GetCTR().AddNewRPr().AddNewSzCs().val        = (ulong)32;
                        //设置行间距
                        //单倍为默认值(240twip)不需设置,1.5倍=240X1.5=360twip,2倍=240X2=480twip
                        ctp.AddNewPPr().AddNewSpacing().line = "720";
                        //ctp.AddNewPPr().AddNewSpacing().lineRule = ST_LineSpacingRule.exact;
                        //设置段落文本
                        r.SetText(index.ToString() + "." + dr["表名"].ToString());

                        //表结构,以表格显示
                        CT_Tbl m_CTTbl = doc.Document.body.AddNewTbl();
                        table = doc.CreateTable(1, 9);
                        //标题行(固定)
                        //列宽
                        CT_TcPr mPr = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "900";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "1500";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "500";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "1000";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "500";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(6).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "900";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(7).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "800";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        mPr          = table.GetRow(0).GetCell(8).GetCTTc().AddNewTcPr();
                        mPr.tcW      = new CT_TblWidth();
                        mPr.tcW.w    = "1500";
                        mPr.tcW.type = ST_TblWidth.dxa;
                        //填充文字
                        table.GetRow(0).GetCell(0).SetText("字段序号");
                        table.GetRow(0).GetCell(1).SetText("字段名");
                        table.GetRow(0).GetCell(2).SetText("主键");
                        table.GetRow(0).GetCell(3).SetText("类型");
                        table.GetRow(0).GetCell(4).SetText("长度");
                        table.GetRow(0).GetCell(5).SetText("精度");
                        table.GetRow(0).GetCell(6).SetText("小数位数");
                        table.GetRow(0).GetCell(7).SetText("允许空");
                        table.GetRow(0).GetCell(8).SetText("字段说明");
                        //内容行
                        XWPFTableRow row = table.CreateRow();
                        row.GetCell(0).SetText(dr["字段序号"].ToString());
                        row.GetCell(1).SetText(dr["字段名"].ToString());
                        row.GetCell(2).SetText(dr["主键"].ToString());
                        row.GetCell(3).SetText(dr["类型"].ToString());
                        row.GetCell(4).SetText(dr["长度"].ToString());
                        row.GetCell(5).SetText(dr["精度"].ToString());
                        row.GetCell(6).SetText(dr["小数位数"].ToString());
                        row.GetCell(7).SetText(dr["允许空"].ToString());
                        row.GetCell(8).SetText("");
                        //
                        index++;
                    }
                    else
                    {
                        if (table != null)
                        {
                            //内容行
                            XWPFTableRow row = table.CreateRow();
                            row.GetCell(0).SetText(dr["字段序号"].ToString());
                            row.GetCell(1).SetText(dr["字段名"].ToString());
                            row.GetCell(2).SetText(dr["主键"].ToString());
                            row.GetCell(3).SetText(dr["类型"].ToString());
                            row.GetCell(4).SetText(dr["长度"].ToString());
                            row.GetCell(5).SetText(dr["精度"].ToString());
                            row.GetCell(6).SetText(dr["小数位数"].ToString());
                            row.GetCell(7).SetText(dr["允许空"].ToString());
                            row.GetCell(8).SetText("");
                        }
                    }
                }
                //输出保存
                string docAllPath = Application.StartupPath + "\\SqlDBDicFile.docx";
                if (File.Exists(docAllPath))
                {
                    File.Delete(docAllPath);
                }
                fs = File.OpenWrite(docAllPath);
                doc.Write(fs);
                doc.Close();
                return(retMsg);
            }
            catch (Exception ex)
            {
                retMsg.isSuccess = false;
                retMsg.Messages  = ex.Message;
                return(retMsg);
            }
            finally
            {
                fs.Close();
                fs.Dispose();
            }
        }
Пример #28
0
    /// <summary>
    /// 创建文档
    /// </summary>
    /// <param name="setting"></param>
    public static void ExportDocument(DocumentSetting setting)
    {
        XWPFDocument docx = new XWPFDocument();
        MemoryStream ms   = new MemoryStream();

        //设置文档
        docx.Document.body.sectPr = new CT_SectPr();
        CT_SectPr setPr = docx.Document.body.sectPr;
        //获取页面大小
        Tuple <int, int> size = GetPaperSize(setting.PaperType);

        setPr.pgSz.w = (ulong)size.Item1;
        setPr.pgSz.h = (ulong)size.Item2;
        //创建一个段落
        CT_P p = docx.Document.body.AddNewP();

        //段落水平居中
        p.AddNewPPr().AddNewJc().val = ST_Jc.center;
        XWPFParagraph gp             = new XWPFParagraph(p, docx);

        XWPFRun gr = gp.CreateRun();

        //创建标题
        if (!string.IsNullOrEmpty(setting.TitleSetting.Title))
        {
            gr.GetCTR().AddNewRPr().AddNewRFonts().ascii    = setting.TitleSetting.FontName;
            gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = setting.TitleSetting.FontName;
            gr.GetCTR().AddNewRPr().AddNewRFonts().hint     = ST_Hint.eastAsia;
            gr.GetCTR().AddNewRPr().AddNewSz().val          = (ulong)setting.TitleSetting.FontSize; //2号字体
            gr.GetCTR().AddNewRPr().AddNewSzCs().val        = (ulong)setting.TitleSetting.FontSize;
            gr.GetCTR().AddNewRPr().AddNewB().val           = setting.TitleSetting.HasBold;         //加粗
            gr.GetCTR().AddNewRPr().AddNewColor().val       = "black";                              //字体颜色
            gr.SetText(setting.TitleSetting.Title);
        }

        //创建文档主要内容
        if (!string.IsNullOrEmpty(setting.MainContentSetting.MainContent))
        {
            p = docx.Document.body.AddNewP();
            p.AddNewPPr().AddNewJc().val = ST_Jc.both;
            gp = new XWPFParagraph(p, docx)
            {
                IndentationFirstLine = 2
            };

            //单倍为默认值(240)不需设置,1.5倍=240X1.5=360,2倍=240X2=480
            p.AddNewPPr().AddNewSpacing().line     = "400";//固定20磅
            p.AddNewPPr().AddNewSpacing().lineRule = ST_LineSpacingRule.exact;

            gr = gp.CreateRun();
            CT_RPr   rpr    = gr.GetCTR().AddNewRPr();
            CT_Fonts rfonts = rpr.AddNewRFonts();
            rfonts.ascii    = setting.MainContentSetting.FontName;
            rfonts.eastAsia = setting.MainContentSetting.FontName;
            rpr.AddNewSz().val   = (ulong)setting.MainContentSetting.FontSize;//5号字体-21
            rpr.AddNewSzCs().val = (ulong)setting.MainContentSetting.FontSize;
            rpr.AddNewB().val    = setting.MainContentSetting.HasBold;

            gr.SetText(setting.MainContentSetting.MainContent);
        }

        //开始写入
        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();
    }
Пример #29
0
        /*
         * This is the default Watermark paragraph; the only variable is the text message
         * TODO: manage all the other variables
         */
        private XWPFParagraph GetWatermarkParagraph(String text, int idx)
        {
            CT_P p = new CT_P();
            byte[] rsidr = doc.Document.body.GetPArray(0).rsidR;
            byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
            p.rsidP = (rsidr);
            p.rsidRDefault = (rsidrdefault);
            CT_PPr pPr = p.AddNewPPr();
            pPr.AddNewPStyle().val = ("Header");
            // start watermark paragraph
            NPOI.OpenXmlFormats.Wordprocessing.CT_R r = p.AddNewR();
            CT_RPr rPr = r.AddNewRPr();
            rPr.AddNewNoProof();
            CT_Picture pict = r.AddNewPict();

            CT_Group group = new CT_Group();
            CT_Shapetype shapetype = group.AddNewShapetype();
            shapetype.id = ("_x0000_t136");
            shapetype.coordsize = ("1600,21600");
            shapetype.spt = (136);
            shapetype.adj = ("10800");
            shapetype.path2 = ("m@7,0l@8,0m@5,21600l@6,21600e");
            CT_Formulas formulas = shapetype.AddNewFormulas();
            formulas.AddNewF().eqn=("sum #0 0 10800");
            formulas.AddNewF().eqn = ("prod #0 2 1");
            formulas.AddNewF().eqn = ("sum 21600 0 @1");
            formulas.AddNewF().eqn = ("sum 0 0 @2");
            formulas.AddNewF().eqn = ("sum 21600 0 @3");
            formulas.AddNewF().eqn = ("if @0 @3 0");
            formulas.AddNewF().eqn = ("if @0 21600 @1");
            formulas.AddNewF().eqn = ("if @0 0 @2");
            formulas.AddNewF().eqn = ("if @0 @4 21600");
            formulas.AddNewF().eqn = ("mid @5 @6");
            formulas.AddNewF().eqn = ("mid @8 @5");
            formulas.AddNewF().eqn = ("mid @7 @8");
            formulas.AddNewF().eqn = ("mid @6 @7");
            formulas.AddNewF().eqn = ("sum @6 0 @5");
            CT_Path path = shapetype.AddNewPath();
            path.textpathok=(ST_TrueFalse.t);
            path.connecttype=(ST_ConnectType.custom);
            path.connectlocs=("@9,0;@10,10800;@11,21600;@12,10800");
            path.connectangles=("270,180,90,0");
            CT_TextPath shapeTypeTextPath = shapetype.AddNewTextpath();
            shapeTypeTextPath.on=(ST_TrueFalse.t);
            shapeTypeTextPath.fitshape=(ST_TrueFalse.t);
            CT_Handles handles = shapetype.AddNewHandles();
            CT_H h = handles.AddNewH();
            h.position=("#0,bottomRight");
            h.xrange=("6629,14971");
            NPOI.OpenXmlFormats.Vml.Office.CT_Lock lock1 = shapetype.AddNewLock();
            lock1.ext=(ST_Ext.edit);
            CT_Shape shape = group.AddNewShape();
            shape.id = ("PowerPlusWaterMarkObject" + idx);
            shape.spid = ("_x0000_s102" + (4 + idx));
            shape.type = ("#_x0000_t136");
            shape.style = ("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
            shape.wrapcoords = ("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068");
            shape.fillcolor = ("black");
            shape.stroked = (ST_TrueFalse.@false);
            CT_TextPath shapeTextPath = shape.AddNewTextpath();
            shapeTextPath.style=("font-family:&quot;Cambria&quot;;font-size:1pt");
            shapeTextPath.@string=(text);
            pict.Set(group);
            // end watermark paragraph
            return new XWPFParagraph(p, doc);
        }
Пример #30
0
        /*
         * This is the default Watermark paragraph; the only variable is the text message
         * TODO: manage all the other variables
         */
        private XWPFParagraph GetWatermarkParagraph(String text, int idx)
        {
            CT_P p = new CT_P();

            byte[] rsidr        = doc.Document.body.GetPArray(0).rsidR;
            byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
            p.rsidP        = (rsidr);
            p.rsidRDefault = (rsidrdefault);
            CT_PPr pPr = p.AddNewPPr();

            pPr.AddNewPStyle().val = ("Header");
            // start watermark paragraph
            Npoi.Core.OpenXmlFormats.Wordprocessing.CT_R r = p.AddNewR();
            CT_RPr rPr = r.AddNewRPr();

            rPr.AddNewNoProof();
            CT_Picture pict = r.AddNewPict();

            CT_Group     group     = new CT_Group();
            CT_Shapetype shapetype = group.AddNewShapetype();

            shapetype.id        = ("_x0000_t136");
            shapetype.coordsize = ("1600,21600");
            shapetype.spt       = (136);
            shapetype.adj       = ("10800");
            shapetype.path2     = ("m@7,0l@8,0m@5,21600l@6,21600e");
            CT_Formulas formulas = shapetype.AddNewFormulas();

            formulas.AddNewF().eqn = ("sum #0 0 10800");
            formulas.AddNewF().eqn = ("prod #0 2 1");
            formulas.AddNewF().eqn = ("sum 21600 0 @1");
            formulas.AddNewF().eqn = ("sum 0 0 @2");
            formulas.AddNewF().eqn = ("sum 21600 0 @3");
            formulas.AddNewF().eqn = ("if @0 @3 0");
            formulas.AddNewF().eqn = ("if @0 21600 @1");
            formulas.AddNewF().eqn = ("if @0 0 @2");
            formulas.AddNewF().eqn = ("if @0 @4 21600");
            formulas.AddNewF().eqn = ("mid @5 @6");
            formulas.AddNewF().eqn = ("mid @8 @5");
            formulas.AddNewF().eqn = ("mid @7 @8");
            formulas.AddNewF().eqn = ("mid @6 @7");
            formulas.AddNewF().eqn = ("sum @6 0 @5");
            CT_Path path           = shapetype.AddNewPath();

            path.textpathok    = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.t);
            path.connecttype   = (ST_ConnectType.custom);
            path.connectlocs   = ("@9,0;@10,10800;@11,21600;@12,10800");
            path.connectangles = ("270,180,90,0");
            CT_TextPath shapeTypeTextPath = shapetype.AddNewTextpath();

            shapeTypeTextPath.on       = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.t);
            shapeTypeTextPath.fitshape = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.t);
            CT_Handles handles = shapetype.AddNewHandles();
            CT_H       h       = handles.AddNewH();

            h.position = ("#0,bottomRight");
            h.xrange   = ("6629,14971");
            Npoi.Core.OpenXmlFormats.Vml.Office.CT_Lock lock1 = shapetype.AddNewLock();
            lock1.ext = (ST_Ext.edit);
            CT_Shape shape = group.AddNewShape();

            shape.id         = ("PowerPlusWaterMarkObject" + idx);
            shape.spid       = ("_x0000_s102" + (4 + idx));
            shape.type       = ("#_x0000_t136");
            shape.style      = ("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
            shape.wrapcoords = ("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068");
            shape.fillcolor  = ("black");
            shape.stroked    = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.@false);
            CT_TextPath shapeTextPath = shape.AddNewTextpath();

            shapeTextPath.style   = ("font-family:&quot;Cambria&quot;;font-size:1pt");
            shapeTextPath.@string = (text);
            pict.Set(group);
            // end watermark paragraph
            return(new XWPFParagraph(p, doc));
        }
Пример #31
0
        public void AddTableParagraph(string title, TableModel model, ParagraphOptions paragraphOptions, TextOptions textOptions)
        {
            if (model == null || model.Rows?.Count < 1)
            {
                throw new ArgumentException(nameof(model));
            }

            bool headerExist = model.Headers != null && model.Headers.Count() > 0;

            var rowCount    = model.Rows.Count;
            var columnCount = model.Rows.FirstOrDefault().ColumnValues.Count;

            if (columnCount > MAX_COLUMN_COUNT)
            {
                throw new ArgumentOutOfRangeException("最多只能支持10列");
            }

            if (headerExist)
            {
                rowCount++;
            }

            XWPFTable table = document.CreateTable(rowCount, columnCount);

            table.Width = MAX_TABLE_WIDTH;
            var columnWidth = MAX_TABLE_WIDTH / columnCount;

            for (int i = 0; i < columnCount; i++)
            {
                table.SetColumnWidth(i, (ulong)columnWidth);
            }

            // 表头
            if (headerExist)
            {
                int idxCurrentHeaderColumns = 0;
                foreach (var header in model.Headers)
                {
                    var para = new CT_P();
                    para.AddNewPPr().AddNewTextAlignment(); // 设置单元格文本对齐

                    XWPFParagraph paragraph = new XWPFParagraph(para, table.Body);
                    SetParagraph(paragraph, paragraphOptions);
                    SetText(header, paragraph, textOptions);
                    table.GetRow(0).GetCell(idxCurrentHeaderColumns++).SetParagraph(paragraph);
                }
            }

            // 表格内容
            int idxCurrentRow = 1;

            foreach (var row in model.Rows)
            {
                int idxCurrentHeaderColumns = 0;

                foreach (var columnValue in row.ColumnValues)
                {
                    var para = new CT_P();
                    para.AddNewPPr().AddNewTextAlignment(); // 设置单元格文本对齐

                    XWPFParagraph paragraph = new XWPFParagraph(para, table.Body);
                    SetParagraph(paragraph, paragraphOptions);
                    SetText(columnValue, paragraph, textOptions);
                    table.GetRow(idxCurrentRow).GetCell(idxCurrentHeaderColumns++).SetParagraph(paragraph);
                }
                idxCurrentRow++;
            }
        }