A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/add_tables_to_word_document.htm below:

How to add tables to a word document using Java

How to add tables to a word document using Java Problem Description

How to add tables to a word document using Java.

Solution

Following is the program to add tables to a word document using Java.

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class TablesToWord {
   public static void main(String[] args)throws Exception {

      //Blank Document
      XWPFDocument document = new XWPFDocument();

      //Write the Document in file system
      FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
      
      //create table
      XWPFTable table = document.createTable();

      //create first row
      XWPFTableRow tableRowOne = table.getRow(0);
      
      tableRowOne.getCell(0).setText("col one, row one");
      tableRowOne.addNewTableCell().setText("col two, row one");
      tableRowOne.addNewTableCell().setText("col three, row one");

      //create second row
      XWPFTableRow tableRowTwo = table.createRow();
      
      tableRowTwo.getCell(0).setText("col one, row two");
      tableRowTwo.getCell(1).setText("col two, row two");
      tableRowTwo.getCell(2).setText("col three, row two");

      //create third row
      XWPFTableRow tableRowThree = table.createRow();
      
      tableRowThree.getCell(0).setText("col one, row three");
      tableRowThree.getCell(1).setText("col two, row three");
      tableRowThree.getCell(2).setText("col three, row three");

      document.write(out);
      out.close();
      
      System.out.println("create_table.docx written successully");
   }
}
Output

java_apache_poi_word


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4