Exemplo n.º 1
0
        /// <summary>
        /// 获取一个新文档
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static CT_SectPr GetNewSectPr(this XWPFDocument doc)
        {
            doc.Document.body.sectPr = new CT_SectPr();
            CT_SectPr sectPr = doc.Document.body.sectPr;

            return(sectPr);
        }
Exemplo n.º 2
0
        public void AppendParagraphText(StringBuilder text, XWPFParagraph paragraph)
        {
            try
            {
                CT_SectPr ctSectPr = null;
                if (paragraph.GetCTP().pPr != null)
                {
                    ctSectPr = paragraph.GetCTP().pPr.sectPr;
                }

                XWPFHeaderFooterPolicy headerFooterPolicy = null;

                if (ctSectPr != null)
                {
                    headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
                    extractHeaders(text, headerFooterPolicy);
                }

                foreach (IRunElement run in paragraph.Runs)
                {
                    text.Append(run.ToString());
                    if (run is XWPFHyperlinkRun && fetchHyperlinks)
                    {
                        XWPFHyperlink link = ((XWPFHyperlinkRun)run).GetHyperlink(document);
                        if (link != null)
                        {
                            text.Append(" <" + link.URL + ">");
                        }
                    }
                }

                // Add comments
                XWPFCommentsDecorator decorator = new XWPFCommentsDecorator(paragraph, null);
                String commentText = decorator.GetCommentText();
                if (commentText.Length > 0)
                {
                    text.Append(commentText).Append('\n');
                }

                // Do endnotes and footnotes
                String footnameText = paragraph.FootnoteText;
                if (footnameText != null && footnameText.Length > 0)
                {
                    text.Append(footnameText + '\n');
                }

                if (ctSectPr != null)
                {
                    extractFooters(text, headerFooterPolicy);
                }
            }
            catch (IOException e)
            {
                throw new POIXMLException(e);
            }
            catch (XmlException e)
            {
                throw new POIXMLException(e);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 设置页面尺寸
        /// </summary>
        /// <param name="setPr"></param>
        /// <param name="pageType"></param>
        public static void SetPageSize(this CT_SectPr setPr, PageType pageType)
        {
            Tuple <ulong, ulong> size = PageSize(pageType);

            setPr.pgSz.w = size.Item1;
            setPr.pgSz.h = size.Item2;
        }
        /// <summary>
        /// Creates an empty header for front pages.
        /// The returned paragraph should be inserted in front of document
        /// </summary>
        /// <example>xwpfDoc.Document.body.Items.Insert(1, CreateFrontHeader());</example>
        /// <returns>CT_P paragraph that contains a blank header</returns>
        private CT_P CreateFrontHeader()
        {
            CT_Hdr header          = new CT_Hdr();
            CT_P   headerParagraph = header.AddNewP();

            headerParagraph.AddNewPPr();

            XWPFHeader documentHeader = (XWPFHeader)newDoc.CreateRelationship(XWPFRelation.HEADER, XWPFFactory.GetInstance(), pageHeaderCount);

            documentHeader.SetHeaderFooter(header);

            // Create new section and set its header
            CT_P      p          = new CT_P();
            CT_SectPr newSection = p.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;

            // Increment page header count so each one gets a unique ID
            pageHeaderCount++;

            return(p);
        }
 public XWPFHeaderFooterPolicy(XWPFDocument doc, CT_SectPr sectPr)
 {
     this.doc = doc;
     for (int i = 0; i < sectPr.SizeOfHeaderReferenceArray(); ++i)
     {
         CT_HdrFtrRef       headerReferenceArray = sectPr.GetHeaderReferenceArray(i);
         POIXMLDocumentPart relationById         = doc.GetRelationById(headerReferenceArray.id);
         XWPFHeader         hdr = (XWPFHeader)null;
         if (relationById != null && relationById is XWPFHeader)
         {
             hdr = (XWPFHeader)relationById;
         }
         ST_HdrFtr type = headerReferenceArray.type;
         this.assignHeader(hdr, type);
     }
     for (int i = 0; i < sectPr.SizeOfFooterReferenceArray(); ++i)
     {
         CT_HdrFtrRef       footerReferenceArray = sectPr.GetFooterReferenceArray(i);
         POIXMLDocumentPart relationById         = doc.GetRelationById(footerReferenceArray.id);
         XWPFFooter         ftr = (XWPFFooter)null;
         if (relationById != null && relationById is XWPFFooter)
         {
             ftr = (XWPFFooter)relationById;
         }
         ST_HdrFtr type = footerReferenceArray.type;
         this.assignFooter(ftr, type);
     }
 }
        public XWPFDocument Render(USFMDocument input)
        {
            UnrenderableMarkers = new List <string>();
            CrossRefMarkers     = new Dictionary <string, Marker>();
            newDoc = new XWPFDocument();

            if (FrontMatter != null)
            {
                RenderFrontMatter(FrontMatter);
            }

            if (configDocx.renderTableOfContents)
            {
                DocumentStylesBuilder.BuildStylesForTOC(newDoc);
                RenderTOC();
            }

            newDoc.CreateFootnotes();

            setStartPageNumber();

            newDoc.ColumnCount = configDocx.columnCount;

            foreach (Marker marker in input.Contents)
            {
                RenderMarker(marker, new StyleConfig());
            }

            // Add section header for final book
            if (previousBookHeader != null)
            {
                createBookHeaders(previousBookHeader);
            }

            // Make final document section continuous so that it doesn't
            // create an extra page at the end.  Final section is unique:
            // it's a direct child of the document, not a child of the last
            // paragraph.
            CT_SectPr finalSection = new CT_SectPr();

            finalSection.type           = new CT_SectType();
            finalSection.type.val       = ST_SectionMark.continuous;
            newDoc.Document.body.sectPr = finalSection;
            finalSection.cols.num       = configDocx.columnCount.ToString();
            CT_PageNumber pageNum = new CT_PageNumber
            {
                fmt = ST_NumberFormat.@decimal
            };

            finalSection.pgNumType = pageNum;

            return(newDoc);
        }
Exemplo n.º 7
0
        private static void word_init(XWPFDocument m_Docx, String prjName)
        {
            //设置页面 将页面设置为A4 纵向
            //参考网站  http://www.aiuxian.com/article/p-1970779.html
            CT_SectPr m_SectPr = new CT_SectPr();

            m_SectPr.pgSz.w             = (ulong)11906;
            m_SectPr.pgSz.h             = (ulong)16838;
            m_Docx.Document.body.sectPr = m_SectPr;

            //第一页 封面
            word_insert_space(5, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, prjName);
            word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统");
            word_insert_text(m_Docx, "宋体", 22, "现场勘查报告");
            word_insert_space(7, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, DateTime.Now.ToString("yyyy年MM月dd日"));
            word_insert_space(7, m_Docx);


            //创建表并获取该表
            XWPFTable table = m_Docx.CreateTable(4, 2);
            CT_Tbl    ctbl  = m_Docx.Document.body.GetTblArray()[0];

            //表居中
            ctbl.AddNewTblPr().jc = new CT_Jc();
            ctbl.AddNewTblPr().jc.val = ST_Jc.center;
            //表宽度为8000
            ctbl.AddNewTblPr().AddNewTblW().w    = "8000";
            ctbl.AddNewTblPr().AddNewTblW().type = ST_TblWidth.dxa;

            //列宽设置
            CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();

            m_Pr.tcW      = new CT_TblWidth();
            m_Pr.tcW.w    = "3000";
            m_Pr.tcW.type = ST_TblWidth.dxa;

            //行高设置


            //设置表中文本
            table.GetRow(0).GetCell(0).SetText("项目");
            table.GetRow(1).GetCell(0).SetText("勘察日期");
            table.GetRow(2).GetCell(0).SetText("现场勘查人员");
            table.GetRow(3).GetCell(0).SetText("报告修正人员");

            word_insert_space(4, m_Docx);
        }
Exemplo n.º 8
0
        private void SaveErrorFile(string strFile, Bitmap img)
        {
            XWPFDocument m_Docx   = new XWPFDocument();
            CT_SectPr    m_SectPr = new CT_SectPr();

            //页面设置A4
            m_SectPr.pgSz.w = (ulong)16838;
            m_SectPr.pgSz.h = (ulong)11906;

            m_Docx.Document.body.sectPr = m_SectPr;

            // 创建段落
            CT_P m_p = m_Docx.Document.body.AddNewP();

            m_p.AddNewPPr().AddNewJc().val = ST_Jc.left;                //段落水平居左
            XWPFParagraph gp = new XWPFParagraph(m_p, m_Docx);          //创建XWPFParagraph

            CT_R m_r = m_p.AddNewR();

            m_r.AddNewT().Value = "记录时间:" + DateTime.Now.ToString("yyyy-MM-dd mm:dd:ss");
            m_r.AddNewCr();
            m_r.AddNewT().Value = "账套公司名称:" + mstrCompanyName;
            m_r.AddNewCr();
            m_r.AddNewT().Value = "按钮名称:" + mstrButtonAction;
            WriteText(m_r, txtMainInfo.Text);
            WriteText(m_r, mstrMoreInfo);

            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                using (Bitmap bmp = GetScreenImage())
                {
                    bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
                }

                stream.Position = 0;

                m_p = m_Docx.Document.body.AddNewP();
                m_p.AddNewPPr().AddNewJc().val = ST_Jc.center;                //段落水平居中
                gp = new XWPFParagraph(m_p, m_Docx);
                XWPFRun gr = gp.CreateRun();
                gr.AddPicture(stream, (int)NPOI.XWPF.UserModel.PictureType.GIF, "1.gif", 360000 * 23, 360000 * 16);
            }

            using (FileStream writer = new FileStream(strFile, FileMode.CreateNew))
            {
                m_Docx.Write(writer);
                writer.Close();
            }
        }
Exemplo n.º 9
0
        private static Size GetPageSize(XWPFDocument doc)
        {
            CT_SectPr sectPr = doc.Document.body.sectPr;

            if (sectPr != null)
            {
                CT_PageSz pgSz = sectPr.pgSz;
                if (pgSz != null)
                {
                    return(new Size((int)(pgSz.w / 20), (int)(pgSz.h / 20)));
                }
            }

            return(new Size(11906 / 20, 16838 / 20));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 生成模板文件
        /// </summary>
        /// <param name="m_Docx">根文档</param>
        private static void word_init(XWPFDocument m_Docx)
        {
            //1‘=1440twip=25.4mm=72pt(磅point)=96px(像素pixel)
            //1px(像素pixel)=0.75pt(磅point)
            // A4:W=11906 twip=8.269''=210mm,h=16838twip=11.693''=297mm
            //A5:W=8390 twip=5.827''=148mm,h=11906 twip=8.269''=210mm
            //A6:W=5953 twip=4.134''=105mm,h=8390twip=5.827''=1148mm
            //16k195mmX270mm:
            //16k184mmX260mm:
            //16k197mmX273mm:
            CT_SectPr m_SectPr = new CT_SectPr();

            //页面设置A4纵向
            m_SectPr.pgSz.w             = (ulong)11906;
            m_SectPr.pgSz.h             = (ulong)16838;
            m_Docx.Document.body.sectPr = m_SectPr;
            //第一页
            word_insert_space(4, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, "【项目名称】");
            word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统");
            word_insert_text(m_Docx, "宋体", 22, "现场勘查报告");

            word_insert_space(8, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, "【日期】");

            word_insert_space(7, m_Docx);
            //第二页
            //表1
            XWPFTable table  = m_Docx.CreateTable(4, 2);
            CT_Tbl    ctbl   = m_Docx.Document.body.GetTblArray()[0];
            CT_TblPr  ctblpr = ctbl.AddNewTblPr();

            ctblpr.jc     = new CT_Jc();
            ctblpr.jc.val = ST_Jc.center;
            table.Width   = 4000;
            table.GetRow(0).GetCell(0).SetText("项目");
            table.GetRow(1).GetCell(0).SetText("日期");
            table.GetRow(2).GetCell(0).SetText("现场勘查人员");
            table.GetRow(3).GetCell(0).SetText("报告编制人员");
            CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();

            m_Pr.tcW      = new CT_TblWidth();
            m_Pr.tcW.w    = "4000";
            m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度
            word_insert_space(2, m_Docx);
            word_insert_text(m_Docx, "宋体", 12, "基站勘察表");
        }
Exemplo n.º 11
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;
        }
Exemplo n.º 12
0
        protected void AddFooter(XWPFDocument document, string text, int textFontSize)
        {
            CT_SectPr secPr  = document.Document.body.sectPr;
            CT_Ftr    footer = new CT_Ftr();
            var       run    = footer.AddNewP().AddNewR();

            run.AddNewT().Value    = text;
            XWPFRelation relation2 = XWPFRelation.FOOTER;
            XWPFFooter   myFooter  = (XWPFFooter)document.CreateRelationship(relation2, XWPFFactory.GetInstance(), document.FooterList.Count + 1);

            myFooter.SetHeaderFooter(footer);
            CT_HdrFtrRef myFooterRef = secPr.AddNewFooterReference();

            myFooterRef.type = ST_HdrFtr.@default;
            myFooterRef.id   = myFooter.GetPackageRelationship().Id;
            myFooter.Paragraphs[0].Runs[0].FontSize = textFontSize != -1 ? textFontSize : 10;
        }
Exemplo n.º 13
0
        private static void CreateHeader(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_Hdr header = new CT_Hdr();
            //header.AddNewP().AddNewR().AddNewT().Value = headerText;
            var hp = header.AddNewP();

            //byte[] data = Encoding.ASCII.GetBytes("00B80FAA");
            hp.rsidR        = new byte[] { 0, 184, 15, 170 };
            hp.rsidRDefault = new byte[] { 0, 146, 87, 240 };
            hp.rsidP        = new byte[] { 0, 184, 15, 170 };

            var ppr  = hp.AddNewPPr();
            var ctjc = new CT_Jc();

            ctjc.val = ST_Jc.right;
            ppr.jc   = ctjc;

            var hr = hp.AddNewR().AddNewT().Value = headerText;

            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader   docHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count);

            docHeader.SetXWPFDocument(doc);
            XWPFFooter footer = new XWPFFooter();

            //设置页眉
            docHeader.SetHeaderFooter(header);
            if (!string.IsNullOrEmpty(imageUrl) && File.Exists(imageUrl))
            {
                var pictureData = new FileStream(imageUrl, FileMode.Open, FileAccess.Read);
                docHeader.AddPictureData(pictureData, (int)PictureType.PNG);
            }
            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = docHeader.GetPackageRelationship().Id;
        }
