重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

基与easyExcel实现excel模版导出-创新互联

Easyexcel官方文档

创新互联-专业网站定制、快速模板网站建设、高性价比武乡网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式武乡网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖武乡地区。费用合理售后完善,10多年实体公司更值得信赖。

excel模版位置:

模版数据填充处理:{.对应的实体对象字段名}

com.alibabaeasyexcel3.1.0

直接上代码:

public void export(HttpServletResponse response) throws IOException {
        ListcustomerComplaintCategoryVOList = DataTransformer.transform(customerComplaintService.selectCustomerComplaintDetailList(), CustomerComplaintDetailVO.class);
        if (CollectionUtils.isEmpty(customerComplaintCategoryVOList)) {
            throw new BusinessException("无可用数据,导出失败");
        }
        Resource resource = new ClassPathResource("template/客诉单导出模版.xlsx");
        String templatePath = resource.getFile().getPath();
        InputStream inputStream = ClassPathResource.class.getClassLoader().getResourceAsStream("template/客诉单导出模版.xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // URLEncoder.encode防止中文乱码
        String fileName = URLEncoder.encode("客诉单明细", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templatePath).build();
//        ExcelWriter excelWriter = EasyExcel.write("D:/Users/wsh/Desktop/客诉单导出模版.xlsx").withTemplate(inputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(customerComplaintCategoryVOList, writeSheet);
        // 关闭流
        excelWriter.finish();

该写法本地一点问题没有,放到服务器上,前端请求,报:找不到模版文件

经过一些了解,jar包的resource的位置发生改变,优化方案:

public void export(HttpServletResponse response) throws IOException {
        ListcustomerComplaintCategoryVOList = DataTransformer.transform(customerComplaintService.selectCustomerComplaintDetailList(), CustomerComplaintDetailVO.class);
        if (CollectionUtils.isEmpty(customerComplaintCategoryVOList)) {
            throw new BusinessException("无可用数据,导出失败");
        }
        InputStream inputStream = ClassPathResource.class.getClassLoader().getResourceAsStream("template/客诉单导出模版.xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // URLEncoder.encode防止中文乱码
        String fileName = URLEncoder.encode("客诉单明细", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream).build();
//        ExcelWriter excelWriter = EasyExcel.write("D:/Users/wsh/Desktop/客诉单导出模版.xlsx").withTemplate(inputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        excelWriter.fill(customerComplaintCategoryVOList, writeSheet);
        // 关闭流
        excelWriter.finish();

最终结果

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


新闻标题:基与easyExcel实现excel模版导出-创新互联
文章URL:http://cqcxhl.com/article/coeghj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP