SpringBoot集成EasyExcel实现模板写入多个sheet导出

EasyExcel使用模板导出多个sheet

开发环境

SpringBoot2.6+EasyExcel3.2.1

复制代码
//第一种输出到指定目录
public static void main(String[] args) throws FileNotFoundException {
    InputStream inputStream = new FileInputStream(new File("模板位置"));
    InputStream inputStream1 = cloneSheet(inputStream, 0, "sheet11", "sheet12", "sheet13");
    //TODO 省略写入业务数据
    StreamUtil.inputStreamToFile(inputStream1, new File("导出数据指定输出位置"));
}

//第二种返回文件流,可用于上传文件资源服务器
public ByteArrayOutputStream export(){
	@Cleanup ByteArrayOutputStream os = new ByteArrayOutputStream();
		 List<String> stringList = Lists.newArrayList();
		 stringList.add("1");
		 stringList.add("2");
		//读取resource文件目录下的模板,打成jar包也能读取到
     	 InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("模板名称.xlsx");
        InputStream inputStream = cloneSheet(resourceAsStream, 0, stringList);
        ExcelWriter excelWriter = null;
        try{
         excelWriter = EasyExcel.write(os).withTemplate(inputStream).build();
         //遍历已经封装好要写入的模板的数据集合,一般一个对象写一个sheet
         for(int i =0;i<10;i++){
        	 excelWriter.fill(dtoList.get(i),writeSheet);
       	  }
        }finally{
         if(null != excelWriter){
                excelWriter.finish();
            }
        }
        return os;
}

/**
 * 克隆sheet
 * 注意传入的文件流在执行本方法后将关闭
 *
 * @param excelIns      excel文件流
 * @param tplSheetIndex 需要克隆的模板索引
 * @param newSheetNames 所需要生成的excel最终的sheet名
 * @return 新的InputStream
 */
@SneakyThrows
public static InputStream cloneSheet(InputStream excelIns, int tplSheetIndex, String... newSheetNames) {
//        Workbook workbook = isXlsx(excelIns) ? new XSSFWorkbook(excelIns) : new HSSFWorkbook(excelIns);
    Workbook workbook = new XSSFWorkbook(excelIns);
    Sheet tplSheet = workbook.getSheetAt(tplSheetIndex);//模板sheet
    for (int i = 0; i < newSheetNames.length; i++) {
        String sheetName = newSheetNames[i];
        if (0 == i) {//第一个,直接改名即可
            workbook.setSheetName(0, sheetName);
        } else {
            Sheet newSheet = workbook.cloneSheet(0);
            //同时复制打印设置
            PrintSetup tplPrintSetup = tplSheet.getPrintSetup();
            PrintSetup newPrintSetup = newSheet.getPrintSetup();
            newPrintSetup.setLandscape(tplPrintSetup.getLandscape());//打印方向,true:横向,false:纵向(默认)
            newPrintSetup.setPaperSize(tplPrintSetup.getPaperSize());//纸张类型

            int sheetIndex = workbook.getSheetIndex(newSheet);
            workbook.setSheetName(sheetIndex, sheetName);
        }
    }
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        workbook.write(bos);
        byte[] bytes = bos.toByteArray();
        InputStream ins = new ByteArrayInputStream(bytes);
        return ins;
    } finally {
        if (null != excelIns) excelIns.close();
    }
}
相关推荐
程序员爱钓鱼12 分钟前
Rust Trait Object详解:dyn Trait与动态分发
后端·面试·rust
chuan.bai5 小时前
Java RAG 实战(第 11 篇):RAG 知识工作台网页
java·开发语言·人工智能
0x538 小时前
网站通信(一)
java
To_OC8 小时前
Next.js + Redis 构建笔记系统:Redis Hash 实战与性能优化
redis·后端·next.js
Terra.K8 小时前
Java异常学习[特殊字符]
java·开发语言·学习
葡萄城技术团队9 小时前
InfluxDB 2\.x 深度解析:核心架构、Flux 函数与制造业落地指南(三)
java·开发语言·架构
Super 含9 小时前
Android 启动优化(五):线程、GC 与 IO 为什么会拖慢启动?
java·服务器·数据库
counting money9 小时前
Java IO流详解:从InputStream到文件操作实战
java·开发语言·python
程序员小八77710 小时前
上海百度B端java后端日常实习一面
java·开发语言
北斗落凡尘10 小时前
LangGraph 入门实战(11)--输出模式
后端·python·langchain