使用 POI api 向 ms word 添加页脚 [英] Adding footer to ms word using POI api

查看:43
本文介绍了使用 POI api 向 ms word 添加页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多并得到了一些结果,其中有一些示例代码,但没有人在工作.所有都是空指针异常,或者如果生成文档,则在打开文件 (.docx) 时给出错误并显示消息text/xml 声明只能出现在输入的最开始处.

I searched a lot and getting some results in which some sample code is there but no one is working. All are either getting null pointer exception or if document is generated then at the time of opening file (.docx) giving error and displaying message A text/xml declaration may occur only at the very beginning of innput.

我想可能是我添加了一些内容,然后添加页脚出现了一些问题,所以我从一开始就粘贴了我的页脚代码,这次我得到了

I thought may be I am adding some content and then adding footer is giving some problem so I pasted my footer code at very beginning now this time I am getting

索引越界异常

这是我的完整代码

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 方法中的代码

code in downloadOperation method

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();
            }
        }

请帮我生成页脚,这是我的代码,如果我在我的段落和表格之后添加页脚代码,那么没有运行时错误,但打开生成的文件时出错,如果我将页脚放在要添加的内容之前在文档中,我收到错误索引超出范围异常".如果有人有任何代码片段或至少有一些指向解决方案的指针,请帮助我.谢谢

Please help me to generate footer, this is my code, if I add footer code after my paragraph and table then no run time error but error in opening generated file, if I placed footer at before the contents that I want to add in documents then I am getting error "index out of bound exception". Please help me if any one having any code snippet or at least some pointers towards the solution. Thanks

推荐答案

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

I faced this issue and the solution is We have to use 3.10 final poi jar. 3.9 having this problem.

请移除之前版本的 jar 并添加修复此错误的 3.10 最终版本的 jar.

Please remove jars of previous version and add jars of 3.10 final version in which this bug is fixed.

罐子要求是:

  1. poi-3.10-FINAL.jar

  1. poi-3.10-FINAL.jar

poi-ooxml-3.10-FINAL.jar

poi-ooxml-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.

The above code is working perfectly, please use 3.10 wars those I mentioned above.

这篇关于使用 POI api 向 ms word 添加页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