How to Insert a Table in Word Using Java
Preamble
Hello everyone, I am a programming snail. As a java developer, sometimes I need to operate word or excel. The more commonly used framework here is POI. I have used POI to read and write excel before, and I have also used alibaba easy excel to read and write excel, and they are all very useful.
However, there are fewer operations on word, and basic text operations should be achievable with poi. But for complex operations, such as inserting tables in word, it is a bit powerless.
Today, I will introduce an open source framework on github, Free Spire.Doc, which is very convenient for word operation. It does not need to write a lot of code like POI. It is very simple and efficient.
Source of this article: infoQ, author: Geek_249eec, finishing: snail who can program
When we are editing a Word document, if we encounter a large amount of data that needs to be reflected, we can choose to create a table directly in the Word document. Applying data to tables can not only simplify the document language, but also make the data content clearer and more intuitive. Below I will use Free Spire.Doc for Java to demonstrate how to create Word tables in Java.
Install Spire.Doc.Jar
method one:
If you are using maven, you can import the JAR file into the application by adding the following code to the project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <url>https: // repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories > <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.2.0</version> </dependency> </dependencies>
Method Two:
If you are not using maven, you can download Free Spire.Doc for Java from https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html , find Spire.Doc under lib folder. jar and unzip it.
Then create a new project in IDEA, click "File", "Project Structure", "Modules", "Dependencies", and then click the green "+" on the right Under the first option "JARs or Directories", find the unzipped Spire.Doc.jar file, click OK to import it into the project.
Create a table in Word:
Specific steps:
Create a Document object and add a section to it.
Store the data for the header row and other rows in a one-dimensional string array and two-dimensional string array, respectively.
Add a table to a section using the Section.addTable() method.
Insert data into the header row, and format the row, including row height, background color, and text alignment.
Insert data into the remaining rows and apply formatting to those rows.
Use the Document.saveToFile() method to save the file.
Relevant code:
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 Document object Document document = new Document(); // Add a section Section section = document.addSection(); // Define table data String[] header = { "Student ID", "Name" , "gender", "class", "grade" }; String[][] data = { new String[]{"0105", "Li Lei", "Male", "1", "88" }, new String[]{"0721", "Zhao Wen", "Female", "7", "92" }, new String[]{"1131", "Chen Hua", "Female", "11", "91" }, new String[]{"0418", "Song Ye", "Male", "4", "95" }, new String[]{"0513", "Han Mei", "Female", "5", "94" }, }; // Add table Table table = section.addTable( true ); table.resetCells(data.length + 1 , header.length); // Set the first row as the table header 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 txtRange =p.appendText(header[i]); txtRange.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); dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); } } // 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 result file document.saveToFile("result.docx" , FileFormat.Docx_2013) ; } }
Renderings show:
Summarize
Using Free Spire.Doc for Java can greatly simplify our word operation. It is a very recommended open source framework. I used this framework to solve a data filling problem before. This framework is ready in a few minutes, recommended.
0 Comments