Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Word Document using Apache POI?

Tags:

apache-poi

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.

like image 550
Matti Kiviharju Avatar asked Nov 22 '12 07:11

Matti Kiviharju


People also ask

What is Apache POI DOCX?

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.


2 Answers

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

  • POI Scratchpad
  • Javadoc HWPFDocument
  • Javadoc org.apache.poi.hwpf.usermodel

XWPF Reference(.docx): Examples from Apache POI SVN Repo

  • SimpleDocument.java
  • SimpleImages.java
  • SimpleTable.java
  • UpdateEmbeddedDoc.java

And also refer POI Javadocs for XWPF (Word Document).

I hope it will provide startup for you!

like image 141
jeevatkm Avatar answered Oct 21 '22 17:10

jeevatkm


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?");   

    }   
  }   
like image 22
Amitabh Ranjan Avatar answered Oct 21 '22 18:10

Amitabh Ranjan