Java导出word表格|poi-tl
目标
多级表头、分页、动态数据
实现
依赖:
<!-- 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")@SneakyThrowspublic 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();}}
}
Java导出word表格|poi-tl相关推荐
- java导出word表格_使用PowerDesigner16.5 逆向数据库 导出word文档
在上一篇<使用PowerDesigner16.5 逆向数据库生产表结构或导出word文档二>中,我们学会了使用PowerDesigner16.5怎么连接数据库,逆向生成表结构.有时候,我们 ...
- java导出word表格 行列合并
生成word表格 新建模板如下(使用offic,不要用wps) 另存为xm格式 将文件放入resource/template目录下,复制一份,将后缀名改为ftl,也可以不复制直接改后缀名(这里忽视我其 ...
- java导出word表格
List<ActivityMetting> list = activityMettingMapper.getWorkTotalDir(startDate,endDate,partyOrgN ...
- java 浏览器 excel导出excel_使用Java导出Excel表格并由浏览器直接下载——基于POI框架...
非异步方法 /** * 使用Java导出Excel表格并由浏览器直接下载--基于POI框架 * * @param response * @return * @throws IllegalAccessE ...
- POI导出word表格 office打开没问题 wps打开列有问题
POI导出word表格 office打开没问题 wps打开列有问题 模板样式 导出文档office打开 wps打开文件 1.给表格设置宽,指定宽度 2.将布局固定 3.动态设置单元格的宽度 4.重点设 ...
- java导出word文件损坏_记录一次POI导出word文件的细节问题
首先百科一下POI是什么: Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.通过字面意思,我们大概知 ...
- poi设置word表格单元格宽度_poi导出word表格的操作讲解
一.效果如下 二.js代码 function export_word(){ //导出word var url = "czzsca/exportWord.do"; this.expo ...
- java导出word的几种方式
目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过J ...
- java导出word纸张方向_java导出word的几种方式
目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过J ...
最新文章
- 未入选新一轮“双一流”!两所高校官宣
- asp实现301跳转的方法
- AtomicInteger源码注释
- 创建、添加字段IFields
- php 比java 快_php比java要快在哪里
- excel日期格式改不了_这一类型的Excel快捷键,为什么如此好用
- Objective-C 2.0 with Cocoa Foundation--- 9,内存管理
- WPF捕获事件即使这个事件被标记为Handled
- 关于listener
- linux下搭建radius服务器,CentOS下Radius服务器搭建
- 蓝桥杯基础练习 杨辉三角形Python实现
- US Domain Center 建站神器
- qlv是什么格式?要怎么把qlv格式转换成mp4视频
- 氧饱夹语音芯片,内置功放直推喇叭输出,低成本语音IC,WTN6系列
- mac如何开两个qq?
- JavaScript---js实现从本地上传图片
- STM32H743开发板移植micropython并外扩32M的SQPI flash和32M的SDRAM
- Qt处理传输协议数据时QByteArray添加多字节的使用案例
- 项目:广告点击预测率评估
- 软件测试八款优秀的API安全测试工具,会用三款工作效率能提升50%