Exemplo n.º 14
0
        public V5XWPFHeader(XWPFDocument doc)
        {
            if (doc.Document.body.sectPr == null)
            {
                doc.Document.body.sectPr = new CT_SectPr();
            }
            CT_SectPr m_SectPr = doc.Document.body.sectPr;

            XWPFRelation Hrelation = XWPFRelation.HEADER;

            XWPFHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count);
            XWPFHeader.SetXWPFDocument(doc);
            XWPFHeader.SetHeaderFooter(header);

            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = XWPFHeader.GetPackageRelationship().Id;
        }
Exemplo n.º 15
0
        /// <summary>
        /// 创建文档
        /// </summary>
        public void Create()
        {
            //readWord();
            XWPFDocument doc = new XWPFDocument();

            //设置业内边距
            doc.Document.body.sectPr = new CT_SectPr();
            CT_SectPr m_sectpr = doc.Document.body.sectPr;

            m_sectpr.pgMar.top    = "2265"; //上40mm
            m_sectpr.pgMar.bottom = "1985"; //下350mm
            m_sectpr.pgMar.left   = 1417;   //左25mm
            m_sectpr.pgMar.right  = 1133;   //右20mm


            CreateHomePage(doc);    //首页
            CreateProjectData(doc); //项目信息
            //CreatePageHeaderFooter(doc,m_sectpr);//页眉页脚
            CreateChaptersAndSections(doc);

            //生成Word文件,保存对话框
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            //设置文件类型
            saveFileDialog.Filter = "Microsoft Word文件(*.docx)|*.docx";
            //保存对话框是否记忆上次打开的目录
            saveFileDialog.RestoreDirectory = true;
            //设置默认的文件名称
            saveFileDialog.FileName = "施工说明书(组合)";
            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            //获取文件路径
            string     flieName = saveFileDialog.FileName.ToString();
            FileStream out1     = new FileStream(flieName, FileMode.Create);

            doc.Write(out1);
            out1.Close();
        }
