In Word documents, tables can be used to make the content of text more concise and clear, while at the same time making the display of data clearer and more intuitive. This article will show you how to use Java code to create a table in a Word document and set the background color of the cells.
** Tools used: ** Free Spire.Doc for Java (Free version)
** How to import JAR file ** ** Method 1: ** Download and unzip the Free Spire.Doc for Java package and import the Spire.Doc.jar package from the lib folder into your Java application.
Method 2: If you are using maven, you need to add the following dependency to your pom.xml file:
<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>
** Java code example: **
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class CreateTable {
public static void main(String[] args) {
//Create a Word document
Document document = new Document();
//Added section
Section section = document.addSection();
//Tabular data
String[] header = {"name", "sex", "Department", "Job number"};
String[][] data =
{
new String[]{"Winny", "Female", "Accountant", "0109"},
new String[]{"Lois", "Female", "Salesperson", "0111"},
new String[]{"Jois", "male", "Technical staff", "0110"},
new String[]{"Moon", "Female", "Salesperson", "0112"},
new String[]{"Vinit", "Female", "Support staff", "0113"},
};
//Add table
Table table = section.addTable(true);
//Set the number of rows and columns in the table
table.resetCells(data.length + 1, header.length);
//Set the first row as a table header and add data
TableRow row = table.getRows().get(0);
row.isHeader(true);
row.setHeight(20);
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(12f);
range1.getCharacterFormat().setBold(true);
}
//Add data to the remaining 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 the background color of the cell
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("Table.docx", FileFormat.Docx_2013);
}
}
** Output document: **
Recommended Posts