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);
    }

导出实现逻辑

java 复制代码
    @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);
        }
    }
相关推荐
用户03321266636715 小时前
Python 自动化办公:如何从零快速生成结构清晰的 Excel 报表
python·excel
千桐科技16 小时前
qData 数据中台专业版功能解析:告别 Excel 与 SQL 拼凑,打通系统合并的可追踪数据链路
数据库·sql·excel·qdata
百事牛科技18 小时前
从局部到整体:Excel限制功能的几种实用设置
windows·excel
ProcessOn官方账号1 天前
ProcessOn Office 插件上线:在 Word / Excel / PPT 里直接画流程图
word·excel·插件·office·processon
小羊没烦恼!2 天前
Ajax与JSON的一些总结
android·java·前端·数据库·mysql·算法·okhttp
reasonsummer2 天前
【办公类-53-12】20261016(预测)Python模仿制作2026学年第二学期校历(excel+pdf,上下学期都包含)
pdf·excel
泡海椒2 天前
JQuick-Excel 自定义 SPI Provider:用 fullName 扩展 TRANSFORM 函数
驱动开发·excel
泡海椒3 天前
从 XML 到 Excel:JQuick-Excel 代理与解析器如何协作
xml·excel
脉动数据行情3 天前
Java OkHttp 对接国际期货批量行情 SpringBoot 定时落库实践
java·spring boot·okhttp
优选资讯3 天前
Excel 批量打印标签的完整流程
excel