· 

Inserting Tables to Word Document in Java Application

In this article, I am going to show you how to insert a plain table, a nested table to a Word document, and how to merge table cells or spilt a cell by using Free Spire.Doc for Java. 

Installing Spire.Doc.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application. 

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</
repositories>
<
dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>2.7.3</version>
    </dependency>
</
dependencies>

Example 1. Insert a table

import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;

import java.awt.*;

public class CreateTable {

   
public static void main(String[] args) {

       
//Create a Document object
       
Document document = new Document();

       
//Add a section
       
Section section = document.addSection();

       
//Add a table
       
Table table = section.addTable(true);

       
//Set the column widths
       
table.setColumnWidth(new float[]{160f, 80f, 80f});

       
//Set the row number and column number
       
table.resetCells(4, 3);

       
//Add content to cells
       
table.get(0, 0).addParagraph().appendText("Name").getCharacterFormat().setTextColor(Color.WHITE);
        table.get(
0, 1).addParagraph().appendText("Sore").getCharacterFormat().setTextColor(Color.WHITE);
        table.get(
0, 2).addParagraph().appendText("Class").getCharacterFormat().setTextColor(Color.WHITE);
        table.get(
1, 0).addParagraph().appendText("Hanson Lim");
        table.get(
1, 1).addParagraph().appendText("258");
        table.get(
1, 2).addParagraph().appendText("4B");
        table.get(
2, 0).addParagraph().appendText("John Doe");
        table.get(
2, 1).addParagraph().appendText("235");
        table.get(
2, 2).addParagraph().appendText("4C");
        table.get(
3, 0).addParagraph().appendText("Shane Midtown");
        table.get(
3, 1).addParagraph().appendText("238");
        table.get(
3, 2).addParagraph().appendText("4A");

       
//Set the background color of the first row
       
table.getRows().get(0).getRowFormat().setBackColor(new Color(190, 100, 120));

       
//Set the horizontal alignment of the first row to center
       
table.get(0, 0).getParagraphs().get(0).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        table.get(
0, 1).getParagraphs().get(0).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        table.get(
0, 2).getParagraphs().get(0).getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

       
//Save the document
       
document.saveToFile("Table.docx", FileFormat.Docx
);
    }
}

Example 2. Insert a nested table

import com.spire.doc.*;

public class InsertNestedTable {

   
public static void main(String[] args) {

       
//Create a Document object
       
Document doc = new Document();

        
//Add a section
       
Section section = doc.addSection();

       
//Add a table
       
Table table = section.addTable(true);

       
//Set the row number and column number
       
table.resetCells(3, 4);

       
//Auto fit table to window
        
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

       
//See the cell padding
       
table.getTableFormat().getPaddings().setAll(4f);

       
//Add content to specific cells
       
table.get(0,0).addParagraph().appendText("Name");
        table.get(
0,1).addParagraph().appendText("Class");
        table.get(
0,2).addParagraph().appendText("Subjects");
        table.get(
0,3).addParagraph().appendText("Result");
        table.get(
1,0).addParagraph().appendText("Jeff Miler");
        table.get(
1,1).addParagraph().appendText("5th");
        table.get(
1,3).addParagraph().appendText("Pass");
        table.get(
2,0).addParagraph().appendText("Jorge Diaz");
        table.get(
2,1).addParagraph().appendText("6th");
        table.get(
2,3).addParagraph().appendText("Fail");

       
//Add a nested table to cell(1,2)
       
Table nestedTable1 = table.get(1,2).addTable(true);
        nestedTable1.resetCells(
2, 2);
        nestedTable1.autoFit(AutoFitBehaviorType.
Auto_Fit_To_Contents);

       
//Add content to the cells of nested table
       
nestedTable1.get(0,0).addParagraph().appendText("Maths");
        nestedTable1.get(
0,1).addParagraph().appendText("B");
        nestedTable1.get(
1,0).addParagraph().appendText("History");
        nestedTable1.get(
1,1).addParagraph().appendText("A");

       
//Add another nested table to cell(2,2)
       
Table nestedTable2 = table.get(2,2).addTable(true);
        nestedTable2.resetCells(
2, 2);
        nestedTable2.autoFit(AutoFitBehaviorType.
Auto_Fit_To_Contents);
        nestedTable2.get(
0,0).addParagraph().appendText("Maths");
        nestedTable2.get(
0,1).addParagraph().appendText("C-");
        nestedTable2.get(
1,0).addParagraph().appendText("History");
        nestedTable2.get(
1,1).addParagraph().appendText("F");

       
//Save the document
       
doc.saveToFile("NestedTable.docx", FileFormat.Docx_2013
);
    }
}

Example 3. Merge or split table cells

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.Table;

public class MergeSplitCells {

   
public static void main(String[] args) {

       
//Create a Document object
       
Document doc = new Document();

       
//Add a section
       
Section sec = doc.addSection();

       
//Add a table
       
Table table = sec.addTable(true);

       
//Set the row number and column number
       
table .resetCells(4,4);

       
//Vertically merge cells
       
table .applyVerticalMerge(0,1,2);

       
//Horizontally merge cells
       
table .applyHorizontalMerge(0,1,2);

       
//Split cell into 2 columns
       
table.get(3,3).splitCell(2,1);

       
//Add content to specific cells
       
table.get(1,0).addParagraph().appendText("Vertically merge cell");
        table.get(
0,1).addParagraph().appendText("Horizontally merge cell");
        table.get(
3,3).addParagraph().appendText("Split cell");

       
//Save the document
       
doc.saveToFile("MergeSplitCells.docx", FileFormat.Docx_2013
);
    }
}

Write a comment

Comments: 0