目标

多级表头、分页、动态数据

实现

依赖:

        <!--  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相关推荐

  1. java导出word表格_使用PowerDesigner16.5 逆向数据库 导出word文档

    在上一篇<使用PowerDesigner16.5 逆向数据库生产表结构或导出word文档二>中,我们学会了使用PowerDesigner16.5怎么连接数据库,逆向生成表结构.有时候,我们 ...

  2. java导出word表格 行列合并

    生成word表格 新建模板如下(使用offic,不要用wps) 另存为xm格式 将文件放入resource/template目录下,复制一份,将后缀名改为ftl,也可以不复制直接改后缀名(这里忽视我其 ...

  3. java导出word表格

    List<ActivityMetting> list = activityMettingMapper.getWorkTotalDir(startDate,endDate,partyOrgN ...

  4. java 浏览器 excel导出excel_使用Java导出Excel表格并由浏览器直接下载——基于POI框架...

    非异步方法 /** * 使用Java导出Excel表格并由浏览器直接下载--基于POI框架 * * @param response * @return * @throws IllegalAccessE ...

  5. POI导出word表格 office打开没问题 wps打开列有问题

    POI导出word表格 office打开没问题 wps打开列有问题 模板样式 导出文档office打开 wps打开文件 1.给表格设置宽,指定宽度 2.将布局固定 3.动态设置单元格的宽度 4.重点设 ...

  6. java导出word文件损坏_记录一次POI导出word文件的细节问题

    首先百科一下POI是什么: Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.通过字面意思,我们大概知 ...

  7. poi设置word表格单元格宽度_poi导出word表格的操作讲解

    一.效果如下 二.js代码 function export_word(){ //导出word var url = "czzsca/exportWord.do"; this.expo ...

  8. java导出word的几种方式

    目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过J ...

  9. java导出word纸张方向_java导出word的几种方式

    目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过J ...

最新文章

  1. 未入选新一轮“双一流”!两所高校官宣
  2. asp实现301跳转的方法
  3. AtomicInteger源码注释
  4. 创建、添加字段IFields
  5. php 比java 快_php比java要快在哪里
  6. excel日期格式改不了_这一类型的Excel快捷键,为什么如此好用
  7. Objective-C 2.0 with Cocoa Foundation--- 9,内存管理
  8. WPF捕获事件即使这个事件被标记为Handled
  9. 关于listener
  10. linux下搭建radius服务器,CentOS下Radius服务器搭建
  11. 蓝桥杯基础练习 杨辉三角形Python实现
  12. US Domain Center 建站神器
  13. qlv是什么格式?要怎么把qlv格式转换成mp4视频
  14. 氧饱夹语音芯片,内置功放直推喇叭输出,低成本语音IC,WTN6系列
  15. mac如何开两个qq?
  16. JavaScript---js实现从本地上传图片
  17. STM32H743开发板移植micropython并外扩32M的SQPI flash和32M的SDRAM
  18. Qt处理传输协议数据时QByteArray添加多字节的使用案例
  19. 项目:广告点击预测率评估
  20. 软件测试八款优秀的API安全测试工具,会用三款工作效率能提升50%

热门文章

  1. 【深度优先搜索】种族部落
  2. 【跨境电商】WooCommerce vs Shopify:哪个更适合您的企业?
  3. 如何彻底关闭Antimalware Service Executable?
  4. syslog 华为 服务器_LINUX 操作记录到syslog,并发送到syslog服务器上
  5. Mac文件大清理 Xcode文件清理
  6. 【C#基础】初识编程语言C#
  7. 解决 windows10 蓝牙图标消失
  8. Mybatis与Spring集成原理
  9. 2019保研推免经验(清华计算机,软院,自动化;中科院计算所,自动化所)
  10. matlab曲线数据序列水平段数据自动识别并标注