天天看点

POI根据模板导出excel

下面列举下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: 统计数据

模板示例:

POI根据模板导出excel

导出示例:

POI根据模板导出excel

代码示例: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);
}