Java实现导出word表格的示例详解
作者:步尔斯特 时间:2023-01-02 21:36:48
目标
多级表头、分页、动态数据
实现
依赖
<!-- poi工具类-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
模版
代码
TableData数据(模版对应的数据对象)
package org.example.bean;
import com.deepoove.poi.data.TableRenderData;
import lombok.Data;
@Data
public class TableData {
/**
* 标题
*/
private String title;
/**
* 表格
*/
private TableRenderData table;
private String[][] tableList;
/**
* 总价
*/
private String totalPrice;
}
核心代码
package org.example.controller;
import lombok.SneakyThrows;
import org.example.bean.TableData;
import org.springframework.web.bind.annotation.*;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
/**
* Java导出word表格
* 根据word模版,手绘表格
*/
@RestController
@RequestMapping(value = "/word")
public class WordController {
@GetMapping(value = "/table")
@SneakyThrows
public void table(TableData tableData, HttpServletResponse response) {
/* 假数据 */
tableData.setTitle("附件1-报价明细表");
String[][] strings = new String[100][5];
for (int i = 0; i < 100; i++) {
strings[i] = new String[]{"1", "EREWHON", "生猪", "酒鬼酒", "125"};
}
tableData.setTableList(strings);
// 模版路径
String wordPath = "/Users/issavior/java/java/seckill-redis/test/src/main/resources/";
String modelName = "表格.docx";
// 手绘表格
// 表头
RowRenderData row0 = Rows.of("项号", "编号", "种类", "", "价格").center().create();
RowRenderData row1 = Rows.of("项号", "编号", "期货", "股票", "价格").center().create();
int length = 0;
if (tableData.getTableList() != null) {
length = tableData.getTableList().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(tableData.getTableList()[i]).center().create();
String s = tableData.getTableList()[i][4];
BigDecimal bigDecimal = new BigDecimal(s);
totalPrice = totalPrice.add(bigDecimal);
}
RowRenderData row4 = Rows.of("总价", "", "", "", totalPrice.toString()).center().create();
rowRenderData[rowRenderData.length - 1] = row4;
// 表格合并,根据坐标
MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(0, 0), MergeCellRule.Grid.of(1, 0)).
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, 3)).
build();
TableRenderData table = Tables.of(rowRenderData).mergeRule(rule).create();
// 数据封装
tableData.setTable(table);
// 传入模板模板地址+信息数据
XWPFTemplate template = XWPFTemplate.compile(wordPath + modelName).render(tableData);
// 指定下载的文件名--设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("附件1-报价明细表.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/128295169
标签:Java,导出,word,表格
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入剖析Java中String类的concat方法
2023-06-02 07:07:13
JUnit5相关内容简介
2021-06-28 19:24:28
mybatis plus中如何编写sql语句
2021-09-21 15:27:55
![](https://img.aspxhome.com/file/2023/1/83121_0s.png)
httpwebreqeust读取httponly的cookie方法
2022-04-19 19:47:05
一篇文章教你如何用多种迭代写法实现二叉树遍历
2023-12-23 04:03:29
C# 使用Fluent API 创建自己的DSL(推荐)
2022-03-30 23:36:48
SpringMVC数据响应详细介绍
2023-09-24 04:12:41
![](https://img.aspxhome.com/file/2023/8/58568_0s.png)
Android绘制圆形百分比加载圈效果
2023-07-16 17:23:38
![](https://img.aspxhome.com/file/2023/1/137221_0s.gif)
C# 实现Table的Merge,Copy和Clone
2021-07-03 02:24:58
C#使用表达式树(LambdaExpression)动态更新类的属性值(示例代码)
2022-05-17 11:46:16
![](https://img.aspxhome.com/file/2023/2/88372_0s.png)
Java面向对象之抽象类,接口的那些事
2022-08-25 19:16:30
![](https://img.aspxhome.com/file/2023/0/61530_0s.jpg)
SpringCloud分布式链路跟踪的方法
2023-11-24 23:42:19
![](https://img.aspxhome.com/file/2023/6/59936_0s.png)
Android使用shape使组件呈现出特殊效果的方法
2022-06-28 03:42:27
![](https://img.aspxhome.com/file/2023/5/93265_0s.png)
Springboot整合log4j2日志全解总结
2021-12-27 22:56:58
![](https://img.aspxhome.com/file/2023/8/96068_0s.jpg)
Java实现人机猜拳游戏
2023-10-16 08:47:56
![](https://img.aspxhome.com/file/2023/5/80205_0s.png)
Java基于解释器模式实现定义一种简单的语言功能示例
2021-06-18 15:53:42
![](https://img.aspxhome.com/file/2023/2/72382_0s.png)
java高并发之理解进程和线程
2023-09-16 20:03:17
![](https://img.aspxhome.com/file/2023/2/108172_0s.jpg)
基于Idea+Jconsole实现线程监控步骤
2021-07-29 10:39:40
![](https://img.aspxhome.com/file/2023/4/61824_0s.png)
java selenium Selenium IDE介绍及用法
2023-11-21 06:41:22
![](https://img.aspxhome.com/file/2023/7/58717_0s.gif)
Java通过反射将 Excel 解析成对象集合实例
2023-05-22 18:49:02
![](https://img.aspxhome.com/file/2023/2/69942_0s.jpg)