Java 在Word中创建表格(java,Word,创建,编程语言)

时间:2024-05-09 05:32:40 作者 : 石家庄SEO 分类 : 编程语言
  • TAG :

Word文档中,表格能使文本内容更加简洁明了,同时也能使数据展示更加清晰直观。本文将介绍如何使Java代码Word文档中创建表格设置其单元格的背景颜色

Jar文件导入方法

方法一:

下载免费Free Spire.Docfor Java包并解压缩然后从lib文件夹下,Spire.Doc.jar包导入到你的Java应用程序中。导入成功如下图所示

Java 在Word中创建表格

方法二:

通过Maven仓库安装导入详细的操作步骤请参考链接:

https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

Java代码示例

importcom.spire.doc.*;importcom.spire.doc.documents.*;importcom.spire.doc.fields.TextRange;importjava.awt.*;publicclassCreateTable{publicstaticvoidmain(String[]args){//创建Word文档Documentdocument=newDocument();//添加一个sectionSectionsection=document.addSection();//数据String[]header={"姓名","性别","部门","工号"};String[][]data={newString[]{"Winny","女","综合","0109"},newString[]{"Lois","女","综合","0111"},newString[]{"Jois","男","技术","0110"},newString[]{"Moon","女","销售","0112"},newString[]{"Vinit","女","后勤","0113"},};//添加表格Tabletable=section.addTable(true);//设置表格的行数和列数table.resetCells(data.length+1,header.length);//设置第一行作为表格的表头并添加数据TableRowrow=table.getRows().get(0);row.isHeader(true);row.setHeight(20);row.setHeightType(TableRowHeightType.Exactly);row.getRowFormat().setBackColor(Color.gray);for(inti=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);}//添加数据到剩余行for(intr=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.getCharacterFormat().setFontName("Arial");range2.getCharacterFormat().setFontSize(10f);}}//设置单元格背景颜色for(intj=1;j<table.getRows().getCount();j++){if(j%2==0){TableRowrow2=table.getRows().get(j);for(intf=0;f<row2.getCells().getCount();f++){row2.getCells().get(f).getCellFormat().setBackColor(newColor(173,216,230));}}}//保存文档document.saveToFile("创建表格.docx",FileFormat.Docx_2013);}}

创建表格效果图:

Java 在Word中创建表格


 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java 在Word中创建表格的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:细数10个隐藏在Python中的彩蛋下一篇:

10 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18