java中excel文件下载

这篇具有很好参考价值的文章主要介绍了java中excel文件下载。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1、System.getProperty(user.dir) 获取的是启动项目的容器位置

2、 Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

  • StandardCopyOption.REPLACE_EXISTING 来忽略文件已经存在的异常,如果存在就去覆盖掉它
  • StandardCopyOption.COPY_ATTRIBUTES copy文件的属性,最近修改时间,最近访问时间等信息,不仅copy文件的内容,连文件附带的属性一并复制
     
    //获得要下载的excel的模板、其中mb.xlsx是模板
    String sourceFilePath = System.getProperty("user.dir") + File.separator + "mb.xlsx";
    //获得用户的当前工作目录
    String currentPath = System.getProperty("user.dir");
    //创建要生成的excel的路径
    String fileName1 = "mb" + System.currentTimeMillis();
    String destinationFilePath = currentPath + File.separator + fileName1 + ".xlsx";
    // 创建源文件和目标文件对象
    File sourceFile = new File(sourceFilePath);
    File destinationFile = new File(destinationFilePath);
    try {
        // 复制文件
        Files.copy(sourceFile.toPath(), destinationFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        System.out.println("文件复制成功!");
    } catch (IOException e) {
        e.printStackTrace();
    }
   //填充数据
    exportRawData(list, 1, destinationFilePath);
    String fileName = "分析-" + s + "月.xlsx";
    File file = new File(destinationFilePath);
    if (file.exists()) {
        String mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        response.setContentType(mimeType);
        response.setContentLength((int) file.length());
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        try (BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(file));
             BufferedOutputStream outStream = new BufferedOutputStream(response.getOutputStream())) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, bytesRead);
            }
        }
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
    Files.delete(Paths.get(destinationFilePath));
}

 填充数据的代码:文章来源地址https://www.toymoban.com/news/detail-645731.html

    boolean exportRawData(List<ImpactIndexTable> list, Integer type, String filePath) {
        String sheetName ="原始数据";
        // 要填充数据的起始行数
        int rowNum = 4;
        // 要填充数据的起始列数
        int colNum = 0;
        int rowTotal = 18;
        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheet(sheetName);
            if (sheet == null) {
                // 如果指定的工作表不存在,可以在这里进行处理
                log.info("指定的工作表不存在!");
            }
            for (int i = 0; i < list.size(); i++) {
                // 创建并设置单元格样式
                CellStyle style = workbook.createCellStyle();
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                SXSSFWorkbook wb = new SXSSFWorkbook(-1);
                // 获取要填充的行
                Row row = sheet.getRow(rowNum + i);
                if (row == null) {
                    row = sheet.createRow(rowNum + i);
                }
                for (int n = 0; n < rowTotal; n++) {
                    Cell cell = row.createCell(colNum + n);
                    switch (n) {
                        case 0:
                            cell.setCellValue(list.get(i).getProjectName());
                            cell.setCellStyle(style);
                            break;
                        case 1:
                            cell.setCellValue(list.get(i).getHw());
                            cell.setCellStyle(style);
                            break;
                         //后续按照需要填充数据
                 
                        default:
                    }

                }
            }
            // 保存修改后的Excel文件
            try (FileOutputStream fos = new FileOutputStream(filePath)) {
                workbook.write(fos);
                log.info("数据导出成功:{}", type);
                return true;

            }
        } catch (Exception e) {
            log.error("导出错误:{}", e);
        }
        return false;
    }

到了这里,关于java中excel文件下载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java设置可供下载的excel模板文件

    2024年01月19日
    浏览(24)
  • 苍穹外卖集成 Apache POI Java实现Excel文件的读写下载

    Apache POI - the Java API for Microsoft Documents Project News 16 September 2022 - POI 5.2.3 available The Apache POI team is pleased to announce the release of 5.2.3. Several dependencies were updated to their latest versions to pick up security fixes and other improvements. A summary of changes is available in the Release Notes. A full list of changes is a

    2024年02月09日
    浏览(44)
  • Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载

    业务需求:从数据库查询多个list集合信息封装excel,每个excel都有2个sheet页,填充不同的信息,最后将所有excel打包成zip文件,以流的形式返回给客户端,供客户端另存为窗口下载。 只发出一次请求 每个excel表中到数据记录不能超过2条 excel文件或者zip包不会上传服务器,而是

    2024年02月06日
    浏览(40)
  • vue excel文件下载

    这个文件是utils/index.js文件里面的,后面要用到。 里面可以传入一些参数和做了IE浏览器的判断,IE浏览器下载的时候,不能使用a标签,所以如果不做判断是没有反应的。 大概就是这些。

    2024年02月16日
    浏览(26)
  • 导出excel,导出模板Excel(双工作蒲)压缩文件导出(即下载文件)

    下面是文件表格和entity对象

    2024年02月12日
    浏览(31)
  • JavaScript下载excel文件

    对于已知地址的目标文件,前端可以使用 a标签 来直接下载,使用a标签下载使用到两个属性 download :下载文件名 href :目标文件下载链接 使用时给触发的div绑定事件 a标签的下载只能使用get请求,且无法在请求体中添加header信息 请求方法可用post或者get,responseType一般需要设

    2024年02月10日
    浏览(26)
  • 模版下载和Excel文件导入

    2024年02月13日
    浏览(31)
  • 后端:使用easyExcel实现解析Excel文件读取数据。前端:Excel模板下载、前端上传文件

            本篇是EasyExcel快速入门知识,讲解如何读取Excel文件,对Excel中错误信息如空字符、必填项为空、表格格式校验做到处理 ,并给出了实际项目中示例代码;为什么要使用easyexcel;原因是相比于poi,easyexcel更加轻量级,读取写入API方便,并且在工作中占用内存较小;

    2024年02月05日
    浏览(53)
  • excel下载后文件名称不对

    正确写法 错误写法 下面这种写法,下载的 excel 名称会有问题

    2024年02月11日
    浏览(36)
  • pom文件冲突引起的Excel无法下载

    报错提示信息: NoClassDefFoundError: Could not initialize class org.apache.poi.xssf.usermodel.XSSFWorkbook, 在最开始初始化的时候找不到对应的类,虽然我的Libraries里面是有的,ctrl也是能进去的,但就是找不进去 原因: 经过排查发现,是poi版本冲突了,之前的 poi版本是4.1.2,但是同事更新成

    2024年01月16日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包