Java生成word文档的示例详解
作者:步尔斯特 时间:2022-02-11 19:37:47
目标
依赖
<!-- poi工具类-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
模版
实体
实体类需要和模版内的动态字段对应
代码
@GetMapping(value = "/all")
@SneakyThrows
public void table(All all, HttpServletResponse response) {
/* 假数据 */
all.setCode("2022-12-ABC");
all.setBuyer("步尔斯特航天有限公司");
String[][] strings = new String[100][5];
for (int i = 0; i < 100; i++) {
strings[i] = new String[]{i + "", "黄金", "100", "人民币", "125"};
}
all.setTableArray(strings);
// 模版路径
String wordPath = "/Users/issavior/java/java/seckill-redis/test/src/main/resources/";
String modelName = "all.docx";
String c = "购买方(Buyer): \n" +
"名称(Name):" + all.getBuyer() + "\n" +
"地址(Address):杭州市余杭区大东路100号\n" +
"电话(Tel.):021-20855588\n" +
"\n" +
"销售方(Seller):\n" +
"名称(Name):杭州火箭制造有限公司\n" +
"地址(Address):杭州市余杭区大东路919号\n" +
"电话(Tel.):021-20855553\n" +
"开户行(Bank):中国农业银行杭州支行\n" +
"账号(Account No.):033270-00873333567";
// 手绘表格
// 表头
RowRenderData row0 = Rows.of(c, "", "", "", "").textBold().create();
RowRenderData row1 = Rows.of("序号", "名称", "数量", "货币", "价格").center().create();
int length = 0;
if (all.getTableArray() != null) {
length = all.getTableArray().length;
}
// 表格数据 加上2行表头 再加上最后一行总价
RowRenderData[] rowRenderData = new RowRenderData[length + 3];
rowRenderData[0] = row0;
rowRenderData[1] = row1;
// 计算价钱
BigDecimal totalPrice = new BigDecimal("0");
for (int i = 0; i < length; i++) {
rowRenderData[i + 2] = Rows.of(all.getTableArray()[i]).center().create();
String s = all.getTableArray()[i][4];
BigDecimal bigDecimal = new BigDecimal(s);
totalPrice = totalPrice.add(bigDecimal);
}
RowRenderData row4 = Rows.of("总价:" + totalPrice, "", "", "小写", totalPrice.toString()).center().create();
rowRenderData[rowRenderData.length - 1] = row4;
// 表格合并,根据坐标
MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(0, 0), MergeCellRule.Grid.of(0, 4)).
// map(MergeCellRule.Grid.of(0, 1), MergeCellRule.Grid.of(1, 1)).
// map(MergeCellRule.Grid.of(0, 2), MergeCellRule.Grid.of(0, 3)).
// map(MergeCellRule.Grid.of(0, 4), MergeCellRule.Grid.of(1, 4)).
map(MergeCellRule.Grid.of(rowRenderData.length - 1, 0), MergeCellRule.Grid.of(rowRenderData.length - 1, 2)).
build();
TableRenderData table = Tables.of(rowRenderData).mergeRule(rule).create();
// 数据封装
all.setTableRenderData(table);
// 传入模板模板地址+信息数据
XWPFTemplate template = XWPFTemplate.compile(wordPath + modelName).render(all);
// 指定下载的文件名--设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("发票.docx", "UTF-8"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try {
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(out);
bos.flush();
out.flush();
template.close();
} catch (IOException e) {
e.printStackTrace();
}
}
来源:https://blog.csdn.net/CSDN_SAVIOR/article/details/128299107
标签:Java,生成,word
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot重写addResourceHandlers映射文件路径方式
2022-05-26 19:02:40
![](https://img.aspxhome.com/file/2023/3/66183_0s.jpg)
CentOS 7下JDK8的详细安装步骤
2022-02-18 12:06:28
![](https://img.aspxhome.com/file/2023/2/76302_0s.png)
IDEA中的.iml文件和.idea文件夹
2023-11-23 11:47:19
![](https://img.aspxhome.com/file/2023/0/59680_0s.png)
Java中包装类介绍与其注意事项
2023-03-20 18:26:36
![](https://img.aspxhome.com/file/2023/0/62810_0s.png)
Java反射之通过反射获取一个对象的方法信息(实例代码)
2021-08-07 15:14:46
![](https://img.aspxhome.com/file/2023/2/66532_0s.jpg)
聊聊Java Double相加出现的怪事
2023-07-22 22:46:39
SpringSecurity实现访问控制url匹配
2021-11-03 00:18:16
![](https://img.aspxhome.com/file/2023/1/63401_0s.jpg)
Java 超详细讲解类的定义方式和对象的实例化
2021-09-11 17:38:29
![](https://img.aspxhome.com/file/2023/9/70939_0s.png)
SpringIOC BeanDefinition的加载流程详解
2023-11-25 18:06:22
![](https://img.aspxhome.com/file/2023/8/59938_0s.png)
使用java从乱码文本中解析出正确的文本
2023-12-03 12:10:22
Java单例模式实现的几种方式
2021-06-09 18:38:47
java获取百度网盘真实下载链接的方法
2021-09-07 21:16:08
线程局部变量的实现 ThreadLocal使用及场景介绍
2023-11-10 03:19:26
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
Java @Async注解导致spring启动失败解决方案详解
2022-03-06 00:15:17
![](https://img.aspxhome.com/file/2023/9/69669_0s.jpg)
Spring Boot2如何构建可部署的war包
2023-11-29 06:40:59
![](https://img.aspxhome.com/file/2023/5/60365_0s.png)
使用Java的Lucene搜索工具对检索结果进行分组和分页
2022-07-27 05:21:17
![](https://img.aspxhome.com/file/2023/9/70929_0s.png)
spring data jpa使用详解(推荐)
2022-06-19 05:22:17
Java泛型中<?>和<T>的区别浅析
2023-11-25 05:20:09
深入浅析Java中普通代码块、构造代码块与静态代码块
2023-04-14 15:16:35
新手入门Jvm-- JVM对象创建与内存分配机制
2021-11-04 19:28:10
![](https://img.aspxhome.com/file/2023/4/66704_0s.png)