首页 > 代码库 > NPOI2.1在docx页脚插入页码

NPOI2.1在docx页脚插入页码

      尽管NPOI发展到了2.1,但支持电子表格要比word强。对于word的操作不仅还不够完善且文档也少。经过分析docx的xml文件,实现了NPOI2.1在docx页脚插入页码。

      代码如下:

            XWPFDocumentm_Docx =new XWPFDocument();

 

            //页面设置

            //A4:W=11906,h=16838

            m_Docx.Document.body.sectPr = new CT_SectPr();

            CT_SectPrm_SectPr = m_Docx.Document.body.sectPr;

            //页面设置A4横向

            m_SectPr.pgSz.w = (ulong)16838;

            m_SectPr.pgSz.h = (ulong)11906;

 

            XWPFParagraphm_xp = m_Docx.CreateParagraph();

            m_xp.CreateRun().AddBreak();   //分页

 

            //创建页脚

            CT_Ftrm_ftr = new CT_Ftr();

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

            CT_SdtBlockm_Sdt =new CT_SdtBlock();

            CT_SdtPrm_SdtPr = m_Sdt.AddNewSdtPr();

            CT_SdtDocPartm_SdtDocPartObj = m_SdtPr.AddNewDocPartObj();

           m_SdtDocPartObj.AddNewDocPartGallery().val = "PageNumbers (Bottom of Page)";

            m_SdtDocPartObj.docPartUnique = new CT_OnOff();

 

            CT_SdtContentBlockm_SdtContent = m_Sdt.AddNewSdtContent();

            CT_Pm_SdtContentP = m_SdtContent.AddNewP();

            CT_PPrm_SdtContentPpPr = m_SdtContentP.AddNewPPr();

            m_SdtContentPpPr.AddNewJc().val = ST_Jc.center;

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

            CT_SimpleFieldm_fldSimple =new CT_SimpleField();

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

 

           m_SdtContentP.Items.Add(m_fldSimple);

            m_ftr.Items.Add(m_Sdt);

 

            //创建页脚关系(footern.xml

            XWPFRelationFrelation =XWPFRelation.FOOTER;

            XWPFFooterm_f = (XWPFFooter)m_Docx.CreateRelationship(Frelation,XWPFFactory.GetInstance(),m_Docx.FooterList.Count + 1);

            //设置页脚

            m_f.SetHeaderFooter(m_ftr);

            CT_HdrFtrRefm_HdrFtr = m_SectPr.AddNewFooterReference();

            m_HdrFtr.type = ST_HdrFtr.@default;

            m_HdrFtr.id =m_f.GetPackageRelationship().Id;

经过调试的例子下载:http://download.csdn.net/detail/gltide/8143409。例中提供了在页脚插入页码的实现代码,NPOI是tonyqus提供的2.1.1.0源码经过新修改编译。


NPOI2.1在docx页脚插入页码