Exemplo n.º 16
0
 /**
  * Figures out the policy for the given document,
  *  and Creates any header and footer objects
  *  as required.
  */
 public XWPFHeaderFooterPolicy(XWPFDocument doc, CT_SectPr sectPr)
 {
     // Grab what headers and footers have been defined
     // For now, we don't care about different ranges, as it
     //  doesn't seem that .docx properly supports that
     //  feature of the file format yet
     this.doc = doc;
     for (int i = 0; i < sectPr.SizeOfHeaderReferenceArray(); i++)
     {
         // Get the header
         CT_HdrFtrRef ref1 = sectPr.GetHeaderReferenceArray(i);
         POIXMLDocumentPart relatedPart = doc.GetRelationById(ref1.id);
         XWPFHeader hdr = null;
         if (relatedPart != null && relatedPart is XWPFHeader)
         {
             hdr = (XWPFHeader)relatedPart;
         }
         // Assign it
         ST_HdrFtr type = ref1.type;
         assignHeader(hdr, type);
     }
     for (int i = 0; i < sectPr.SizeOfFooterReferenceArray(); i++)
     {
         // Get the footer
         CT_HdrFtrRef ref1 = sectPr.GetFooterReferenceArray(i);
         POIXMLDocumentPart relatedPart = doc.GetRelationById(ref1.id);
         XWPFFooter ftr = null;
         if (relatedPart != null && relatedPart is XWPFFooter)
         {
             ftr = (XWPFFooter)relatedPart;
         }
         // Assign it
         ST_HdrFtr type = ref1.type;
         assignFooter(ftr, type);
     }
 }
Exemplo n.º 17
0
 /**
  * Figures out the policy for the given document,
  *  and Creates any header and footer objects
  *  as required.
  */
 public XWPFHeaderFooterPolicy(XWPFDocument doc, CT_SectPr sectPr)
 {
     // Grab what headers and footers have been defined
     // For now, we don't care about different ranges, as it
     //  doesn't seem that .docx properly supports that
     //  feature of the file format yet
     this.doc = doc;
     for (int i = 0; i < sectPr.SizeOfHeaderReferenceArray(); i++)
     {
         // Get the header
         CT_HdrFtrRef       ref1        = sectPr.GetHeaderReferenceArray(i);
         POIXMLDocumentPart relatedPart = doc.GetRelationById(ref1.id);
         XWPFHeader         hdr         = null;
         if (relatedPart != null && relatedPart is XWPFHeader)
         {
             hdr = (XWPFHeader)relatedPart;
         }
         // Assign it
         ST_HdrFtr type = ref1.type;
         assignHeader(hdr, type);
     }
     for (int i = 0; i < sectPr.SizeOfFooterReferenceArray(); i++)
     {
         // Get the footer
         CT_HdrFtrRef       ref1        = sectPr.GetFooterReferenceArray(i);
         POIXMLDocumentPart relatedPart = doc.GetRelationById(ref1.id);
         XWPFFooter         ftr         = null;
         if (relatedPart != null && relatedPart is XWPFFooter)
         {
             ftr = (XWPFFooter)relatedPart;
         }
         // Assign it
         ST_HdrFtr type = ref1.type;
         assignFooter(ftr, type);
     }
 }
Exemplo n.º 18
0
        public void WordCreate(string filePath, string content)
        {
            XWPFDocument MyDoc = new XWPFDocument();

            CT_SectPr m_SectPr = new CT_SectPr();  //实例一个尺寸类的实例

            m_SectPr.pgSz.w            = 16838;    //设置宽度(这里是一个ulong类型)
            m_SectPr.pgSz.h            = 11906;    //设置高度(这里是一个ulong类型)
            MyDoc.Document.body.sectPr = m_SectPr; //设置页面的尺;
            var data = MyDoc.CreateParagraph();

            // 向新文档中添加段落

            data.Alignment = ParagraphAlignment.CENTER;
            // 向该段落中添加文字
            XWPFRun titleRun = data.CreateRun();

            titleRun.SetText(content);
            //处理doc后,生成新的文件,写入doc ,生成word完成。
            FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write);

            MyDoc.Write(file);
            file.Close();
        }
        /// <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++;
        }
 public OpenXmlSectionPropertiesModel()
 {
     this._sectionPr = new CT_SectPr();
 }
Exemplo n.º 21
0
 public CT_Body()
 {
     this.sectPrField           = new CT_SectPr();
     this.itemsElementNameField = new List <DocumentBodyItemChoiceType>();
     this.itemsField            = new List <object>();
 }
Exemplo n.º 22
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();
    }
Exemplo n.º 23
0
        /// <summary>
        /// 创建首页
        /// </summary>
        /// <param name="doc"></param>
        private void CreateHomePage(XWPFDocument doc)
        {
            CT_SectPr cT_Sect = new CT_SectPr();

            cT_Sect.pgMar.top    = "1700"; //上30mm
            cT_Sect.pgMar.bottom = "1700"; //下30mm
                                           //cT_Sect.pgMar.left = 1417;//左25mm
                                           //cT_Sect.pgMar.right = 1133;//右20mm


            XWPFParagraph p1 = doc.CreateParagraph();

            p1.Alignment = ParagraphAlignment.BOTH;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            XWPFRun r1 = p1.CreateRun();

            r1.SetText("图号:S123456S-D0101-01                       电力行业(送电、变电)专业甲级");
            r1.FontFamily = "宋体";
            r1.FontSize   = 12;

            p1           = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.BOTH;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            r1 = p1.CreateRun();
            r1.SetText("版本:A	                                    	 勘测设计证号:A144000587");
            r1.FontFamily = "宋体";
            r1.FontSize   = 12;


            for (int i = 0; i < 4; i++)
            {
                p1           = doc.CreateParagraph();
                p1.Alignment = ParagraphAlignment.CENTER;
                p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            }

            p1           = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.CENTER;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            r1 = p1.CreateRun();
            r1.SetText("110kV");
            r1.FontFamily = "Times New Roman";
            r1.FontSize   = 22;
            r1            = p1.CreateRun();
            r1.SetText("濂泉送电线路工程");
            r1.FontFamily = "宋体";
            r1.FontSize   = 22;

            p1           = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.CENTER;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);

            p1           = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.CENTER;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            r1 = p1.CreateRun();
            r1.SetText("施工图设计说明书");
            r1.FontFamily = "宋体";
            r1.FontSize   = 18;

            for (int i = 0; i < 14; i++)
            {
                p1           = doc.CreateParagraph();
                p1.Alignment = ParagraphAlignment.CENTER;
                p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            }
            p1           = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.CENTER;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            r1 = p1.CreateRun();
            r1.SetText("广 州 电 力 设 计 院 有 限 公 司");
            r1.FontFamily = "宋体";
            r1.FontSize   = 14;

            p1           = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.CENTER;
            p1.setSpacingBetween(1.5, LineSpacingRule.AUTO);
            r1 = p1.CreateRun();
            r1.SetText(DateTime.Today.Year + "年" + DateTime.Today.Month + "月  广州");
            r1.FontFamily = "宋体";
            r1.FontSize   = 14;

            p1.CreateRun().AddBreak(BreakType.COLUMN);//插入空白页
        }
