已經有一個帶有頁眉和頁腳以及首頁中的一些文本的現有 Word 檔案。word檔案的正文是空的。我每次都需要創建一個表格(或覆寫以前的表格),它位于 word 檔案的第二頁。我正在從頭開始創建一個表格并嘗試更新 word 檔案。但是我收到一個錯誤。下面是我的代碼
XWPFDocument document = new XWPFDocument(OPCPackage.open(source
"Testword.docx"));
//create table
XWPFTable table = document.createTable();
//create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
tableRowThree.getCell(2).setText("col three, row three");
document.write(new FileOutputStream(source
"Testword.docx")); // This line is generating an error
錯誤是
Exception in thread "main" org.apache.poi.ooxml.POIXMLException: java.io.EOFException: Unexpected end of ZLIB input stream
uj5u.com熱心網友回復:
public static void main(String[] args) throws InvalidFormatException, IOException {
InputStream is = new FileInputStream("D:\\Testword.docx");
XWPFDocument document = new XWPFDocument(is);
is.close();
//create table
XWPFTable table = document.createTable();
//create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
tableRowThree.getCell(2).setText("col three, row three");
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Testword.docx");
document.write(fileOutputStream);
fileOutputStream.close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516950.html
上一篇:如何用陣列索引串列
下一篇:如何使用條件對表進行物件更新
