如何通過Java應用程式創建Word表格
- 2022 年 9 月 23 日
- 筆記
- JAVA, Spire. Doc for Java, Word, 創建表格
表格,又稱為表,既是一種可視化交流模式,又是一種組織整理數據的手段。人們在通訊交流、科學研究以及數據分析活動當中廣泛採用著形形色色的表格。那麼如何通過Java應用程式創建Word表格呢?別擔心,本文將詳細為您介紹通過Java應用程式創建Word表格。下面是我整理的思路以及具體步驟,並附上Java程式碼供大家參考。
使用工具: Free Spire.Doc for Java (免費版)
程式環境:
方法1:手動引入。將 Free Spire.Doc for Java 下載到本地,解壓,找到lib文件夾下的Spire.Doc.jar文件。在IDEA中打開如下介面,將本地路徑中的jar文件引入Java程式
方法2: 如果您想通過 Maven安裝,則可以在 pom.xml 文件中添加以下程式碼導入 JAR 文件。
<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>
具體步驟:
下表列出了一些負責創建和格式化表格的核心類和方法。
名稱 |
描述 |
Table類 |
表示 Word 文檔中的表格。 |
TableRow類 |
表示表中的一行。 |
TableCell類 |
表示表格中的特定單元格。 |
Section.addTbale()方法 |
將新表格添加到指定節。 |
Table.resetCells()方法 |
重置行號和列號。 |
Table.getRows()方法 |
獲取表格行。 |
TableRow.setHeight() 方法 |
設置指定行的高度。 |
TableRow.getCells()方法 |
返回單元格集合。 |
TableRow.getFormat()方法 |
獲取指定行的格式。 |
- 創建一個 Document 對象,並向其添加一個節。
- 準備標題行和其他行的數據,分別存儲在一維字元串數組和二維字元串數組中。
- 使用 Section.addTable() 方法將表格添加到節。
- 將數據插入標題行,並設置行格式,包括行高、背景顏色和文本對齊方式。
- 將數據插入其餘行,並對這些行應用格式。
- 使用 Document.saveToFile() 方法保存文件。
完整程式碼:
【Java】
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) { //創建一個 Document 對象 Document document = new Document(); //添加一個節 Section section = document.addSection(); //定義表格數據 String[] header = {"國家", "首都", "大陸", "國土面積", "人口數量"}; String[][] data = { new String[]{"玻利維亞", "拉巴斯", "南美", "1098575", "7300000"}, new String[]{"巴西", "巴西利亞", "南美", "8511196", "150400000"}, new String[]{"加拿大", "渥太華", "北美", "9976147", "26500000"}, new String[]{"智利」「聖地亞哥", "南美", "756943", "13200000"}, new String[]{"哥倫比亞", "波哥大", "南美", "1138907", "33000000"}, new String[]{"古巴", "哈瓦那", "北美", "114524", "10600000"}, new String[]{"厄瓜多", "基多", "南美", "455502", "10600000"}, new String[]{"薩爾瓦多", "聖薩爾瓦多", "北美", "20865", "5300000"}, new String[]{ "蓋亞那", "喬治城","南美", "214969", "800000"}, }; //添加表格 Table table = section.addTable(true); table.resetCells(data.length + 1, header.length); //將第一行設置為表格標題 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); } //將數據添加到其餘行 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]); } } //設置單元格的背景顏色 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)); } } } //保存文件 document.saveToFile("創建表格.docx", FileFormat.Docx_2013); } }
效果圖: