当前位置: 首页 > news >正文

XSSFWorkbook读取模板,批量填充并导出文件

1、pom文件导入

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.0</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.0</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.0</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.0</version>
</dependency>

2、读取模板并导出

@Datapublic static final class Example {private List<ExampleChild> child;private String orderNum;private String sortNum;private String barCode;}@Datapublic static final class ExampleChild {private String length;private String width;private String high;}//一个Example就是一个sheetprivate void handleData(HttpServletResponse response, List<Example> list) throws IOException {//读取模板String lastSheetName = list.get(0).getBarCode();//模板放在项目的 resource/templates/example.xlsxXSSFWorkbook workbook = new XSSFWorkbook(new ClassPathResource("templates/example.xlsx").getInputStream());//修改模板第一个sheet名称workbook.setSheetName(0, lastSheetName);//一个list就是一个sheetfor (int i = 1; i < list.size(); i++) {if(!lastSheetName.equals(list.get(i).getBarCode())){workbook.cloneSheet(0, list.get(i).getBarCode());lastSheetName = list.get(i).getBarCode();}else{workbook.cloneSheet(i-1);}}//模板转成存本地的临时文件String userHome = System.getProperties().getProperty("user.home") + "/example.xlsx";FileOutputStream fos = new FileOutputStream(userHome);workbook.write(fos);// 加载导出模板,excel模板的项目中路径ExcelTemplate excel = new ExcelTemplate(userHome);if (!excel.examine()) {return;}for (int l = 0; l < list.size(); l++) {LinkedHashMap<Integer, LinkedList<String>> rows = new LinkedHashMap<>();Map<String, String> fill = new HashMap<>();fill.put("barcode", "1");fill.put("sortNum", "2");fill.put("width", "3");fill.put("length", "4");fill.put("range", "5");fill.put("name", "6");try {// 第一个参数,需要操作的sheet的索引// 第二个参数,需要复制的区域的第一行索引// 第三个参数,需要复制的区域的最后一行索引// 第四个参数,需要插入的位置的索引// 第五个参数,填充行区域中${}的值// 第六个参数,是否需要删除原来的区域// 需要注意的是,行的索引一般要减一excel.addRowByExist(l, 3, 4, 5, rows, true);// 第一个参数,需要操作的sheet的索引// 第二个参数,替换sheet当中${<变量的值>}的值excel.fillVariable(l, fill);} catch (IOException e) {e.printStackTrace();}}String fileName = "样例文件"+new Date().getTime()+".xlsx";String fileNameURL = URLEncoder.encode(fileName, "UTF-8");response.setContentType("octets/stream");response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";" + "filename*=utf-8''" + fileNameURL);response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");try (OutputStream os = response.getOutputStream()) {// 删除临时文件File file = new File(userHome);file.deleteOnExit();os.write(excel.getBytes());os.flush();}}

示例:

http://www.lryc.cn/news/215411.html

相关文章:

  • Lazada新店运营秘籍自养号测评技术
  • python:逐像素处理遥感数据时间序列数据(求时间序列最大值、最大值所对应的索引、最大值所在的时间)
  • SpringCloudGateway--过滤器(自定义filter)
  • 【docker】安装 showdoc
  • 智慧公厕:科技赋予公共卫生新生命,提升城市管理品质
  • 深度学习_2 数据操作之数据预处理
  • 在美团和阿里6年,很难却也真实...
  • 2、NLP文本预处理技术:词干提取和词形还原
  • Fabric官方示例测试网络搭建
  • ubuntu20.04 conda pack 打包虚拟环境,直接将其用到其他终端
  • 云原生-AWS EC2使用、安全性及国内厂商对比
  • 【Proteus仿真】【Arduino单片机】简易电子琴
  • QT5.15.2 for Android 真机调试
  • Mysql my.cnf配置文件参数详解
  • linux下构建rocketmq-dashboard多架构镜像——筑梦之路
  • git,ssh,sourcetree代码管理
  • Jenkins中解决下载maven包巨慢的问题
  • Redis(11)| 持久化AOF和RDB
  • ZYNQ实验---IQ调制实现SSB PART2
  • 机器学习-特征工程
  • 大数据技术之集群数据迁移
  • CF1265E Beautiful Mirrors
  • 软件测试/测试开发丨利用ChatGPT自动生成架构图
  • Java学习笔记(六)——面向对象编程(基础)
  • 0基础学习PyFlink——个数滚动窗口(Tumbling Count Windows)
  • 车载终端构筑智慧工厂:无人配送车的高效物流体系
  • 插件_日期_lunar-calendar公历农历转换
  • 【FreeRTOS】【STM32】08 FreeRTOS 消息队列
  • 【计算机组成原理】CPU的工作原理
  • 部署ELK