表格作為一種可視化交流模式及組織整理數據的手段,在各種場合及文檔中應用廣泛。常見的表格可包含文字、圖片等元素,我們操作表格時可以插入圖片、寫入文字及格式化表格樣式等。下面,將通過Java編程在Word文檔中創建表格或者嵌套表格,并實現格式化操作,包括設置字體、字號、字體顏色、字體粗細等,設置單元格對齊方式、單元格背景色、單元格合并、設置表格邊框樣式、插入圖片等。另外,本文也將介紹對表格中的行或者列的一些操作,包括添加行或列、復制行或列以及設置表格是否跨頁斷行等。
Free Spire.Doc for Java (免費版)
jar文件獲取及導入:
Jar文件可通過e-iceblue中文 官網獲取,下載后,解壓文件,將lib文件夾下的Spire.Doc.jar導入Java程序;也可以通過maven倉庫安裝導入 , 參考 導入方法 。導入效果如下:
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); } }
表格創建效果:
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.TextRange; public class NestedTable { public static void main(String[]args){ //加載測試文檔 Document doc = new Document("sample.docx"); //獲取指定表格中的單元格,并設置行高、列寬 Section sec = doc.getSections().get(0); Table table = sec.getTables().get(0); table.getRows().get(0).setHeight(120f); table.getRows().get(0).getCells().get(0).setWidth(380); //添加嵌套表格到指定單元格 Table nestedtable = table.get(0,0).addTable(true); nestedtable.getTableFormat().setHorizontalAlignment(RowAlignment.Center);//設置嵌套表格在單元格中的對齊方式 nestedtable.resetCells(4,4);//指定嵌套表格行數、列數 nestedtable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//設置嵌套表格內容自適應方法 //聲明表格數組內容 String[][] data ={ new String[]{"編號","產區","最新型號","生產日期",}, new String[]{"1","A","V2.2.0","2019-06-21"}, new String[]{"2","B","V2.6.1","2019-06-18"}, new String[]{"3","C","V2.6.2","2019-06-14"}, }; //填充數組內容到嵌套表格 for (int i = 0; i < data.length; i++) { TableRow dataRow = nestedtable.getRows().get(i); dataRow.getCells().get(i).setWidth(160); dataRow.setHeight(25); dataRow.setHeightType(TableRowHeightType.Exactly); for (int j = 0; j < data[i].length; j++) { dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]); range.getCharacterFormat().setFontName("楷體"); range.getCharacterFormat().setFontSize(11f); range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); } } //保存文檔 doc.saveToFile("nesedtable1.docx",FileFormat.Docx_2010); } }
嵌套表格添加效果:
3.1 添加行
import com.spire.doc.*; public class AddRow { public static void main(String[] args){ //加載測試文檔 Document doc = new Document(); doc.loadFromFile("sample.docx"); //獲取表格 Section section = doc.getSections().get(0); Table table = section.getTables().get(0); table.addRow();//默認在表格最下方插入一行 //table.getRows().insert(2,table.addRow());//在表格中第3行插入一行 //table.addRow(4);//默認在表格最下方添加4個單元格 //table.addRow(true,2);//帶格式在最后一行添加2個單元格 //table.addRow(false,2);//不帶格式在最后一行添加2個單元格 //保存文檔 doc.saveToFile("addrow.docx",FileFormat.Docx_2013); doc.dispose(); } }
行添加效果:
3.2 添加列
import com.spire.doc.*; import com.spire.doc.documents.BorderStyle; import java.awt.*; public class AddColumn { public static void main(String[] args){ //加載測試文檔 Document doc = new Document(); doc.loadFromFile("sample.docx"); //獲取表格 Section section = doc.getSections().get(0); Table table = section.getTables().get(0); //獲取表格單元格寬度及類型 float width = table.get(0,0).getWidth(); CellWidthType type = table.get(0,0).getCellWidthType(); //遍歷表格每一行 for (int i = 0; i < table.getRows().getCount(); i++) { TableRow row = table.getRows().get(i);//獲取表格每一行 Color color = row.getCells().get(0).getCellFormat().getBackColor();//獲取表格單元格背景色 //基于表格每行,在最后添加一個單元格,并設置單元格格式 TableCell cell = row.addCell(true);//默認在最后一列添加單元格 cell.setWidth(width); cell.setCellWidthType(type); cell.getCellFormat().getBorders().setBorderType(BorderStyle.Single); cell.getCellFormat().setBackColor(color); //如需在指定位置插入列,基于以上代碼并增加下面一行代碼即可 //row.getCells().insert(2,cell);//插入一列作為第三列 } //保存文檔 doc.saveToFile("addcolumn.docx", FileFormat.Docx_2013); doc.dispose(); } }
列添加效果:
4.1 復制行
import com.spire.doc.*; public class CopyRow { public static void main(String[] args) { //加載測試文檔 Document doc = new Document(); doc.loadFromFile("test.docx"); //獲取表格 Section section = doc.getSections().get(0); Table table =section.getTables().get(0); //復制第三行,并將復制后的行插入到表格作為第五行 TableRow row = table.getRows().get(2).deepClone(); table.getRows().insert(4,row); //保存文檔 doc.saveToFile("CopyRow.docx",FileFormat.Docx_2013); doc.dispose(); } }
表格行復制效果:
4.2 復制列
import com.spire.doc.*; public class CopyColumn { public static void main(String[] args) { //加載測試文檔 Document doc = new Document(); doc.loadFromFile("test.docx"); //獲取表格 Section section = doc.getSections().get(0); Table table =section.getTables().get(0); //遍歷表格每行 for (int i = 0; i < table.getRows().getCount(); i++) { //復制表格中每行的最后一個單元格,復制 TableRow row = table.getRows().get(i); TableCell cell = (TableCell) row.getCells().getLastItem().deepClone(); //row.getCells().add(cell);//默認在每行最后添加復制后的單元格 row.getCells().insert(2,cell);//在指定位置插入復制后的單元格 } //保存文檔 doc.saveToFile("CopyColumn1.docx",FileFormat.Docx_2013); doc.dispose(); } }
表格列復制效果:
這里通過兩種方式來設置防止表格跨頁出現斷行的效果,供參考。
1. 設置屬性禁止跨頁斷行
import com.spire.doc.*; public class PreventPagebreak { public static void main(String[]args){ //加載測試文檔 Document doc= new Document("test.docx"); //獲取表格 Table table = doc.getSections().get(0).getTables().get(0); //設置表格是否分頁斷行 table.getTableFormat().isBreakAcrossPages(false); //保存文檔 doc.saveToFile("result.docx",FileFormat.Docx_2013); } }
2. 保持表格內容在同一頁面
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class PreventPagebreak { public static void main(String[]args){ //加載測試文檔 Document doc= new Document("test.docx"); //獲取表格 Table table = doc.getSections().get(0).getTables().get(0); //遍歷表格單元格 for (int i = 0;i< table.getRows().getCount();i++) { TableRow rows = table.getRows().get(i); for (int j = 0; j< rows.getCells().getCount(); j++){ for (int z= 0; z < rows.getCells().get(j).getParagraphs().getCount();z++){ Paragraph p = rows.getCells().get(j).getParagraphs().get(z); p.getFormat().setKeepFollow(true);//設置表格內容在同一頁顯示 } } } //保存文檔 doc.saveToFile("result1.docx",FileFormat.Docx_2013); } }
(本文完)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。