How to create a Word Document using Apache POI?
I am developing a Resume Editor for Atlassian Confluence as Commercial Plugin.
I am sorry I had to ask this but I do not find tutorials witch can help me.
Apache POI is a Java library for working with the various file formats based on the Office Open XML standards (OOXML) and Microsoft's OLE 2 Compound Document format (OLE2). This tutorial focuses on the support of Apache POI for Microsoft Word, the most commonly used Office file format.
Your attached code file "DownloadAsMicrosoftWordDocument.java.txt" has a coding for file download functionality; no Word document creation.
As you looking for Word Document creation, please find references below:
HWPF Reference(.doc): POI trunk doesn't have examples as XWPF do, However POI Scratchpad has Testcases around it, please find
XWPF Reference(.docx): Examples from Apache POI SVN Repo
And also refer POI Javadocs for XWPF (Word Document).
I hope it will provide startup for you!
package org.poi.images;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class DocFile {
public void newWordDoc(String filename, String fileContent)
throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText(fileContent);
tmpRun.setFontSize(18);
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\amitabh\\Pictures\\pics\\"+filename + ".doc"));
document.write(fos);
fos.close();
}
public static void main(String[] args) throws Exception {
DocFile app = new DocFile();
app.newWordDoc("testfile", "Hi hw r u?");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With