提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
遇到一个需要动态生成表格的PDF,并且需要骑缝章的业务,特此记录
提示:以下是本篇文章正文内容,下面案例可供参考
一、动态生成表格
采用spire.office,里面的spire.doc方法较多,可以满足大部分需求,而且对于轻度需求都是免费,废话不多说,直接上代码
二、使用步骤
1.引入库
代码如下(示例):
<!--免费版本的Spire.Office for Java v5.3.-->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.office.free</artifactId>
<version>5.3.1</version>
</dependency>
2.实例
代码如下(示例):
//创建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("C:\\Users\\Administrator\\Pictures\\Camera Roll\\技术负责人.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("E:\\Documents\\Word\\CreateTable.docx", FileFormat.Docx_2013);
添加Word表格行或列
//加载测试文档
Document doc = new Document();
doc.loadFromFile("E:\\Documents\\Word\\模板6.docx");
//获取表格
Section section = doc.getSections().get(0);
Table table = section.getTables().get(0);
//table.addRow();//默认在表格最下方插入一行
//table.addRow(4);//默认在表格最下方插入4个单元格数量的行
//table.getRows().insert(2,table.addRow());//在表格中第三行插入一行
//table.addRow(true,1);//带格式插入一行作为第2行
//table.addRow(false,1);//不带格式插入一行作为第2行
//插入数据
table.getRows().insert(4,table.addRow());
//table.applyHorizontalMerge(4,1,2);//调用方法横向合并第*行中的第2、3个单元格
TableRow dataRow = table.getRows().get(4);
//dataRow.setHeight(10);//行高
TableCell tableCell0 = dataRow.getCells().get(0);
// tableCell0.setWidth(400);//单元格宽度
Paragraph paragraph0 = tableCell0.addParagraph();
TextRange range0 = paragraph0.appendText("接闪器");
TableCell tableCell1 = dataRow.getCells().get(1);
// tableCell1.setWidth(1200);//单元格宽度
Paragraph paragraph1 = tableCell1.addParagraph();
paragraph1.appendText("接闪器类型");
TableCell tableCell2 = dataRow.getCells().get(2);
// tableCell2.setWidth(400);//单元格宽度
Paragraph paragraph2 = tableCell2.addParagraph();
paragraph2.appendText("杆、线、带(网)、金属屋面");
TableCell tableCell3 = dataRow.getCells().get(3);
// tableCell3.setWidth(400);//单元格宽度
Paragraph paragraph3 = tableCell3.addParagraph();
paragraph3.appendText("金属屋面");
TableCell tableCell4 = dataRow.getCells().get(4);
// tableCell4.setWidth(400);//单元格宽度
Paragraph paragraph4 = tableCell4.addParagraph();
paragraph4.appendText("合格");
//合并单元格
//table.applyVerticalMerge(0,3,5);
//保存文档
doc.saveToFile("E:\\Documents\\Word\\addrow.docx",FileFormat.Docx_2013);
doc.dispose();
总结
以上就是今天要讲的内容,本文仅仅简单介绍了spire.doc的使用,而spire.doc提供了大量能使我们快速便捷地处理数据的函数和方法,详情请参考:https://www.e-iceblue.cn/pdf_java_watermark/java-add-image-watermark-to-pdf.html