Java 在Word创建表格
程序员文章站
2022-03-20 08:58:19
表格作为一种可视化交流模式及组织整理数据的手段,在各种场合及文档中应用广泛。常见的表格可包含文字、图片等元素,我们操作表格时可以插入图片、写入文字及格式化表格样式等。下面,将通过Java编程在Word文档中创建表格并实现格式化操作,包括设置字体、字号、字体颜色、字体粗细等,设置单元格对齐方式、单元格 ......
表格作为一种可视化交流模式及组织整理数据的手段,在各种场合及文档中应用广泛。常见的表格可包含文字、图片等元素,我们操作表格时可以插入图片、写入文字及格式化表格样式等。下面,将通过java编程在word文档中创建表格并实现格式化操作,包括设置字体、字号、字体颜色、字体粗细等,设置单元格对齐方式、单元格背景色、单元格合并、设置表格边框样式、插入图片等。
使用工具:free spire.doc for java 2.0.0 (免费版)
jar文件导入
方法1:首先通过官网获取jar包。下载控件包并解压。
导入步骤:在程序中新建一个directory目录,并命名(本示例中命名为lib);将控件包lib文件夹下的spire.doc.jar文件(如下图1)复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”add as library”。完成导入(如下图2)。
图1:
图2:
方法2:通过maven导入。参考。
java代码示例(供参考)
step 1: 创建文档
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); } }
(本文完)
转载请注明出处!
下一篇: 谈网络赚钱的体会和感受