Exemplo n.º 24
0
        protected void Button1_Click(object sender, EventArgs e)
        {
            //图片位置
            String       m_PicPath = @"C:\ttt.jpeg";
            FileStream   gfs       = null;
            MemoryStream ms        = new MemoryStream();
            XWPFDocument m_Docx    = new XWPFDocument();

            //页面设置
            //A4:W=11906,h=16838
            //CT_SectPr m_SectPr = m_Docx.Document.body.AddNewSectPr();
            m_Docx.Document.body.sectPr = new CT_SectPr();
            CT_SectPr m_SectPr = m_Docx.Document.body.sectPr;

            //页面设置A4纵向
            m_SectPr.pgSz.h = (ulong)16838;
            m_SectPr.pgSz.w = (ulong)11906;
            XWPFParagraph gp = m_Docx.CreateParagraph();
            // gp.GetCTPPr().AddNewJc().val = ST_Jc.center; //水平居中
            XWPFRun gr = gp.CreateRun();

            gr.GetCTR().AddNewRPr().AddNewRFonts().ascii    = "黑体";
            gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体";
            gr.GetCTR().AddNewRPr().AddNewRFonts().hint     = ST_Hint.eastAsia;
            gr.GetCTR().AddNewRPr().AddNewSz().val          = (ulong)44; //2号字体
            gr.GetCTR().AddNewRPr().AddNewSzCs().val        = (ulong)44;
            gr.GetCTR().AddNewRPr().AddNewB().val           = true;      //加粗
            gr.GetCTR().AddNewRPr().AddNewColor().val       = "red";     //字体颜色
            gr.SetText("NPOI创建Word2007Docx");
            gp = m_Docx.CreateParagraph();
            //gp.GetCTPPr().AddNewJc().val = ST_Jc.both;
            // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
            gp.IndentationFirstLine = 15;
            gr = gp.CreateRun();
            CT_RPr   rpr    = gr.GetCTR().AddNewRPr();
            CT_Fonts rfonts = rpr.AddNewRFonts();

            rfonts.ascii    = "宋体";
            rfonts.eastAsia = "宋体";
            rpr.AddNewSz().val   = (ulong)21;//5号字体
            rpr.AddNewSzCs().val = (ulong)21;
            gr.SetText("NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够帮助开 发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等 。目前POI的稳定版本中支持Excel文件格式(xls和xlsx),其他的都属于不稳定版本(放在poi的scrachpad目录 中)。");
            //创建表
            XWPFTable table     = m_Docx.CreateTable(1, 4);              //创建一行4列表
            CT_Tbl    m_CTTbl   = m_Docx.Document.body.GetTblArray()[0]; //获得文档第一张表
            CT_TblPr  m_CTTblPr = m_CTTbl.AddNewTblPr();

            m_CTTblPr.AddNewTblW().w    = "2000";              //表宽
            m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa;
            m_CTTblPr.tblpPr               = new CT_TblPPr();  //表定位
            m_CTTblPr.tblpPr.tblpX         = "4003";           //表左上角坐标
            m_CTTblPr.tblpPr.tblpY         = "365";
            m_CTTblPr.tblpPr.tblpXSpec     = ST_XAlign.center; //若不为“Null”,则优先tblpX,即表由tblpXSpec定位
            m_CTTblPr.tblpPr.tblpYSpec     = ST_YAlign.center; //若不为“Null”,则优先tblpY,即表由tblpYSpec定位
            m_CTTblPr.tblpPr.leftFromText  = (ulong)180;
            m_CTTblPr.tblpPr.rightFromText = (ulong)180;
            m_CTTblPr.tblpPr.vertAnchor    = ST_VAnchor.text;
            m_CTTblPr.tblpPr.horzAnchor    = ST_HAnchor.page;


            //表1行4列充值:a,b,c,d
            table.GetRow(0).GetCell(0).SetText("a");
            table.GetRow(0).GetCell(1).SetText("b");
            table.GetRow(0).GetCell(2).SetText("c");
            table.GetRow(0).GetCell(3).SetText("d");
            CT_Row       m_NewRow = new CT_Row();//创建1行
            XWPFTableRow m_Row    = new XWPFTableRow(m_NewRow, table);

            table.AddRow(m_Row);                     //必须要!!!
            XWPFTableCell cell = m_Row.CreateCell(); //创建单元格,也创建了一个CT_P
            CT_Tc         cttc = cell.GetCTTc();
            CT_TcPr       ctPr = cttc.AddNewTcPr();

            //ctPr.gridSpan.val = "3";//合并3列
            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
            cttc.GetPList()[0].AddNewR().AddNewT().Value  = "666";
            cell = m_Row.CreateCell();//创建单元格,也创建了一个CT_P
            cell.GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
            cell.GetCTTc().GetPList()[0].AddNewR().AddNewT().Value  = "e";
            //合并3列,合并2行
            //1行
            m_NewRow = new CT_Row();
            m_Row    = new XWPFTableRow(m_NewRow, table);
            table.AddRow(m_Row);
            cell = m_Row.CreateCell(); //第1单元格
            cell.SetText("f");
            cell = m_Row.CreateCell(); //从第2单元格开始合并
            cttc = cell.GetCTTc();
            ctPr = cttc.AddNewTcPr();
            // ctPr.gridSpan.val = "3";//合并3列
            ctPr.AddNewVMerge().val = ST_Merge.restart;     //开始合并行
            ctPr.AddNewVAlign().val = ST_VerticalJc.center; //垂直居中
            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
            cttc.GetPList()[0].AddNewR().AddNewT().Value  = "777";
            //2行
            m_NewRow = new CT_Row();
            m_Row    = new XWPFTableRow(m_NewRow, table);
            table.AddRow(m_Row);
            cell = m_Row.CreateCell(); //第1单元格
            cell.SetText("g");
            cell = m_Row.CreateCell(); //第2单元格
            cttc = cell.GetCTTc();
            ctPr = cttc.AddNewTcPr();
            // ctPr.gridSpan.val = "3";//合并3列
            ctPr.AddNewVMerge().val = ST_Merge.@continue;//继续合并行


            //表插入图片
            m_NewRow = new CT_Row();
            m_Row    = new XWPFTableRow(m_NewRow, table);
            table.AddRow(m_Row);
            cell = m_Row.CreateCell();//第1单元格
            //inline方式插入图片
            //gp = table.GetRow(table.Rows.Count - 1).GetCell(0).GetParagraph(table.GetRow(table.Rows.Count - 1).GetCell(0).GetCTTc().GetPList()[0]);//获得指定表单元格的段
            gp  = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr  = gp.CreateRun();                                              //创建run
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);   //读取图片文件
            gr.AddPicture(gfs, (int)PictureType.PNG, "1.jpg", 500000, 500000); //插入图片
            gfs.Close();
            //Anchor方式插入图片
            CT_Anchor an = new CT_Anchor();

            an.distB          = (uint)(0);
            an.distL          = 114300u;
            an.distR          = 114300U;
            an.distT          = 0U;
            an.relativeHeight = 251658240u;
            an.behindDoc      = false; //"0"
            an.locked         = false; //"0"
            an.layoutInCell   = true;  //"1"
            an.allowOverlap   = true;  //"1"


            NPOI.OpenXmlFormats.Dml.CT_Point2D simplePos = new NPOI.OpenXmlFormats.Dml.CT_Point2D();
            simplePos.x = (long)0;
            simplePos.y = (long)0;
            CT_EffectExtent effectExtent = new CT_EffectExtent();

            effectExtent.b = 0L;
            effectExtent.l = 0L;
            effectExtent.r = 0L;
            effectExtent.t = 0L;
            //wrapSquare(四周)
            cell = m_Row.CreateCell(); //第2单元格
            gp   = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr   = gp.CreateRun();     //创建run
            CT_WrapSquare wrapSquare = new CT_WrapSquare();

            wrapSquare.wrapText = ST_WrapText.bothSides;
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件
            // gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();
            //wrapTight(紧密)
            cell = m_Row.CreateCell(); //第3单元格
            gp   = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr   = gp.CreateRun();     //创建run
            CT_WrapTight wrapTight = new CT_WrapTight();

            wrapTight.wrapText            = ST_WrapText.bothSides;
            wrapTight.wrapPolygon         = new CT_WrapPath();
            wrapTight.wrapPolygon.edited  = false;
            wrapTight.wrapPolygon.start   = new CT_Point2D();
            wrapTight.wrapPolygon.start.x = 0;
            wrapTight.wrapPolygon.start.y = 0;
            CT_Point2D lineTo = new CT_Point2D();

            wrapTight.wrapPolygon.lineTo = new List <CT_Point2D>();
            lineTo   = new CT_Point2D();
            lineTo.x = 0;
            lineTo.y = 1343;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 1343;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 0;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            lineTo.x = 0;
            lineTo.y = 0;
            wrapTight.wrapPolygon.lineTo.Add(lineTo);
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件
            //gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapTight, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();
            //wrapThrough(穿越)
            cell = m_Row.CreateCell();                                        //第4单元格
            gp   = cell.GetParagraph(cell.GetCTTc().GetPList()[0]);
            gr   = gp.CreateRun();                                            //创建run
            gfs  = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read); //读取图片文件
            CT_WrapThrough wrapThrough = new CT_WrapThrough();

            wrapThrough.wrapText            = ST_WrapText.bothSides;
            wrapThrough.wrapPolygon         = new CT_WrapPath();
            wrapThrough.wrapPolygon.edited  = false;
            wrapThrough.wrapPolygon.start   = new CT_Point2D();
            wrapThrough.wrapPolygon.start.x = 0;
            wrapThrough.wrapPolygon.start.y = 0;
            lineTo = new CT_Point2D();
            wrapThrough.wrapPolygon.lineTo = new List <CT_Point2D>();
            lineTo   = new CT_Point2D();
            lineTo.x = 0;
            lineTo.y = 1343;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 1343;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            lineTo   = new CT_Point2D();
            lineTo.x = 21405;
            lineTo.y = 0;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            lineTo.x = 0;
            lineTo.y = 0;
            wrapThrough.wrapPolygon.lineTo.Add(lineTo);
            // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 0, 0, wrapThrough, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();


            gp = m_Docx.CreateParagraph();
            //gp.GetCTPPr().AddNewJc().val = ST_Jc.both;
            // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
            gp.IndentationFirstLine = 15;
            gr = gp.CreateRun();
            gr.SetText("NPOI是POI项目的.NET版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。使用NPOI你就可以在没有安装Office或者相应环境的机器上对WORD/EXCEL文档进行读写。NPOI是构建在POI3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。");
            gp = m_Docx.CreateParagraph();
            // gp.GetCTPPr().AddNewJc().val = ST_Jc.both;
            //gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符
            gp.IndentationFirstLine = 15;
            gr = gp.CreateRun();
            gr.SetText("NPOI之所以强大,并不是因为它支持导出Excel,而是因为它支持导入Excel,并能“理解”OLE2文档结构,这也是其他一些Excel读写库比较弱的方面。通常,读入并理解结构远比导出来得复杂,因为导入你必须假设一切情况都是可能的,而生成你只要保证满足你自己需求就可以了,如果把导入需求和生成需求比做两个集合,那么生成需求通常都是导入需求的子集。");
            //在本段中插图-wrapSquare
            //gr = gp.CreateRun();//创建run
            wrapSquare          = new CT_WrapSquare();
            wrapSquare.wrapText = ST_WrapText.bothSides;
            gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件
            // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 900000, 200000, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent);
            gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000);
            gfs.Close();


            m_Docx.Write(ms);
            ms.Flush();
            SaveToFile(ms, Path.GetPathRoot(Directory.GetCurrentDirectory()) + "\\NPOI.docx");
        }
