使用easyPoi根据模板导出excel

本文介绍了一个Java方法,通过官方代码示例展示了如何使用fe_map方法动态生成Excel模板,并处理数据映射和单元格合并问题。重点在于地图数据的灵活插入和Excel格式的精确控制。

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

官方代码示例:

@Test
    public void fe_map() throws Exception {
        TemplateExportParams params = new TemplateExportParams(
                "WEB-INF/doc/专项支出用款申请书_map.xls");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date", "2014-12-25");
        map.put("money", 2000000.00);
        map.put("upperMoney", "贰佰万");
        map.put("company", "执笔潜行科技有限公司");
        map.put("bureau", "财政局");
        map.put("person", "JueYue");
        map.put("phone", "1879740****");
        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("id", i + 1 + "");
            lm.put("zijin", i * 10000 + "");
            lm.put("bianma", "A001");
            lm.put("mingcheng", "设计");
            lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
            lm.put("quancheng", "开源项目");
            lm.put("sqje", i * 10000 + "");
            lm.put("hdje", i * 10000 + "");

            listMap.add(lm);
        }
        map.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        //下载到本地
        FileOutputStream fos = new FileOutputStream("D:/excel/专项支出用款申请书_map.xls");
        workbook.write(fos);
        fos.close();

       //输出文件流
        //        ByteArrayOutputStream os = new ByteArrayOutputStream();
//        ByteArrayInputStream is = null;
//
//        try {
//            try {
//                wb.write(os);
//            } catch (IOException var21) {
//                var21.printStackTrace();
//            }
//
//            byte[] content = os.toByteArray();
//            is = new ByteArrayInputStream(content);
//        } finally {
//            try {
//                os.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        }
//
//        OutputStream out = null;
//        InputStream in = is;
//        try {
//            //设置文件MIME类型
//            String fileName = (String) map.get("fileName");
//            response.setContentType("application/octet-stream");
//            try {
//                fileName = new String((fileName).getBytes("gb2312"), "ISO8859-1");
//            } catch (UnsupportedEncodingException e1) {
//                e1.printStackTrace();
//            }
//            //设置Content-Disposition
//            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//            out = response.getOutputStream();
//            int b;
//            while ((b = in.read()) != -1) {
//                out.write(b);
//            }
//            out.flush();
//            in.close();
//            out.close();
//        } finally {
//            try {
//                if (null != in) {
//                    in.close();
//                }
//                if (null != out) {
//                    out.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
    }

坑坑坑坑坑坑坑坑坑坑坑坑

1.fe的写法 fe标志 冒号 list数据 单个元素数据(默认t,可以不写) 第一个元素
{{KaTeX parse error: Expected 'EOF', got '}' at position 20: …maplist t t.id }̲}--------------…fe: maplist t.id }}

2.如果遍历的数据下面没有内容,用{{fe: maplist t.id }},不用{{$fe: maplist t.id }}

3.遍历数据后可能存在样式问题,如模板单元格合并了,但是导出来的excel并没有合并,需要手动处理

 //发票sheet
                Sheet fpSheet = wb.getSheetAt(1);
                for (int i = 15; i < 15 + size; i++) {
                    if(!isMergedRegion(fpSheet, i, 1)) {
                        CellRangeAddress nsTwelveFpSpmc = new CellRangeAddress(i, i, 0, 1);  //合并商品名称单元格
                        fpSheet.addMergedRegion(nsTwelveFpSpmc);
                    }
                }


 /**
     * 判断指定的单元格是否是合并单元格
     * @param sheet
     * @param row 行下标
     * @param column 列下标
     * @return
     */
    private  boolean isMergedRegion(Sheet sheet,int row ,int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if(row >= firstRow && row <= lastRow){
                if(column >= firstColumn && column <= lastColumn){
                    return true;
                }
            }
        }
        return false;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值