How to Create a Word Document with JAVA

I need to generate MS word documents automatically. Then I started to search how to accomplish this task and found Apache POI library. We can easily work with MS Office documents (word, excel, etc.) with Apache POI library. For details please go https://poi.apache.org.

I want to go on with my case. I need to get values (parameters) from a text file or excel file and generate multiple word documents according to those values. For the simplicity, I selected to use a text file for reading parameters and then generate word documents.

Now, let’s go with JAVA codes.

I divided my code in three parts.

1) ReadFile Class: Reads the text file and returns list of values. These values will be our parameters for word generation.

2) WordGenerator Class: It contains getLines and createWord methods. First we get the values with getLines method and createWord method to create word documents.

3) Main Class: This is our top level class in which we instantiate WordGenerator Class and call its methods.

ReadFile Class

WordGenerator Class

Main Class

Input Text File

Console Output and Result:

console_output

 

word generation

 

By |2018-11-28T01:09:27+00:00January 29th, 2016|Java Tutorials|5 Comments

About the Author:

Onur Baskirt is a senior IT professional with 10+ years of experience. He worked at Bahçesehir University, ST Microelectronics, Huawei and Ericsson as a research assistant, design verification engineer, and software test leader. Also, he worked as software test leader and software operations manager at Turkey's biggest technology retailer, Teknosa. After Teknosa, he worked as Head of Software Testing and Manager of two Development Teams at Kariyer.net. Now, he is working as a Senior Technical Consultant at Emirates Airlines in Dubai. His current research areas are technical software testing, programming, and computer science. Formerly, he has several research works and papers on digital chip design & verification. His hobbies are sport, dancing, traveling, and nutrition. You can find detailed information about him on his linked-in page.

5 Comments

  1. Canberk January 30, 2016 at 6:09 pm - Reply

    Excel’le ilgili işlemler yaptım POİ kütüphanesi sayesinde. Baya basarili

    • Onur Baskirt January 30, 2016 at 7:49 pm - Reply

      Canberk, kesinlikle sana katılıyorum. POI, çok başarılı bir library. Ben de haftalık rutin yaptığım işlerdenen birinde excel’den data okumak için POI’yi kullanmıştım. Onu da ileride JAVA FX ile ilgili bir örnek yaparken yayınlayacağım.

  2. Steve February 2, 2016 at 8:17 am - Reply

    Another excellent API for Java is JWord from Independentsoft.

    //Steve

  3. Kadir February 6, 2016 at 11:03 pm - Reply

    Word veya excel üzerinde bir işlem yapacaksanız bir scripting dili olan Python kullanmak daha rahat oluyor.(Tabi özel bir program yazmıyorsanız.) Oradaki paketler de ücretsiz.

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.