[easypoi]模板导出

本文详细介绍了如何使用Easypoi在Spring Boot项目中,通过排除原项目POI依赖,创建并导入自定义模板,将集合数据精确映射到Excel工作表,包括了关键步骤如模板制作、数据填充和URL配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

easypoi 的模板导出

一.确定想实现的导出样式

在这里插入图片描述

二.添加依赖

因为原项目中有poi 引入,这里需要排除掉

<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>slf4j-log4j12</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml-schemas</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

三.制作excel 模板

1.代码中集合数据放于maplist中,模板中map的名字也需对应为maplist,注意集合开始处的写法:
{{$fe: maplist t.id

2.集合结束的写法:
t.startStation}}

3.不在集合中的数据赋值写法:
{{title}}

4.最后一个**{{remark233}}**特意赋了一个空字符串的值,目的是为了占位.
如果没有此行,集合数据与其他行数据会多处一行空白格.

在这里插入图片描述
将制作后的模板命名为port.xlsx,上传到服务器的某个位置,这样可以直接用url获取

四.代码实现

   @Value("${easypoi.template}")
    private String templat  

//下载输出流
    private  void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }
    
@Override
    public void download(Long id,HttpServletResponse response) throws IOException {
        PortPlanInfo   planInfo  =downInfo(id);
        List<PortBoxInfoDTO>  list=planInfo.getPortBoxInfoDtoList();
        TemplateExportParams templateExportParams = new TemplateExportParams(template);
        Map<String,Object> map = new HashMap<>();
        map.put("remark",(StringUtils.isBlank(planInfo.getRemark())? "":planInfo.getRemark()));
        map.put("connectPerson",planInfo.getConnectPerson()+"");
        map.put("connectPhone",planInfo.getConnectPhone()+"");
        map.put("title","集装箱铁路进港计划明细表");
        map.put("remark233","");
        List<Map<String,String>> mapList = new ArrayList<>();
        int i=0;
        if(list.size()>0){
            for (PortBoxInfoDTO portBoxInfoDTO : list) {
                Map<String,String> ml = new HashMap<>();
                ml.put("id", i + 1 + "");
                ml.put("carNum",portBoxInfoDTO.getCarNum()+"");
                ml.put("boxNum",portBoxInfoDTO.getBoxNum()+"");
                ml.put("boxGenus",portBoxInfoDTO.getBoxGenus()+"");
                ml.put("boxType",portBoxInfoDTO.getBoxType()+"");
                ml.put("goodsName",portBoxInfoDTO.getGoodsName()+"");
                ml.put("shipNameAndVoyageNum",portBoxInfoDTO.getShipNameAndVoyageNum()+"");
                ml.put("waybillNo",portBoxInfoDTO.getWaybillNo()+"");
                ml.put("receiverName",portBoxInfoDTO.getReceiverName()+"");
                ml.put("firstDischarge",portBoxInfoDTO.getFirstDischarge()+"");
                ml.put("startStation",portBoxInfoDTO.getStartStation()+"");
                mapList.add(ml);
                i++;
            }
        }
        map.put("maplist",mapList);

        String fileName = "detail";

        //正式环境
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams,map);
        downLoadExcel(fileName, response, workbook);

        //测试环境(导出到本地)
//        File saveFile = new File("D:/test/");
//        if (!saveFile.exists()){
//            saveFile.mkdir();
//        }
//        FileOutputStream fileOutputStream = null;
//        try {
//            fileOutputStream = new FileOutputStream("D:/test/本地文件.xlsx");
//            workbook.write(fileOutputStream);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            try {
//                fileOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }

在bootstrap.yml中配置模板的url

easypoi:
  template: 'http://www.xxx.cn/statics/template/port.xlsx'

//下载输出流

private  void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        workbook.write(response.getOutputStream());
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}

五.方法已实现,可以测试啦

easypoi是一个用于Excel和Word文档操作的Java库。它提供了简单易用的API,可以通过模板导出Excel文件。下面是使用easypoi进行模板导出的示例代码: 1. 导入easypoi的maven坐标: ```xml <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.2.0</version> </dependency> ``` 2. 在Spring Boot的配置文件(bootstrap.yml或application.yml)中配置模板的URL: ```yaml easypoi: template: 'http://www.xxx.cn/statics/template/port.xlsx' ``` 3. 使用easypoi进行模板导出: ```java import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import org.apache.poi.ss.usermodel.Workbook; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; public class TemplateExportDemo { public static void main(String[] args) throws IOException { // 模板文件的URL String templateUrl = "http://www.xxx.cn/statics/template/port.xlsx"; // 下载模板文件 URL url = new URL(templateUrl); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); // 加载模板文件 Workbook workbook = ExcelExportUtil.importExcel(inputStream); // 创建模板参数 TemplateExportParams params = new TemplateExportParams(); params.setSheetNum(0); // 指定导出的Sheet页 // 创建数据模型 Map<String, Object> dataModel = new HashMap<>(); dataModel.put("name", "John"); dataModel.put("age", 25); // 导出Excel文件 FileOutputStream outputStream = new FileOutputStream("output.xlsx"); ExcelExportUtil.exportExcel(params, dataModel, workbook.getSheetAt(params.getSheetNum()), outputStream); // 关闭流 outputStream.close(); inputStream.close(); } } ``` 这段代码会从指定的URL下载模板文件,然后根据模板和数据模型生成新的Excel文件。你可以根据自己的需求修改模板文件和数据模型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值