Exemplo n.º 25
0
        /// <summary>
        /// 生成模板文件
        /// </summary>
        /// <param name="m_Docx">根文档</param>
        private static void word_init(XWPFDocument m_Docx)
        {
            //1‘=1440twip=25.4mm=72pt(磅point)=96px(像素pixel)
            //1px(像素pixel)=0.75pt(磅point)
            // A4:W=11906 twip=8.269''=210mm,h=16838twip=11.693''=297mm
            //A5:W=8390 twip=5.827''=148mm,h=11906 twip=8.269''=210mm
            //A6:W=5953 twip=4.134''=105mm,h=8390twip=5.827''=1148mm
            //16k195mmX270mm:
            //16k184mmX260mm:
            //16k197mmX273mm:
            CT_SectPr m_SectPr = new CT_SectPr();
            //页面设置A4纵向
            m_SectPr.pgSz.w = (ulong) 11906;
            m_SectPr.pgSz.h = (ulong) 16838;
            m_Docx.Document.body.sectPr = m_SectPr;
            //第一页
            word_insert_space(4, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, "【项目名称】");
            word_insert_text(m_Docx, "宋体", 22, "GSM-R 通信系统");
            word_insert_text(m_Docx, "宋体", 22, "现场勘查报告");

            word_insert_space(8, m_Docx);
            word_insert_text(m_Docx, "宋体", 22, "【日期】");

            word_insert_space(7, m_Docx);
            //第二页
            //表1
            XWPFTable table = m_Docx.CreateTable(4, 2);
            CT_Tbl ctbl = m_Docx.Document.body.GetTblArray()[0];
            CT_TblPr ctblpr = ctbl.AddNewTblPr();
            ctblpr.jc = new CT_Jc();
            ctblpr.jc.val = ST_Jc.center;
            table.Width = 4000;
            table.GetRow(0).GetCell(0).SetText("项目");
            table.GetRow(1).GetCell(0).SetText("日期");
            table.GetRow(2).GetCell(0).SetText("现场勘查人员");
            table.GetRow(3).GetCell(0).SetText("报告编制人员");
            CT_TcPr m_Pr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr();
            m_Pr.tcW = new CT_TblWidth();
            m_Pr.tcW.w = "4000";
            m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度
            word_insert_space(2, m_Docx);
            word_insert_text(m_Docx, "宋体", 12, "基站勘察表");
        }
