序言 各位好啊,我是會編程的蝸牛,作為java開發者,有時候需要操作word或者excel,這里面比較常用的框架是POI,我之前用過POI來讀寫excel,也有用過alibaba easy excel來讀寫excel,還都挺好用的, 不過對于word的操作就比較少了,基本的文本操作用poi應該也是可以實作的,但對于復雜的操作,像在word里面插入表格等,就有點力不從心了, 今天介紹一款github上開源的框架,Free Spire.Doc,它對于word的操作還是很方便,沒有像POI那樣需要寫一大堆代碼,非常簡潔高效, 本文來源:infoQ,作者:Geek_249eec,整理:會編程的蝸牛 當我們在編輯 Word 檔案時,如果遇到大量資料需要體現,可以選擇直接在 Word 檔案中創建表格,將資料應用于表格內,不僅能夠簡化檔案語言,而且也可以使資料內容更加清晰、直觀,下面我就將使用Free Spire.Doc for Java演示如何在 Java 中創建 Word 表格, 安裝 Spire.Doc.Jar 方法一: 如果您使用的是 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>
方法二: 如果您沒有使用 maven,則可以從https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html下載 Free Spire.Doc for Java,找到 lib 檔案夾下的 Spire.Doc.jar 并進行解壓, 然后在 IDEA 中創建一個新專案,依次點擊“檔案”(File),“專案結構”(Project Structure),“組件”(Modules),“依賴項”(Dependencies),再點擊右方綠色“+”下的第一個選項“JAR 檔案或路徑”(JARs or Directories),找到解壓后的 Spire.Doc.jar 檔案,點擊確認,將其匯入到專案中, 在 Word 中創建表格: 具體操作步驟:
- 創建一個 Document 物件,并向其添加一個節,
- 將標題行和其他行的資料分別存盤在一維字串陣列和二維字串陣列中,
- 使用 Section.addTable() 方法將表格添加到節,
- 將資料插入標題行,并設定行格式,包括行高、背景顏色和文本對齊方式,
- 將資料插入其余行,并對這些行應用格式,
- 使用 Document.saveToFile() 方法保存檔案,
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[]{"0105", "李雷", "男", "1", "88"}, new String[]{"0721", "趙文", "女", "7", "92"}, new String[]{"1131", "陳華", "女", "11", "91"}, new String[]{"0418", "宋野", "男", "4", "95"}, new String[]{"0513", "韓梅", "女", "5", "94"}, }; //添加表格 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("result.docx", FileFormat.Docx_2013); } }
效果圖展示:
總結
使用Free Spire.Doc for Java,可以大大簡化我們操作word,是一款非常值得推薦的開源框架,我之前就用這個框架解決了一個資料填充的問題,這個用POI寫了半天也沒搞定,用這個框架幾分鐘就搞定了,推薦,
推薦閱讀 點擊標題可跳轉
Tabby,一款老外都在用的 SSH工具,竟然還支持網頁操作
IDEA 除錯起來太費勁?你需要了解這幾招!
如何用VMWARE創建一個Linux虛擬機
java程式員在交接別人的作業時如何保證順利交接?
如何用Virtualbox搭建一個虛擬機
我整理的干貨,回復【JAVA核心】獲取《JAVA核心面試知識整理》
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/522949.html
標籤:其他
上一篇:KubeSphere 社區雙周報 | 2022-10-28
下一篇:web安全學習(sql注入1)
