NPOI2.1创建docx页眉页脚

      目前NPOI发展到了2.1,但总的来说,NPOI支持电子表格要比word强。对于word的操作不仅还不够完善且文档也少。经过分析docx的xml文件,实现了NPOI2.1创建docx页眉页脚。

      代码如下:

            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.w = (ulong)16838;
            m_SectPr.pgSz.h = (ulong)11906;

            //创建页脚
            CT_Ftr m_ftr = new CT_Ftr();
            m_ftr.AddNewP().AddNewR().AddNewT().Value = "fff";//页脚内容
            //创建页脚关系(footern.xml)
            XWPFRelation Frelation = XWPFRelation.FOOTER;
            XWPFFooter m_f = (XWPFFooter)m_Docx.CreateRelationship(Frelation, XWPFFactory.GetInstance(), m_Docx.FooterList.Count + 1);
            //设置页脚
            m_f.SetHeaderFooter(m_ftr);
            CT_HdrFtrRef m_HdrFtr = m_SectPr.AddNewFooterReference();
            m_HdrFtr.type = ST_HdrFtr.@default;
            m_HdrFtr.id = m_f.GetPackageRelationship().Id;

            //创建页眉
            CT_Hdr m_Hdr = new CT_Hdr();
            m_Hdr.AddNewP().AddNewR().AddNewT().Value = "hhh";//页眉内容
            //创建页眉关系(headern.xml)
            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader m_h = (XWPFHeader)m_Docx.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), m_Docx.HeaderList.Count + 1);
            //设置页眉
            m_h.SetHeaderFooter(m_Hdr);
            m_HdrFtr = m_SectPr.AddNewHeaderReference();
            m_HdrFtr.type = ST_HdrFtr.@default;
            m_HdrFtr.id = m_h.GetPackageRelationship().Id;


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页