表格做爲一種可視化交流模式及組織整理數據的手段,在各類場合及文檔中應用普遍。常見的表格可包含文字、圖片等元素,咱們操做表格時能夠插入圖片、寫入文字及格式化表格樣式等。下面,將經過Java編程在Word文檔中建立表格並實現格式化操做,包括設置字體、字號、字體顏色、字體粗細等,設置單元格對齊方式、單元格背景色、單元格合併、設置表格邊框樣式、插入圖片等。html
使用工具:Free Spire.Doc for Java 2.0.0 (免費版)java
方法1:首先經過官網獲取jar包。下載控件包並解壓。c++
導入步驟:在程序中新建一個directory目錄,並命名(本示例中命名爲lib);將控件包lib文件夾下的Spire.Doc.jar文件(以下圖1)複製到程序中新建的目錄下。複製jar文件後,鼠標右鍵點擊jar文件,選擇」Add as Library」。完成導入(以下圖2)。編程
圖1:數組
圖2:app
方法2:經過maven導入。參考導入方法。maven
Step 1: 建立文檔ide
Document doc = new Document(); Section sec = doc.addSection();
Step 2:聲明數組內容工具
//聲明數組內容 String[] header = {"班級","姓名","性別", "學號", "專業成績"}; String[][] data = { new String[]{"一班","王麗", "女", "Y1256486", "138"}, new String[]{"一班","洪菲菲", "女", "Y5425875", "134"}, new String[]{"二班","劉洋", "男", "B1546258", "141"}, new String[]{"三班","馮剛", "男", "B1542367", "136"}, new String[]{"三班","劉思源", "男", "Z1263547", "133"}, };
Step 3:添加表格並寫入數據字體
//添加表格 Table table = sec.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.ORANGE); 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); range1.getCharacterFormat().setTextColor(Color.white); } //寫入剩餘組內容到表格,並格式化數據 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.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); range2.getCharacterFormat().setFontName("Arial"); range2.getCharacterFormat().setFontSize(10f); } }
Step 4:合併單元格
table.applyVerticalMerge(0,1,2);
table.applyVerticalMerge(0,4,5);
Step 5:插入圖片到單元格
DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
Step 6:設置單元格背景色
for (int j = 1; j < table.getRows().getCount(); j++) { if (j % 2 == 0) { TableRow row2 = table.getRows().get(j); for (int f = 1; f < row2.getCells().getCount(); f++) { row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144)); } } }
Step 7:設置表格邊框樣式
table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);
Step 8: 保存文檔
doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
表格建立效果:
所有代碼:
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.DocPicture; import com.spire.doc.fields.TextRange; import java.awt.*; public class CreateTable { public static void main(String[] args){ //建立Document對象 Document doc = new Document(); Section sec = doc.addSection(); //聲明數組內容 String[] header = {"班級","姓名","性別", "學號", "專業成績"}; String[][] data = { new String[]{"一班","王麗", "女", "Y1256486", "138"}, new String[]{"一班","洪菲菲", "女", "Y5425875", "134"}, new String[]{"二班","劉洋", "男", "B1546258", "141"}, new String[]{"三班","馮剛", "男", "B1542367", "136"}, new String[]{"三班","劉思源", "男", "Z1263547", "133"}, }; //添加表格 Table table = sec.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.ORANGE); 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); range1.getCharacterFormat().setTextColor(Color.white); } //寫入剩餘組內容到表格,並格式化數據 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.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); range2.getCharacterFormat().setFontName("Arial"); range2.getCharacterFormat().setFontSize(10f); } } //縱向合併指定單元格 table.applyVerticalMerge(0,1,2); table.applyVerticalMerge(0,4,5); //插入圖片到指定單元格 DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png"); dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //設置單元格背景顏色 for (int j = 1; j < table.getRows().getCount(); j++) { if (j % 2 == 0) { TableRow row2 = table.getRows().get(j); for (int f = 1; f < row2.getCells().getCount(); f++) { row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144)); } } } //設置表格邊框樣式 table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap); //保存文檔 doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013); } }
(本文完)
轉載請註明出處!