How to create docx file using apache poi
Posted By : Akash Tomer | 10-Jan-2018
Here, I will discuss how to create a docx file using apache poi in android. Although there is another library also like aspose, doc4j etc. But I used apache poi because it is open source and we can easily perform read & write operation in docx file.
So, first of all, we have to add the jar file of apache poi 3.15. For this, you will download it from here. After this, you have to add these lines in your application class to set the system properties.
System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");
Now below is the following method for creating the docx file .
private void generateDocFile() {
try (XWPFDocument doc = new XWPFDocument()) {
XWPFParagraph p = doc.createParagraph();
p.setAlignment(ParagraphAlignment.CENTER);
XWPFRun r = p.createRun();
p.setIndentationFirstLine(-2);
r.addPicture(getAssets().open("logo.jpg"), XWPFDocument.PICTURE_TYPE_JPEG, "logo.jpg", Units.toEMU(200), Units.toEMU(125));
XWPFParagraph p2 = doc.createParagraph();
XWPFRun r2 = p2.createRun();
r2.setFontSize(20);
r2.setBold(true);
r2.addBreak();
r2.addBreak();
r2.addBreak();
p2.setAlignment(ParagraphAlignment.CENTER);
r2.setText("HEADING 1");
r2.addBreak();
r2.setText("HEADING 2");
r2.addBreak();
r2.setText("HEADING 3");
r2.addBreak(BreakType.TEXT_WRAPPING);
for (String imgFile : getAllMediaPath()) {
int format;
XWPFParagraph p3 = doc.createParagraph();
p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
p3.setIndentFromLeft(0);
p3.setIndentationLeft(0);
p3.setIndentationRight(0);
p3.setIndentationFirstLine(-2);
p3.setIndentationHanging(0);
p3.setIndentFromRight(0);
if (imgFile.endsWith(".emf")) format = XWPFDocument.PICTURE_TYPE_EMF;
else if (imgFile.endsWith(".wmf")) format = XWPFDocument.PICTURE_TYPE_WMF;
else if (imgFile.endsWith(".pict")) format = XWPFDocument.PICTURE_TYPE_PICT;
else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
format = XWPFDocument.PICTURE_TYPE_JPEG;
else if (imgFile.endsWith(".png")) format = XWPFDocument.PICTURE_TYPE_PNG;
else if (imgFile.endsWith(".dib")) format = XWPFDocument.PICTURE_TYPE_DIB;
else if (imgFile.endsWith(".gif")) format = XWPFDocument.PICTURE_TYPE_GIF;
else if (imgFile.endsWith(".tiff")) format = XWPFDocument.PICTURE_TYPE_TIFF;
else if (imgFile.endsWith(".eps")) format = XWPFDocument.PICTURE_TYPE_EPS;
else if (imgFile.endsWith(".bmp")) format = XWPFDocument.PICTURE_TYPE_BMP;
else if (imgFile.endsWith(".wpg")) format = XWPFDocument.PICTURE_TYPE_WPG;
else {
System.err.println("Unsupported picture: " + imgFile +
". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
continue;
}
FileInputStream in = new FileInputStream(imgFile);
p3.createRun().addPicture(in, format, imgFile, Units.pixelToEMU(768), Units.pixelToEMU(1218)); // 200x200 pixels
}
try (FileOutputStream out = new FileOutputStream(getOutputMediaFile())) {
doc.write(out);
FileUtils.deletePicute();
setResult(AppConstant.RES_CAMERAACTIVITY);
finish();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
First of all, you have to create the instance of XWPFDocument. Then you have to create the paragraph instance. If you want to align a paragraph you can use setAlignment() method for that. If you want to write some text you can do it by using the XWPFRun object. To add the image addPicture() method. If you want to break a line or page use addbreak() method.
Hope this will help you.
Best of luck!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Tomer
Akash is an Android Developer at Oodles Technology.