Java导出Excel的核心代码

在开发程序中,系统内经常为了用户数据编写方便,同时大批量进行数据的整理而进行的Excel工具的导出导入的工作。

以下是在开发程序过程中,经过验证的导出Excel的核心代码逻辑,通过使用EasyExcel来快速达到导出的目的。

{
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter excelWriter1 = EasyExcel.write(out).registerConverter(new LocalDateTimeConverter()).build();
        //设置head信息
        try {
            WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "页签的名字").head(heads).build();
            excelWriter1.write(dataStr, writeSheet1);
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes(),
                    "ISO8859-1"));
            out.flush();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (excelWriter1 != null) {
                excelWriter1.finish();
            }
        }
    }

注意: 关于head的设置,工具类中提供了2种载入的方式,上述代码使用的是List<List<String>>的方式,同时它还支持class的方式。

相关表格样式的调整未在上述代码中指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值