This article shows how to use Spire.Doc for Java to programmatically insert a table into a word document and set the format and styles for the table.
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 manually 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</artifactId>
<version>4.3.6</version>
</dependency>
</dependencies>
The following assembly directives are required to compile the code in this topic.
import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TableRowHeightType;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.TextRange;
import java.awt.*;
With Spire.Doc, we could use the document.addSection().addTable method to add a table into a new word document. And we could use table.applyHorizontalMerge(0,1,3) method to merge the table cells horizontally. Here are the code snippets of how to insert a table to word processing document and set the format of the table.
import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TableRowHeightType;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class addTable {
public static void main(String[] args) {
//Create a new document and add a section
Document document = new Document();
Section section = document.addSection();
//define the data for table
String[] header = {"Table", "Horizontal merge the cells"};
String[][] data =
{
new String[]{"John", "Male", "Sales", "0109"},
new String[]{"Louis", "Female", "Developer", "0111"},
new String[]{"Jack", "Male", "Developer", "0110"},
new String[]{"Moon", "Male", "Support", "0112"},
new String[]{"Vicky", "Female", "Test", "0113"},
};
//Add a table
Table table = section.addTable(true);
//set the rows and columns for the table
table.resetCells(6,4);
//merge the cells horizontally
table.applyHorizontalMerge(0,1,3);
//merge the cells vertically
//table.applyVerticalMerge(2,2,3);
//Auto fit column widths to window
table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);
//Set the first row as table header and add data
TableRow row = table.getRows().get(0);
row.isHeader(true);
row.setHeight(40);
row.setHeightType(TableRowHeightType.Exactly);
row.getRowFormat().setBackColor(Color.gray);
for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange range1 = p.appendText(header[i]);
range1.getCharacterFormat().setFontName("Arial");
range1.getCharacterFormat().setFontSize(14f);
range1.getCharacterFormat().setBold(false);
}
//add data to the rest of rows
for (int r = 0; r < data.length; r++) {
TableRow dataRow = table.getRows().get(r + 1);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
range2.getCharacterFormat().setFontName("Arial");
range2.getCharacterFormat().setFontSize(10f);
}
}
//Set background color for cells
for (int j = 1; j < table.getRows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = table.getRows().get(j);
for (int f = 0; f < row2.getCells().getCount(); f++) {
row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230));
}
}
}
//Save the document
document.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
}
}
Once the code is executed successfully, we will get a Word document with table like this:
Top comments (0)