POI根据模板导出excel

这篇博客介绍了如何使用EasyPoi库处理Excel模板,包括数据遍历、条件判断、格式化输出等指令,如三目运算、格式化日期和数字、行列遍历等。同时提供了一个jersey框架的代码示例,展示如何将数据导出为Excel文件。
摘要由CSDN通过智能技术生成

下面列举下EasyPoi支持的指令以及作用,最主要的就是各种fe的用法

空格分割
三目运算 {{test ? obj:obj2}}
n: 表示 这个cell是数值类型 {{n:}}
le: 代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化数字 {{fn:(obj;###.00)}}
fe: 遍历数据,创建row
!fe: 遍历数据不创建row
$fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
#fe: 横向遍历
v_fe: 横向遍历值
!if: 删除当前列 {{!if:(test)}}
单引号表示常量值 '' 比如'1' 那么输出的就是 1
&NULL& 空格
]] 换行符 多行遍历导出
sum: 统计数据

 

模板示例:

导出示例:

代码示例:jersey框架

    @GET
    @Path("export")
    public Response export(@QueryParam("userId") String userId, @QueryParam("warehouseId") String warehouseId) {
        InputStream in = userService.export(userId, warehouseId);
        try {
            return Response.ok(in)
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + URLEncoder.encode("XX模板", "UTF-8") + ".xls").build();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
public InputStream export(String userId, String warehouseId) {
        Workbook workbook;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Map map = Maps.newHashMap();
        String templateUrl = getClass().getClassLoader().getResource(ExcelUtil.EXCEL_TEMPLATES_FOLDER).getPath() + "XX导出模板.xls";
        buildExpressChargeMap(userId, warehouseId, map);
 
        TemplateExportParams params = new TemplateExportParams(templateUrl, true);
        try {
            workbook = org.jeecgframework.poi.excel.ExcelExportUtil.exportExcel(params, map);
            workbook.write(outputStream);
            workbook.getSheetAt(0);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("export UserChargeTemplet error");
        }
        return new ByteArrayInputStream(outputStream.toByteArray());
    }


public void buildExpressChargeMap(String userId, String warehouseId, Map map) {
        List<Map<String, Object>> data = null;//查询数据
        map.put("upsList", data);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值