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

easyexcel poi根据模板导出Excel

1.导入依赖

<!--    poi依赖-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.1</version>
</dependency>
<!--    poi对于excel 2007的支持依赖-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.1</version>
</dependency>
<!--    poi对于excel 2007的支持依赖-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.0.1</version>
</dependency>

2.代码实现

package com.example.exceldemo;import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;public class EasyExcelTest {public static void main(String[] args) throws Exception {InputStream fis = null;OutputStream outputStream= null;Workbook wb = null;try {String fileName = "测试单位"+ UUID.randomUUID() + ".xlsx";String filePath = "D:\\report\\"+fileName;//1.获取数据List<ExcelVo> list = new ArrayList<>();list.add(new ExcelVo(1,"1221","黄山"));list.add(new ExcelVo(2,"3333","河水"));list.add(new ExcelVo(3,"6666","青石"));//2.获取模板fis = new ClassPathResource("templates/report_template.xlsx").getInputStream();File file=new File(filePath);if (!file.exists()) {file.createNewFile();}outputStream = new FileOutputStream(file);//3.根据模板创建工作簿wb = new XSSFWorkbook(fis);//4.读取工作表Sheet sheet = wb.getSheetAt(0);CellStyle styles[] = new CellStyle[row.getLastCellNum()];//5.抽取第2行的公共样式 , 因为第一行 为标题 第2行是数据 下标为1Row row = sheet.getRow(1);CellStyle styles[] = new CellStyle[row.getLastCellNum()];Cell cell = null;for (int i = 0; i < row.getLastCellNum(); i++) {cell = row.getCell(i);styles[i] = cell.getCellStyle();}//5.构造单元格int rowIndex=1;//方式一 手动for (ExcelVo vo2:list) {//创建每一行,同excel的第二行开始row= sheet.createRow(rowIndex++);//第一列int i=0;cell = row.createCell(i++);cell.setCellStyle(styles[0]);cell.setCellValue(vo2.getId());//写入数据 序号//第二列cell = row.createCell(i++);cell.setCellStyle(styles[0]);cell.setCellValue(vo2.getCode());//第三列(注:最后一列不用i++,前面的必须i++)cell = row.createCell(i);cell.setCellStyle(styles[0]);cell.setCellValue(vo2.getName());}//方式二 动态for (ExcelVo t:list) {//创建每一行,同excel的第二行开始row = sheet.createRow(rowIndex++);Field[] fields=t.getClass().getDeclaredFields();//获取filedfor (int i = 0; i < fields.length; i++) {Field field=fields[i];field.setAccessible(true);//设置私有属性可以访问Object val = field.get(t);//获取值if (val==null)val="";cell = row.createCell(i);cell.setCellStyle(styles[0]);//设置单元格样式cell.setCellValue(val.toString());//写入数据}}wb.write(outputStream);//写入数据} catch (Exception e) {throw new Exception(e);} finally {fis.close();wb.close();outputStream.close();}}
}

3.模板

在这里插入图片描述

参考:https://blog.csdn.net/weixin_45742032/article/details/119593288?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-119593288-blog-86538258.235%5Ev38%5Epc_relevant_anti_t3_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-119593288-blog-86538258.235%5Ev38%5Epc_relevant_anti_t3_base&utm_relevant_index=2

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

相关文章:

  • 怎么把pdf图片转换成jpg?pdf转jpg的方法分享
  • MongoDB 双机热备那篇文章是 “毒”
  • Leetcode17电话号码的组合
  • 入职一家公司只会功能测试,如何进一步提升自己?
  • WordPress导航主题源码
  • 基于ADAU1452 DSP ANC和AEC算法的实现
  • Wireshark数据抓包分析之传输层协议(TCP协议)
  • ADRV9009子卡 设计原理图:FMCJ450-基于ADRV9009的双收双发射频FMC子卡 便携测试设备
  • Linux 桌面上的 Firefox 面临着大问题
  • 查漏补缺 - 构造函数,原型,this,原型链,继承
  • C# 学习笔记--个人学习使用 <2>
  • Linux网络编程Socket通信6-Libevent移植与使用
  • c#:委托 泛型委托的使用 泛型约束
  • 大数据之linux入门
  • MPI之MPI_Sendrecv接口以及空进程概念介绍
  • Revit SDK:PointCurveCreation 创建点来拟合曲线
  • 嵌入式Linux开发实操(十五):nand flash接口开发
  • vue2 组件库之vetur提示
  • 慕课网 Go工程师 第三周 package和gomodules章节
  • 【ES6】JavaScript 中的数组方法reduce
  • 数据结构--树4.2(二叉树)
  • 详解Numpy(基于jupyter notbook)
  • uniapp实现:点击拨打电话,弹出电话号码列表,可以选择其中一个进行拨打
  • swc-loader Segmentation fault “$NODE_EXE“ “$NPM_CLI_JS“ “$@“
  • Leetcode78. 子集
  • 百度“AI智障”到AI智能体验之旅
  • R中当并行运算遇到C++函数时,让foreach+Rcpp一起工作
  • 实现带头双向循环链表
  • Mysql 表字符集变更
  • golang抓取tcp包的实现