簡體   English   中英

使用POI api向ms字添加頁腳

[英]Adding footer to ms word using POI api

我搜索了很多並獲得了一些結果,其中有一些示例代碼,但沒有人工作。 所有都要么得到空指針異常,要么生成文檔然后在打開文件(.docx)時給出錯誤並顯示消息只有在innput的最開始才會出現text / 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();
            }
        }

請幫我生成頁腳,這是我的代碼,如果我在段落和表后添加頁腳代碼然后沒有運行時錯誤但是在打開生成的文件時出錯,如果我在頁面中放置了我要在文檔中添加的內容然后我收到錯誤“索引超出范圍的異常”。 如果任何人有任何代碼片段或至少有一些指向解決方案的指針,請幫助我。 謝謝

我遇到了這個問題,解決方法是我們必須使用3.10最終的poi jar。 3.9有這個問題。 請刪除以前版本的罐子並添加3.10最終版本的罐子,其中修復了此錯誤。 罐子需要是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戰爭。

我不知道從頭開始向頭部添加圖像是否適用於新版本,但我知道“模板”的工作原理非常完美。 我用word創建了模板文檔,按照我需要的方式調整了標題(在我的例子中,logo-image在右邊,左邊是虛擬文本的段落,另一個是將上面兩個對象分開的圖像)內容,在標題底部,所有頁面上都重復這些內容,並將文檔的其余部分留空。 在一開始,我沒有通過調用...new XWPFDocument()創建...new XWPFDocument() ,我是這樣創建的:

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

不只是用你的內容填充你的文檔,如果你需要像我一樣更新不同導出的標題文本,請調用更新標題函數,在我的情況下看起來像這樣:

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上運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM