使用POI api将页脚添加到ms字中

问题描述 投票:3回答:2

我进行了很多搜索,并获得了一些结果,这些结果中包含一些示例代码,但是没有人在工作。所有人都将获得空指针异常,或者如果生成了文档,则在打开文件(.docx)时会出错并显示消息文本/ xml声明可能仅在输入开始时出现。

我以为可能是我要添加一些内容,然后添加页脚会带来一些问题,所以这次我刚开始时就粘贴了页脚代码,这是我得到的

索引超出范围异常

这是我的完整代码

String fileName ="Book.docx";
String   folderPath=SystemProperties.get(SystemProperties.TMP_DIR)+File.separator+"liferay" + File.separator    + "document_preview";
String filePath=folderPath+File.separator+fileName;
File file=new File(filePath);
XWPFDocument document = new XWPFDocument();  
XWPFParagraph paragraphOne = document.createParagraph();
paragraphOne.setAlignment(ParagraphAlignment.CENTER);
XWPFRun paragraphOneRunOne = paragraphOne.createRun();
paragraphOneRunOne.setText("Training Report");
paragraphOneRunOne.addBreak();
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("No");
tableRowOne.createCell().setText("Name");
XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy();
            if (headerFooterPolicy == null) {
                CTBody body = document.getDocument().getBody();
                CTSectPr sectPr = body.getSectPr();
                if (sectPr == null) {
                sectPr = body.addNewSectPr();
                }
                headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
                }
            CTP ctP1 = CTP.Factory.newInstance();
            CTR ctR1 = ctP1.addNewR();
            CTText t = ctR1.addNewT();
            t.setStringValue("first footer");
            XWPFParagraph codePara = new XWPFParagraph(ctP1);
            XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
            newparagraphs[0] = codePara;
             XWPFFooter xwpfFooter = null;
    xwpfFooter =  headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
 FileOutputStream fileoutOfTraining = new FileOutputStream(file);
            document.write(fileoutOfTraining);
            fileoutOfTraining.flush();
            fileoutOfTraining.close();
   downloadOperation(file, fileName, resourceResponse);

downloadOperation方法中的代码

HttpServletResponse httpServletResponse =PortalUtil.getHttpServletResponse(resourceResponse);
BufferedInputStream input = null;
        BufferedOutputStream output = null;
 httpServletResponse.setHeader("Content-Disposition", "attachment;    filename=\""+fileName+"\"; filename*=UTF-8''"+fileName);
int  DEFAULT_BUFFER_SIZE=1024;
        try {
            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            resourceResponse.flushBuffer();
            output = new BufferedOutputStream(httpServletResponse.getOutputStream(), DEFAULT_BUFFER_SIZE);
        } catch (IOException e) {
            e.printStackTrace();
        }
byte[] buffer = new byte[2*DEFAULT_BUFFER_SIZE];
        int length;
        try {
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

[请帮助我生成页脚,这是我的代码,如果我在段落和表后添加页脚代码,则没有运行时错误,但是在打开生成的文件时出现错误,如果我将页脚放在要添加的内容之前文档,然后出现错误“索引超出绑定异常”。如果有任何代码片段或至少有一些指向该解决方案的指针,请帮助我。谢谢

java ms-word apache-poi footer
2个回答
3
投票

我遇到了这个问题,解决方法是我们必须使用3.10最终poi jar。3.9有此问题。

请删除先前版本的jar,并添加已修复此错误的3.10最终版本的jar。

罐子需要:

  1. poi-3.10-FINAL.jar

  2. poi-ooxml-3.10-FINAL.jar

  3. poi-ooxml-schemas-3.10-FINAL.jar

很容易在网上获得:

http://mvnrepository.com/artifact/org.apache.poi/poi/3.10-FINAL

XWPFDocument document = new XWPFDocument();
CTP ctp = CTP.Factory.newInstance();
CTR ctr = ctp.addNewR();
CTRPr rpr = ctr.addNewRPr();
CTText textt = ctr.addNewT();
textt.setStringValue( " Page 1" );
XWPFParagraph codePara = new XWPFParagraph( ctp, document );
XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
newparagraphs[0] = codePara;
CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new  XWPFHeaderFooterPolicy( document, sectPr );
headerFooterPolicy.createFooter( STHdrFtr.DEFAULT, newparagraphs );

上面的代码运行正常,请使用我上面提到的3.10 wars。


0
投票

我不知道是否从头开始将图像添加到标头在新版本中是否有效,但是我知道“模板化”可以完美地工作。我用文字创建了模板文档,按照我需要的方式修改了标题(在我的情况下,徽标图像位于右侧,段落的左侧为虚拟文本,另一个图像将上部两个对象与内容,在页眉底部,并且所有内容都在所有页面上重复),并将文档的其余部分留空。从一开始,我就不再通过调用...new XWPFDocument()来创建XWPFDocument,而是通过以下方式创建的:

XWPFDocument doc = new XWPFDocument(new FileInputStream("pathTo/template.docx"));

比仅用您的内容填充文档,如果您需要像我一样为不同的导出更新标题文本,请调用update标题函数,在我的情况下看起来像这样:

public void updateHeader() throws InvalidFormatException, IOException {
        // load the header policy from template and update the paragraph text
        XWPFHeaderFooterPolicy headerFooterPolicy = document
                .getHeaderFooterPolicy();
        XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader();
        defaultHeader.getParagraphs().get(0).getRuns().get(0)
                .setText(profileName, 0);
        // this is only to put some space between the content in the header and the real content
        defaultHeader.getParagraphs()
                .get(defaultHeader.getParagraphs().size() - 1)
                .setSpacingAfter(300);
    }

据我所知,此功能自3.10起生效,如果您偶然发现了某些Java安全问题,请尝试使用最新的夜间版本,其中许多安全问题已得到解决。对我来说,它甚至可以在Google App Engine上运行。

© www.soinside.com 2019 - 2024. All rights reserved.