Exemplo n.º 26
0
        public static CT_Body Parse(XmlNode node, XmlNamespaceManager namespaceManager)
        {
            if (node == null)
            {
                return(null);
            }
            CT_Body ctObj = new CT_Body();

            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (childNode.LocalName == "moveTo")
                {
                    ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveTo);
                }
                else if (childNode.LocalName == "sectPr")
                {
                    ctObj.sectPr = CT_SectPr.Parse(childNode, namespaceManager);
                }
                else if (childNode.LocalName == "oMathPara")
                {
                    ctObj.Items.Add(CT_OMathPara.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.oMathPara);
                }
                else if (childNode.LocalName == "customXml")
                {
                    ctObj.Items.Add(CT_CustomXmlBlock.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXml);
                }
                else if (childNode.LocalName == "oMath")
                {
                    ctObj.Items.Add(CT_OMath.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.oMath);
                }
                else if (childNode.LocalName == "altChunk")
                {
                    ctObj.Items.Add(CT_AltChunk.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.altChunk);
                }
                else if (childNode.LocalName == "bookmarkEnd")
                {
                    ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.bookmarkEnd);
                }
                else if (childNode.LocalName == "bookmarkStart")
                {
                    ctObj.Items.Add(CT_Bookmark.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.bookmarkStart);
                }
                else if (childNode.LocalName == "commentRangeEnd")
                {
                    ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.commentRangeEnd);
                }
                else if (childNode.LocalName == "commentRangeStart")
                {
                    ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.commentRangeStart);
                }
                else if (childNode.LocalName == "customXmlDelRangeEnd")
                {
                    ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlDelRangeEnd);
                }
                else if (childNode.LocalName == "customXmlDelRangeStart")
                {
                    ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlDelRangeStart);
                }
                else if (childNode.LocalName == "customXmlInsRangeEnd")
                {
                    ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlInsRangeEnd);
                }
                else if (childNode.LocalName == "customXmlInsRangeStart")
                {
                    ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlInsRangeStart);
                }
                else if (childNode.LocalName == "customXmlMoveFromRangeEnd")
                {
                    ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveFromRangeEnd);
                }
                else if (childNode.LocalName == "customXmlMoveFromRangeStart")
                {
                    ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveFromRangeStart);
                }
                else if (childNode.LocalName == "customXmlMoveToRangeEnd")
                {
                    ctObj.Items.Add(CT_Markup.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveToRangeEnd);
                }
                else if (childNode.LocalName == "customXmlMoveToRangeStart")
                {
                    ctObj.Items.Add(CT_TrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.customXmlMoveToRangeStart);
                }
                else if (childNode.LocalName == "del")
                {
                    ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.del);
                }
                else if (childNode.LocalName == "ins")
                {
                    ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.ins);
                }
                else if (childNode.LocalName == "moveFrom")
                {
                    ctObj.Items.Add(CT_RunTrackChange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveFrom);
                }
                else if (childNode.LocalName == "moveFromRangeEnd")
                {
                    ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveFromRangeEnd);
                }
                else if (childNode.LocalName == "moveFromRangeStart")
                {
                    ctObj.Items.Add(CT_MoveBookmark.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveFromRangeStart);
                }
                else if (childNode.LocalName == "moveToRangeEnd")
                {
                    ctObj.Items.Add(CT_MarkupRange.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveToRangeEnd);
                }
                else if (childNode.LocalName == "moveToRangeStart")
                {
                    ctObj.Items.Add(CT_MoveBookmark.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.moveToRangeStart);
                }
                else if (childNode.LocalName == "p")
                {
                    ctObj.Items.Add(CT_P.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.p);
                }
                else if (childNode.LocalName == "permEnd")
                {
                    ctObj.Items.Add(CT_Perm.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.permEnd);
                }
                else if (childNode.LocalName == "permStart")
                {
                    ctObj.Items.Add(CT_PermStart.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.permStart);
                }
                else if (childNode.LocalName == "proofErr")
                {
                    ctObj.Items.Add(CT_ProofErr.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.proofErr);
                }
                else if (childNode.LocalName == "sdt")
                {
                    ctObj.Items.Add(CT_SdtBlock.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.sdt);
                }
                else if (childNode.LocalName == "tbl")
                {
                    ctObj.Items.Add(CT_Tbl.Parse(childNode, namespaceManager));
                    ctObj.ItemsElementName.Add(DocumentBodyItemChoiceType.tbl);
                }
            }
            return(ctObj);
        }
Exemplo n.º 27
0
        /// <summary>
        /// 创建文档
        /// </summary>
        /// <param name="setting"></param>
        public static void ExportDocument(List <Table> listTable, List <TableMapping> listMapping, string fileName)
        {
            XWPFDocument doc = new XWPFDocument(NPOI.OpenXml4Net.OPC.OPCPackage.Open("12345.docx"));

            MemoryStream    ms      = new MemoryStream();
            DocumentSetting setting = new DocumentSetting();

            int docItemsCount = doc.BodyElements.Count;

            for (int i = 0; i < docItemsCount; i++)
            {
                doc.RemoveBodyElement(0);
            }

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

            setPr.pgSz.w = (ulong)size.Item1;
            setPr.pgSz.h = (ulong)size.Item2;

            int tableNum = 0;

            foreach (var table in listTable)
            {
                tableNum++;

                string tableTitle = table.TableName;
                if (!string.IsNullOrEmpty(table.TableComment))
                {
                    tableTitle = $"{table.TableComment} {tableTitle}";
                }

                Console.WriteLine($"正在处理 {tableNum}.{tableTitle}");

                //创建一个段落
                XWPFParagraph gp = doc.CreateParagraph();

                XWPFStyles styles = doc.CreateStyles();

                //表格段落标题
                gp.SetNumID($"2.1");
                gp.Style = "Heading2";
                XWPFRun gr = gp.CreateRun();
                gr.SetText($"{tableTitle}");

                //获取表字段信息
                var listTableSchema = GetTableSchema <TableSchema>(MysqlGetTableSchemaSQL(table.TableName));

                //创建表格
                XWPFTable docTable = doc.CreateTable(listTableSchema.Count + 1, listMapping.Count);
                int       i        = 0;
                foreach (var tableMapping in listMapping)
                {
                    var currentCell = docTable.GetRow(0).GetCell(i);
                    currentCell = SetCell(currentCell, tableMapping.DocumentColumn, tableMapping.ColumnWidth, "CCCCCC", true, 10, true);
                    //var ppr = currentCell.GetCTTc().AddNewP().AddNewPPr();
                    //space.line="0";
                    //space.lineRule = ST_LineSpacingRule.auto;
                    i++;
                }
                int rowIndex = 1;
                foreach (var tableSchema in listTableSchema)
                {
                    int cellIndex = 0;
                    foreach (var tableMapping in listMapping)
                    {
                        var    currentCell = docTable.GetRow(rowIndex).GetCell(cellIndex);
                        string color       = string.Empty;
                        string text        = string.Empty;
                        if (rowIndex % 2 == 0)
                        {
                            color = "DDDDDD";
                        }

                        if (string.IsNullOrEmpty(tableMapping.DBColumn))
                        {
                            text = rowIndex.ToString();
                        }
                        else
                        {
                            text = GetPropValue(tableSchema, tableMapping.DBColumn);
                        }

                        currentCell = SetCell(currentCell, text, tableMapping.ColumnWidth, color, tableMapping.IsCenter, 10);

                        cellIndex++;
                    }
                    rowIndex++;
                }
            }

            //开始写入
            doc.Write(ms);

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                byte[] data = ms.ToArray();
                fs.Write(data, 0, data.Length);
                fs.Flush();
            }
            ms.Close();
        }
