我正嘗試在apache poi中使用XWPF格式創建一個word文檔。該文檔需要創建表格,所以我需要將頁面方向設置爲橫向。我使用了Landscape and portrait pages in the same word document using Apache POI XWPF in Java 的現有代碼,並在創建文檔後包含了一個函數調用,但它拋出了空指針豁免。任何人都可以幫助我。 預先感謝您。 我用下面的代碼:無法使用apache poi更改ms-word文件(XWPF)的方向
private void changeOrientation(XWPFDocument document, String orientation){
CTDocument1 doc = document.getDocument();
CTBody body = doc.getBody();
CTSectPr section = body.addNewSectPr();
XWPFParagraph para = document.createParagraph();
CTP ctp = para.getCTP();
CTPPr br = ctp.addNewPPr();
br.setSectPr(section);
CTPageSz pageSize = section.getPgSz();
if(orientation.equals("landscape")){
pageSize.setOrient(STPageOrientation.LANDSCAPE);
pageSize.setW(BigInteger.valueOf(842 * 20));
pageSize.setH(BigInteger.valueOf(595 * 20));
}
else{
pageSize.setOrient(STPageOrientation.PORTRAIT);
pageSize.setH(BigInteger.valueOf(842 * 20));
pageSize.setW(BigInteger.valueOf(595 * 20));
}
}
其在該行拋出一個錯誤:
pageSize.setOrient(STPageOrientation.LANDSCAPE);
哪條線路給NPE?如果我們不知道它來自哪裏,它很難修復! – Gagravarr
@Gagravarr我已經包括了我得到錯誤的行! 請幫助我! –