使用hutool导出excel文件

一、导包

compile 'cn.hutool:hutool-all:4.5.15'
compile 'org.apache.poi:poi-ooxml:3.17'

 

导出示例

        OutputStream out = new FileOutputStream("D:\\aa.xlsx");

        List<String[]> titleAliasList = new ArrayList<>();
        titleAliasList.add(new String[]{"startDate", "开始时间"});
        titleAliasList.add(new String[]{"endDate", "结束时间"});
        titleAliasList.add(new String[]{"page", "分页索引"});
        titleAliasList.add(new String[]{"size", "页元素大小"});


        List<DateQuery> rows = new ArrayList<>();
        rows.add(new DateQuery("ha1", "he5", 1, 5));
        rows.add(new DateQuery("ha2", "he4", 2, 4));
        rows.add(new DateQuery("ha3", "he3", 3, 3));
        rows.add(new DateQuery("ha4", "he2", 4, 2));
        rows.add(new DateQuery("ha5", "he1", 5, 1));

        String title = "myTitle";

        // 生成xlsx
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.renameSheet("mySheet");

        titleAliasList.forEach(arr -> writer.addHeaderAlias(arr[0], arr[1]));

        //合并单元格后的标题行,使用默认标题样式
        writer.merge(titleAliasList.size() - 1, title);

        // 设置标题样式
        StyleSet style = writer.getStyleSet();
        CellStyle headCellStyle = style.getHeadCellStyle();
        //水平居中
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        //设置内容字体
        Font font = writer.createFont();
        //加粗
        font.setBold(true);
        //设置标题字体大小
        font.setFontHeightInPoints((short)12);

        headCellStyle.setFont(font);


        // 一次性写出内容,使用默认样式,强制输出标题
        writer.write(rows, true);

        //out为OutputStream,需要写出到的目标流
        writer.flush(out);
        // 关闭writer,释放内存
        writer.close();

返回给前端下载

    public static void excelExport(String fileName, String sheetName, String title, List<HeaderTitle> titleAliasList, List<?> rows, HttpServletResponse response) throws IOException {
        // 生成xlsx
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.renameSheet(sheetName);

        titleAliasList.forEach(h -> writer.addHeaderAlias(h.getKey(), h.getName()));

        //合并单元格后的标题行,使用默认标题样式
        writer.merge(titleAliasList.size() - 1, title);

        // 设置标题样式
        StyleSet style = writer.getStyleSet();
        CellStyle headCellStyle = style.getHeadCellStyle();
        //水平居中
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        //设置内容字体
        Font font = writer.createFont();
        //加粗
        font.setBold(true);
        //设置标题字体大小
        font.setFontHeightInPoints((short)12);

        headCellStyle.setFont(font);

        // 一次性写出内容,使用默认样式,强制输出标题
        writer.write(rows, true);


        //导出数据
        OutputStream out = null;
        try {
            //设置Http响应头告诉浏览器下载这个附件
            response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
            response.setHeader("Content-Transfer-Encoding", "binary");
            out = response.getOutputStream();
            writer.flush(out, true);
            // 关闭writer,释放内存
            writer.close();
        } catch (Exception ex) {
            log.info("导出Excel异常,异常信息");
            throw new IOException("导出Excel异常,异常信息:" + ex.getMessage());
        } finally {
            //清理资源
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                log.info("清理资源错误");
            }
        }

    }

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Hutool工具库来导出Excel文件。下面是一个简单的示例代码: ```java import cn.hutool.core.date.DateUtil; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import java.util.ArrayList; import java.util.Date; import java.util.List; public class ExcelExportExample { public static void main(String[] args) { // 创建ExcelWriter对象 ExcelWriter writer = ExcelUtil.getWriter("D:/example.xlsx"); // 设置表头 writer.addHeaderAlias("id", "ID"); writer.addHeaderAlias("name", "姓名"); writer.addHeaderAlias("age", "年龄"); writer.addHeaderAlias("birthday", "生日"); // 设置内容 List<Person> personList = createTestData(); writer.write(personList, true); // 关闭writer,完成写入 writer.close(); } // 创建测试数据 private static List<Person> createTestData() { List<Person> personList = new ArrayList<>(); personList.add(new Person(1, "张三", 20, DateUtil.parse("2000-01-01"))); personList.add(new Person(2, "李四", 22, DateUtil.parse("1998-05-10"))); personList.add(new Person(3, "王五", 25, DateUtil.parse("1995-12-20"))); return personList; } // 定义Person类 static class Person { private Integer id; private String name; private Integer age; private Date birthday; // 省略构造方法、getter和setter } } ``` 上述代码通过HutoolExcelWriter类来创建一个ExcelWriter对象,并指定要导出文件路径。然后,使用addHeaderAlias方法设置表头别名,以及write方法将数据写入Excel文件中。最后,调用close方法关闭writer对象,完成写入操作。 请注意,需要先在项目中引入Hutool的依赖,例如: ```xml <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.10</version> </dependency> ``` 这样就可以使用Hutool导出Excel文件了。希望对你有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值