Exemplo n.º 28
0
        public void Write()
        {
            XWPFDocument mydoc = new XWPFDocument();

            //设置页面大小
            CT_SectPr size = new CT_SectPr();

            size.pgSz.w = 11900;
            size.pgSz.h = 16830;
            mydoc.Document.body.sectPr = size;

            //创建段落
            XWPFParagraph p1 = mydoc.CreateParagraph();

            //设置段落样式
            p1.Alignment = ParagraphAlignment.CENTER;//居中
            XWPFRun run1 = p1.CreateRun();
            XWPFRun run3 = p1.CreateRun();

            run3.SetText("测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试");
            run1.FontSize = 16;       //设置字体大小
            run1.SetColor("#808080"); //设置字体颜色
            run1.SetText("标题文本标题文本标题文本标题文本标题文本");

            //创建段落
            XWPFParagraph p2 = mydoc.CreateParagraph();

            p2.Alignment = ParagraphAlignment.CENTER;//居中
            XWPFRun run2 = p2.CreateRun();

            run2.SetColor("#123456");
            run2.FontSize = 8;
            run2.SetText("时间:" + DateTime.Now.ToString());

            //获取数据
            DataSet   set   = GetData();
            DataTable table = set.Tables[0];
            //创建表单
            XWPFTable table1 = mydoc.CreateTable(table.Rows.Count + 1, table.Columns.Count);

            //设置第一行表格
            XWPFTableCell firstcell1 = table1.GetRow(0).GetCell(0);

            //XWPFRun firstcell1 = t1.CreateRun();
            firstcell1.SetColor("#FF0000");
            firstcell1.SetText("name1");

            XWPFTableCell firstcell2 = table1.GetRow(0).GetCell(1);

            //XWPFRun firstcell2 = t2.CreateRun();
            firstcell2.SetColor("#FF0000");
            firstcell2.SetText("name2");

            XWPFTableCell firstcell3 = table1.GetRow(0).GetCell(2);

            //XWPFRun firstcell3 = t3.CreateRun();
            firstcell3.SetColor("#FF0000");
            firstcell3.SetText("id");

            /**
             * 这些单元格默认换行
             */
            for (int row = 0; row < table.Rows.Count; row++)
            {
                for (int colum = 0; colum < table.Columns.Count; colum++)
                {
                    table1.GetRow(row + 1).Height = 10;
                    XWPFParagraph t   = table1.GetRow(row + 1).GetCell(colum).AddParagraph();
                    XWPFRun       rIO = t.CreateRun();
                    rIO.FontSize = 5;
                    rIO.IsBold   = true;
                    rIO.SetText(table.Rows[row][colum].ToString());
                }
            }

            //行合并
            //table1.GetRow(2).MergeCells(0, 1);


            FileStream stream = new FileStream(filename, FileMode.Create);

            mydoc.Write(stream);
            stream.Close();
            mydoc.Close();
        }
Exemplo n.º 29
0
 public void AddNewSectPr()
 {
     this.sectPrField = new CT_SectPr();
     //return this.sectPrField;
 }
 internal OpenXmlSectionPropertiesModel()
 {
     _sectionPr = new CT_SectPr();
 }
