Java生成并下载Excel文件-工具类

时间:2021-07-22
本文章向大家介绍Java生成并下载Excel文件-工具类,主要包括Java生成并下载Excel文件-工具类使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.设计思路

Excel文件内容分为:表头和数据内容,需要下载Excel文件,先需要查询数据,将需要导出的数据生成Excel文件,然后才能通过输出流下载

2.代码实现

a. 给定fps路径、Excel文件名,新建一个空文件对象File,调用生成文件方法,调用FileUtil工具类将文件以流的形式写入response

public void exportTaskDetailFile(ErrHandleTaskReq req, HttpServletResponse response) throws IOException {
    String fileName = String.format("%s_%s.%s", req.getId(), System.currentTimeMillis(), "xlsx");
    String dirPath = String.format("%s/%s", ConfigUtil.getConf("fps.filepath"), LocalDate.now().format(WeDateUtils.FMT_DATE_SHORT));
    File dir = new File(dirPath);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    File excelFile = new File(String.format("%s/%s", dirPath, fileName));
    generateDetailFile(excelFile, req.getId());
    FileUtil.writeResponse(excelFile, response);
}

本文来自博客园,作者:冰枫丶,转载请注明原文链接:https://www.cnblogs.com/yydscn/p/15045436.html

原文地址:https://www.cnblogs.com/yydscn/p/15045436.html