表格作為一種可視化交流模式及組織整理數(shù)據(jù)的手段,在各種場(chǎng)合及文檔中應(yīng)用廣泛授瘦。常見(jiàn)的表格可包含文字醋界、圖片等元素竟宋,我們操作表格時(shí)可以插入圖片、寫入文字及格式化表格樣式等形纺。下面丘侠,將通過(guò)Java編程在Word文檔中創(chuàng)建表格并實(shí)現(xiàn)格式化操作,包括設(shè)置字體逐样、字號(hào)蜗字、字體顏色、字體粗細(xì)等脂新,設(shè)置單元格對(duì)齊方式挪捕、單元格背景色、單元格合并争便、設(shè)置表格邊框樣式级零、插入圖片等。
使用工具:Free Spire.Doc for Java 2.0.0 (免費(fèi)版)
Jar文件導(dǎo)入
方法?1?:?首先通過(guò)?官網(wǎng)獲取?jar包滞乙。下載控件包并解壓妄讯。
導(dǎo)入步驟?:在程序中新建一個(gè)directory目錄,并命名(本示例中命名為lib)酷宵;將控件包lib文件夾下的Spire.Doc.jar文件(如下圖1)復(fù)制到程序中新建的目錄下。復(fù)制jar文件后躬窜,鼠標(biāo)右鍵點(diǎn)擊jar文件浇垦,選擇”Add as Library”。完成導(dǎo)入(如下圖2)荣挨。
圖?1?:
圖?2?:
方法?2?:?通過(guò)maven導(dǎo)入男韧。參考?導(dǎo)入方法?。
Java代碼示例(供參考)
?Step 1:創(chuàng)建文檔
Documentdoc = new Document();Sectionsec = doc.addSection();
Step 2:聲明數(shù)組內(nèi)容
//聲明數(shù)組內(nèi)容String[] header = {"班級(jí)","姓名","性別","學(xué)號(hào)","專業(yè)成績(jī)"};String[][] data =? ? ? ? {newString[]{"一班","王麗","女","Y1256486","138"},newString[]{"一班","洪菲菲","女","Y5425875","134"},newString[]{"二班","劉洋","男","B1546258","141"},newString[]{"三班","馮剛","男","B1542367","136"},newString[]{"三班","劉思源","男","Z1263547","133"},? ? ? ? };
Step 3:添加表格并寫入數(shù)據(jù)
//添加表格Tabletable = sec.addTable(true);table.resetCells(data.length +1, header.length);//設(shè)置表格第一行作為表頭默垄,寫入表頭數(shù)組內(nèi)容此虑,并格式化表頭數(shù)據(jù)TableRowrow = 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);Paragraphp = row.getCells().get(i).addParagraph();? ? p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);TextRangerange1 = p.appendText(header[i]);? ? range1.getCharacterFormat().setFontName("Arial");? ? range1.getCharacterFormat().setFontSize(12f);? ? range1.getCharacterFormat().setBold(true);? ? range1.getCharacterFormat().setTextColor(Color.white);}//寫入剩余組內(nèi)容到表格,并格式化數(shù)據(jù)for(int r =0; r < data.length; r++) {TableRowdataRow = table.getRows().get(r +1);? ? dataRow.setHeight(25);? ? dataRow.setHeightType(TableRowHeightType.Exactly);? ? dataRow.getRowFormat().setBackColor(Color.white);for(intc=0;c< data[r].length;c++) {? ? ? ? dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);TextRangerange2 = 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:設(shè)置單元格背景色
for (intj =1; j < table.getRows().getCount(); j++) {if (j%2==0) {? ? ? ? TableRow row2 = table.getRows().get(j);for (intf =1; f < row2.getCells().getCount(); f++) {row2.getCells().get(f).getCellFormat().setBackColor(newColor(144,238,144));}? ? }}
Step 7:設(shè)置表格邊框樣式
table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);
Step 8:保存文檔
doc.saveToFile("CreateTable.docx",FileFormat.Docx_2013);
表格創(chuàng)建效果:
全部代碼:
importcom.spire.doc.*;importcom.spire.doc.documents.*;importcom.spire.doc.fields.DocPicture;importcom.spire.doc.fields.TextRange;importjava.awt.*;publicclassCreateTable{? ? publicstaticvoidmain(String[] args){//創(chuàng)建Document對(duì)象Document doc =newDocument();? ? ? ? Section sec = doc.addSection();//聲明數(shù)組內(nèi)容String[] header = {"班級(jí)","姓名","性別","學(xué)號(hào)","專業(yè)成績(jī)"};String[][] data =? ? ? ? ? ? ? ? {newString[]{"一班","王麗","女","Y1256486","138"},newString[]{"一班","洪菲菲","女","Y5425875","134"},newString[]{"二班","劉洋","男","B1546258","141"},newString[]{"三班","馮剛","男","B1542367","136"},newString[]{"三班","劉思源","男","Z1263547","133"},? ? ? ? ? ? ? ? };//添加表格Table table = sec.addTable(true);? ? ? ? table.resetCells(data.length +1, header.length);//設(shè)置表格第一行作為表頭口锭,寫入表頭數(shù)組內(nèi)容朦前,并格式化表頭數(shù)據(jù)TableRow row = table.getRows().get(0);? ? ? ? row.isHeader(true);? ? ? ? row.setHeight(20);? ? ? ? row.setHeightType(TableRowHeightType.Exactly);? ? ? ? row.getRowFormat().setBackColor(Color.ORANGE);for(inti =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);? ? ? ? }//寫入剩余組內(nèi)容到表格,并格式化數(shù)據(jù)for(intr =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(intc =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);//設(shè)置單元格背景顏色for(intj =1; j < table.getRows().getCount(); j++) {if(j %2==0) {? ? ? ? ? ? ? ? TableRow row2 = table.getRows().get(j);for(intf =1; f < row2.getCells().getCount(); f++) {? ? ? ? ? ? ? ? ? ? row2.getCells().get(f).getCellFormat().setBackColor(newColor(144,238,144));? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }//設(shè)置表格邊框樣式table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);//保存文檔doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);? ? }}View Code
(本文完)
我自己是一個(gè)從事了6年的Java全棧工程師鹃操,最近整理了一套適合2019年學(xué)習(xí)的Java\大數(shù)據(jù)資料韭寸,從基礎(chǔ)的Java、大數(shù)據(jù)面向?qū)ο蟮竭M(jìn)階的框架知識(shí)都有整理哦荆隘,可以來(lái)我的主頁(yè)免費(fèi)領(lǐng)取哦恩伺。