Exemplo n.º 31
0
        private static void CreateWaterMark(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_Hdr header = new CT_Hdr();
            //header.AddNewP().AddNewR().AddNewT().Value = headerText;
            var paragraph = header.AddNewP();

            var ppr = paragraph.AddNewPPr();

            ppr.AddNewPStyle().val = "a3";
            ppr.AddNewJc().val     = ST_Jc.right;

            var r = paragraph.AddNewR();

            r.AddNewRPr().AddNewNoProof();

            CT_Picture pict = r.AddNewPict();

            CT_Shapetype shapetype = new CT_Shapetype();

            shapetype.id        = ("_x0000_t136");
            shapetype.coordsize = ("21600,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.OpenXmlFormats.Vml.ST_TrueFalse.t);
            path.connecttype   = (NPOI.OpenXmlFormats.Vml.Office.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.OpenXmlFormats.Vml.ST_TrueFalse.t);
            shapeTypeTextPath.fitshape = (NPOI.OpenXmlFormats.Vml.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);
            lock1.text      = NPOI.OpenXmlFormats.Vml.Office.ST_TrueFalse.t;
            lock1.shapetype = NPOI.OpenXmlFormats.Vml.Office.ST_TrueFalse.t;

            CT_Shape shape = new CT_Shape();

            shape.id    = ("PowerPlusWaterMarkObject" + "1109904797");
            shape.spid  = ("_x0000_s206" + (4 + "1"));
            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 = ("#ed7d31 [3205]");
            shape.stroked   = (NPOI.OpenXmlFormats.Vml.ST_TrueFalse.@false);

            shape.AddNewFill().opacity = ".5";
            CT_TextPath shapeTextPath  = shape.AddNewTextpath();

            shapeTextPath.style   = ("font-family:&quotYu Gothic UI&quot");
            shapeTextPath.@string = ("吴润杰水印");

            // pict.Items.Add(shapetype);
            //pict.Set(shapetype);
            pict.Set(shape);
            //pict.Set(group);


            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader   docHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count);

            docHeader.SetXWPFDocument(doc);
            //设置页眉
            docHeader.SetHeaderFooter(header);
            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = docHeader.GetPackageRelationship().Id;
        }
    private void FlightPlanDataExport(List <string> planlist)
    {
        AjaxResult result = new AjaxResult();

        result.IsSuccess = true;
        XWPFDocument doc      = new XWPFDocument();
        CT_SectPr    m_SectPr = new CT_SectPr();

        //设置宽度(这里是一个ulong类型)
        m_SectPr.pgSz.w = 16838;
        //设置高度(这里是一个ulong类型)
        m_SectPr.pgSz.h       = 11906;
        m_SectPr.pgMar.left   = (ulong)300; //左边距
        m_SectPr.pgMar.right  = (ulong)300; //右边距
        m_SectPr.pgMar.top    = "300";      //上边距
        m_SectPr.pgMar.bottom = "300";      //下边距
        //设置页面的尺寸
        doc.Document.body.sectPr = m_SectPr;

        XWPFParagraph p0 = doc.CreateParagraph(); //创建段落

        p0.Alignment = ParagraphAlignment.CENTER; //居中显示
        XWPFRun r0 = p0.CreateRun();

        //设置字体
        r0.FontFamily = "宋体";
        //设置字体大小
        r0.FontSize = 16;
        //字体是否加粗,这里加粗了
        r0.IsBold = true;
        r0.SetText("武汉管制分区(河南地区)通用航空计划表");        //写入文本

        XWPFParagraph p1 = doc.CreateParagraph(); //创建段落

        p1.Alignment = ParagraphAlignment.CENTER; //居中显示
        XWPFRun r1 = p1.CreateRun();

        //设置字体
        r1.FontFamily = "宋体";
        //设置字体大小
        r1.FontSize = 16;
        //字体是否加粗,这里加粗了
        r1.IsBold = true;
        r1.SetText("武基范围内");//写入文本

        var wordlist = new ExportDataBLL().FlightPlanDataExport(planlist);

        XWPFTable table = doc.CreateTable(2 + wordlist.Count, 13);

        table.SetColumnWidth(0, 4 * 256);//设置列的宽度
        table.SetColumnWidth(1, 4 * 256);
        table.SetColumnWidth(2, 4 * 256);
        table.SetColumnWidth(3, 4 * 256);
        table.SetColumnWidth(4, 4 * 256);
        table.SetColumnWidth(5, 20 * 256);
        table.SetColumnWidth(6, 4 * 256);
        table.SetColumnWidth(7, 4 * 256);
        table.SetColumnWidth(8, 3 * 256);
        table.SetColumnWidth(9, 3 * 256);
        table.SetColumnWidth(10, 3 * 256);
        table.SetColumnWidth(11, 3 * 256);
        table.SetColumnWidth(12, 2 * 256);
        CT_Tbl m_CTTbl = doc.Document.body.GetTblArray()[0];//获得文档第一张表

        m_CTTbl.AddNewTblPr().jc = new CT_Jc();
        m_CTTbl.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中


        table.GetRow(0).Height = 1200;
        setCell(table.GetRow(0).GetCell(0), "序号");
        setCell(table.GetRow(0).GetCell(1), "起降场");
        setCell(table.GetRow(0).GetCell(2), "单位");
        setCell(table.GetRow(0).GetCell(3), "机型/架数");
        setCell(table.GetRow(0).GetCell(4), "机号");
        setCell(table.GetRow(0).GetCell(5), "空域/航线");
        setCell(table.GetRow(0).GetCell(6), "高度");
        setCell(table.GetRow(0).GetCell(7), "任务");
        setCell(table.GetRow(0).GetCell(8), "开飞时间");
        setCell(table.GetRow(0).GetCell(10), "结束时间");
        setCell(table.GetRow(0).GetCell(12), "备注");
        table.GetRow(1).Height = 1200;
        setCell(table.GetRow(1).GetCell(8), "计划");
        setCell(table.GetRow(1).GetCell(9), "实施");
        setCell(table.GetRow(1).GetCell(10), "计划");
        setCell(table.GetRow(1).GetCell(11), "实施");

        mergeCellsVertically(table, 0, 0, 1);
        table.GetRow(0).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 1, 0, 1);
        table.GetRow(0).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 2, 0, 1);
        table.GetRow(0).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 3, 0, 1);
        table.GetRow(0).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 4, 0, 1);
        table.GetRow(0).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 5, 0, 1);
        table.GetRow(0).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 6, 0, 1);
        table.GetRow(0).GetCell(6).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 7, 0, 1);
        table.GetRow(0).GetCell(7).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsHorizontal(table, 0, 8, 9);
        table.GetRow(0).GetCell(8).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsHorizontal(table, 0, 10, 11);
        table.GetRow(0).GetCell(10).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        mergeCellsVertically(table, 12, 0, 1);
        table.GetRow(0).GetCell(12).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        table.GetRow(1).GetCell(8).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        table.GetRow(1).GetCell(9).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        table.GetRow(1).GetCell(10).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        table.GetRow(1).GetCell(11).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);


        for (int i = 0; i < wordlist.Count; i++)
        {
            setCell(table.GetRow(2 + i).GetCell(0), (i + 1).ToString());
            setCell(table.GetRow(2 + i).GetCell(1), wordlist[i].airport);
            setCell(table.GetRow(2 + i).GetCell(2), wordlist[i].company);
            setCell(table.GetRow(2 + i).GetCell(3), wordlist[i].airtype);
            setCell(table.GetRow(2 + i).GetCell(4), "");
            setCell(table.GetRow(2 + i).GetCell(5), wordlist[i].airline);
            setCell(table.GetRow(2 + i).GetCell(6), wordlist[i].high);
            setCell(table.GetRow(2 + i).GetCell(7), wordlist[i].messiontype);
            setCell(table.GetRow(2 + i).GetCell(8), wordlist[i].planbegin);
            setCell(table.GetRow(2 + i).GetCell(9), "");
            setCell(table.GetRow(2 + i).GetCell(10), wordlist[i].planend);
            setCell(table.GetRow(2 + i).GetCell(11), "");
            setCell(table.GetRow(2 + i).GetCell(12), wordlist[i].remark);
        }

        //var book = new HSSFWorkbook();
        ////设置默认工作表的默认列宽和行高
        //var sheet = book.CreateSheet("通航计划表-范围内");
        //sheet.DefaultRowHeight = 20 * 20;
        //sheet.DefaultColumnWidth = 12;
        //sheet.SetColumnWidth(5, 60 * 256);
        ////设置标题样式
        //var titleStyle = book.CreateCellStyle();
        //titleStyle.Alignment = HorizontalAlignment.Center;
        //titleStyle.VerticalAlignment = VerticalAlignment.Center;
        //var titleFont = book.CreateFont();
        //titleFont.FontHeightInPoints = 20;
        //titleFont.IsBold = true;
        //titleStyle.SetFont(titleFont);
        ////设置表头样式
        //var headStyle = book.CreateCellStyle();
        //headStyle.Alignment = HorizontalAlignment.Center;
        //headStyle.VerticalAlignment = VerticalAlignment.Center;
        //var headFont = book.CreateFont();
        //headFont.FontHeightInPoints = 14;
        //headFont.IsBold = true;
        //headStyle.SetFont(headFont);
        ////设置标题样式
        //var listStyle = book.CreateCellStyle();
        //listStyle.Alignment = HorizontalAlignment.Center;
        //listStyle.VerticalAlignment = VerticalAlignment.Center;
        //var listFont = book.CreateFont();
        //listFont.FontHeightInPoints = 14;
        //listStyle.SetFont(listFont);
        ////填入标题内容并合并
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 12));
        //var titleRow = sheet.CreateRow(0);
        //titleRow.CreateCell(0).SetCellValue("通航计划表-范围内");
        //titleRow.Cells[0].CellStyle = titleStyle;
        //titleRow.Height = 50 * 20;
        ////填入表头内容并合并
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 0, 0));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 1, 1));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 2, 2));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 3, 3));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 4, 4));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 5, 5));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 6, 6));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 7, 7));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 8, 9));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 10, 11));
        //sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 2, 12, 12));
        //var headRow1 = sheet.CreateRow(1);
        //headRow1.CreateCell(0).SetCellValue("序号");
        //headRow1.CreateCell(1).SetCellValue("起降场");
        //headRow1.CreateCell(2).SetCellValue("单位");
        //headRow1.CreateCell(3).SetCellValue("机型/架次");
        //headRow1.CreateCell(4).SetCellValue("机号");
        //headRow1.CreateCell(5).SetCellValue("空域/航线");
        //headRow1.CreateCell(6).SetCellValue("高度");
        //headRow1.CreateCell(7).SetCellValue("任务");
        //headRow1.CreateCell(8).SetCellValue("开飞时间");
        //headRow1.CreateCell(10).SetCellValue("结束时间");
        //headRow1.CreateCell(12).SetCellValue("备注");
        //foreach (var cell in headRow1.Cells)
        //{
        //    cell.CellStyle = headStyle;
        //}
        //headRow1.Height = 25 * 20;
        //var headRow2 = sheet.CreateRow(2);
        //headRow2.CreateCell(8).SetCellValue("计划");
        //headRow2.CreateCell(9).SetCellValue("实施");
        //headRow2.CreateCell(10).SetCellValue("计划");
        //headRow2.CreateCell(11).SetCellValue("实施");
        //foreach (var cell in headRow2.Cells)
        //{
        //    cell.CellStyle = headStyle;
        //}
        //headRow2.Height = 25 * 20;

        //var exportlist = new ExportDataBLL().FlightPlanDataExport(planlist);
        //for (int i = 0; i < exportlist.Count; i++)
        //{
        //    var row = sheet.CreateRow(3 + i);
        //    row.CreateCell(0).SetCellValue(i + 1);
        //    row.CreateCell(1).SetCellValue(exportlist[i].airport);
        //    row.CreateCell(2).SetCellValue(exportlist[i].company);
        //    row.CreateCell(3).SetCellValue(exportlist[i].airtype);
        //    row.CreateCell(4).SetCellValue(exportlist[i].aircraft);
        //    row.CreateCell(5).SetCellValue(exportlist[i].airline);
        //    row.CreateCell(6).SetCellValue(exportlist[i].high);
        //    row.CreateCell(7).SetCellValue(exportlist[i].airtype);
        //    row.CreateCell(8).SetCellValue(exportlist[i].planbegin);
        //    row.CreateCell(10).SetCellValue(exportlist[i].planend);
        //    row.CreateCell(12).SetCellValue(exportlist[i].remark);
        //    foreach (var cell in row.Cells)
        //    {
        //        cell.CellStyle = listStyle;
        //    }
        //    //row.Height = 30 * 20;
        //}
        //生成文件
        var file = new MemoryStream();

        doc.Write(file);
        Response.ContentType     = "application/msword";
        Response.ContentEncoding = Encoding.UTF8;
        Response.Charset         = "";
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("通航计划表-范围内.doc", Encoding.UTF8));
        file.WriteTo(Response.OutputStream);
        file.Close();
        Response.End();
    }