In many cases we need to deal with tables in the Word Document. Apache POI makes it simple to add table to the Word Document. Lets see the way to add table to word doc using POI.
Classes / Interfaces Used
XWPFTable
org.apache.poi.xwpf.usermodel.XWPFTable is used to create simple table.
XWPFTableRow
org.apache.poi.xwpf.usermodel.XWPFTableRow is used to create a Row of the table
XWPFTableCell
org.apache.poi.xwpf.usermodel.XWPFTableCell is used to create a cell of the table
The Code
Creating table is very simple. You already have the document object created from the XWPFDocument. Using that we can create simple table
1 |
XWPFTable table = document.createTable(); |
Now creating the 1st Row and 1st Cell is different from creating the rest of Rows and Cells.
Creating First Row
XWPFTableRow tableRow0 = table.getRow(0);
Creating First Cell of First Row
XWPFTableCell tableCell0 = tableRow0.getCell(0);
Creating Next Cell to the First Row
XWPFTableCell tableCell1 = tableRow0.addNewTableCell();
Creating Next Rows and Next Cells
XWPFTableRow tableRow1 = table.createRow();
tableRow1.getCell(0).setText(” Row 1 Column 0 “);
You will see comments in the code below for better understanding of the Row and Cell creation process
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.kscodes.test; 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.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; public class CreateTableDocPoi { public static void main(String args[]) { XWPFDocument document = null; FileOutputStream fileOutputStream = null; try { document = new XWPFDocument(); File fileToBeCreated = new File("C:\\kscodes_temp\\POI_Table_Test.docx"); fileOutputStream = new FileOutputStream(fileToBeCreated); // Create a Simple Table using the document. XWPFTable table = document.createTable(); // Now add Rows and Columns to the Table. // Creating the First Row XWPFTableRow tableRow0 = table.getRow(0); // Creating the First Cell XWPFTableCell tableCell0 = tableRow0.getCell(0); tableCell0.setText(" Row 0 Column 0 "); // Creating the Other Cells for the First Row XWPFTableCell tableCell1 = tableRow0.addNewTableCell(); tableCell1.setText(" Row 0 Column 1 "); XWPFTableCell tableCell2 = tableRow0.addNewTableCell(); tableCell2.setText(" Row 0 Column 2 "); // Creating the Next Rows and Cells XWPFTableRow tableRow1 = table.createRow(); tableRow1.getCell(0).setText(" Row 1 Column 0 "); tableRow1.getCell(1).setText(" Row 1 Column 1 "); tableRow1.getCell(2).setText(" Row 1 Column 2 "); document.write(fileOutputStream); System.out.println("Table created in Word File Succefully !!!"); } catch (Exception e) { System.out.println("We had an error while creating the Word Doc " + e.getMessage()); e.printStackTrace(); } finally { try { if (document != null) { document.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (Exception ex) { } } } } |