ajax 下载文件(excel导出)

<button class="btn btn-danger m-r-5" id="exportClick"
style="width: 100px;margin-left:10px;">日报表</button>

ajax 请求后端

    $("#exportClick").click(function () {
        var url = '${basePath}/rest/cart/export'
        console.log('url ' + url);
        var a = document.createElement('a')
        a.href = encodeURI(url)
        a.setAttribute('target', '_blank')
        a.download = "日报表.xlsx";
        a.click();
    });

后端代码

    @RequestMapping(value = "/export")
    public void export(HttpServletResponse response, HttpServletRequest request) {
        service.export(response, request);
    }

导出实现逻辑

    @Override
    public void export(HttpServletResponse response, HttpServletRequest request) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            //创建工作表sheeet
            XSSFSheet sheet = workbook.createSheet("数据处理结果");
            //创建第一行
            sheet.setColumnWidth(0, 3766);
            sheet.setColumnWidth(1, 3766);
            sheet.setColumnWidth(2, 5766);
            XSSFRow row = sheet.createRow(0);
            String[] title = {"日期", "营业额", "消费量"};
            XSSFCell cell_title;
            for (int i = 0; i < title.length; i++) {
                cell_title = row.createCell(i);
                cell_title.setCellValue(title[i]);
            }
            for (int i = 0; i < dayList.size(); i++) {
                XSSFRow createRow = sheet.createRow(i + 1);
                XSSFCell name = createRow.createCell(0);
                name.setCellValue(dayList.get(i));
                XSSFCell username = createRow.createCell(1);
                username.setCellValue(String.valueOf(amountList.get(i)));
                XSSFCell gender = createRow.createCell(2);
                gender.setCellValue(String.valueOf(countList.get(i)));
            }
            response.setHeader("content-disposition", "attachment;filename=day.xlsx");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOCloseUtils.close(inputStream);
            IOCloseUtils.close(outputStream);
        }
    }

相关推荐

  1. ajax 下载文件excel导出

    2024-01-01 09:36:02       40 阅读
  2. 前端下载导出文件流,excel/word/pdf/zip等

    2024-01-01 09:36:02       35 阅读
  3. Ajax + Easy Excel 通过Blob实现导出excel

    2024-01-01 09:36:02       8 阅读
  4. OSS 文件下载-Excel

    2024-01-01 09:36:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-01 09:36:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-01 09:36:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-01 09:36:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-01 09:36:02       18 阅读

热门阅读

  1. vue怎么跨页面传参

    2024-01-01 09:36:02       43 阅读
  2. 纯前端 文件预览方法汇总

    2024-01-01 09:36:02       44 阅读
  3. 一文详解pyspark常用算子与API

    2024-01-01 09:36:02       46 阅读
  4. 八股文打卡day16——计算机网络(16)

    2024-01-01 09:36:02       31 阅读
  5. 元旦假期的第二天:干家务

    2024-01-01 09:36:02       32 阅读
  6. git常用命令详解

    2024-01-01 09:36:02       34 阅读
  7. Debian安装k8s记录

    2024-01-01 09:36:02       31 阅读
  8. MySQL数据表加密字段支持模糊查询的方案

    2024-01-01 09:36:02       45 阅读
  9. 云原生Kubernetes系列 | Liveness和Readiness使用

    2024-01-01 09:36:02       37 阅读
  10. Prpmetheus监控rabbitmq

    2024-01-01 09:36:02       31 阅读