-1
我認爲我的問題圍繞着我在下面創建的Write
方法。如何創建多個段落/行?
我剛接觸Apache POI Word(v 3.15),但一直在使用教程來學習如何使用它 - 但網上幾乎沒有文檔。
我想將創建文檔的一些基本功能放到一個或兩個類中,以簡化編寫冗餘代碼並簡化創建文檔。但是,當我嘗試爲同一個輸出流創建第二段時,我的代碼失敗,無論它是單獨編寫還是在下面的Write
方法中寫入。所以我不確定如何保留引用相同的流,但寫入多個段落或行?我總是收到的錯誤是這樣的:
org.apache.poi.openxml4j.exceptions.OpenXML4JException:該部分/docProps/app.xml無法被保存在流......「
的MSWord
類:
public class MSWord {
private String path = "";
public XWPFDocument Document = null;
public FileOutputStream wordStream = null;
public String getPath() {
return path;
}
public MSWord(String path) {
this.path = path;
try {
WordApplication(path);
CreateWordDocument();
} catch(IOException e) {
System.out.println("Error creating Word Document.");
}
}
private void WordApplication(String path) throws IOException {
wordStream = new FileOutputStream(path);
}
private void CreateWordDocument() {
this.Document = new XWPFDocument();
}
public void Write(String copy) {
XWPFParagraph paragraph = null;
XWPFRun run = null;
try {
paragraph = this.Document.createParagraph();
run = paragraph.createRun();
run.setText(copy);
this.Document.write(this.wordStream);
} catch(IOException e) {
System.out.println("Failed to write");
}
}
}
的MainWord
調用類:
public class MainWord {
public static void main(String[] args) throws Exception {
MSWord doc = new MSWord("C:/users/scott/Desktop/doc.docx");
doc.Write("First line.");
doc.Write("Second line.");
doc.Write("Third line.");
doc.wordStream.close();
}
}
請不要發佈重複的問題,解決您的原帖由[編輯]荷蘭國際集團代替。 – Deduplicator