ajax 下载文件(excel导出)

news/2024/7/21 4:56:07 标签: ajax, excel, okhttp
<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);
        }
    }


http://www.niftyadmin.cn/n/5297398.html

相关文章

从0到1入门C++编程——01 C++基础知识

文章目录 一、工具安装二、新建项目三、设置字体、注释、行号四、C基础知识1.数据类型2.输入输出3.运算符4.选择、循环结构5.跳转语句6.数组7.函数8.指针9.结构体 一、工具安装 学习C使用到的工具是Visual Studio&#xff0c;Visual Studio 2010旗舰版下载链接&#xff1a;点此…

【Docker-Dev】Mac M2 搭建docker的redis环境

Redis的dev环境docker搭建 1、前言2、官方文档重点信息提取2.1、创建redis实例2.2、使用自己的redis.conf文件。 3、单机版redis搭建4、redis集群版4.1、一些验证4.2、一些问题 结语 1、前言 本文主要针对M2下&#xff0c;相应进行开发环境搭建&#xff0c;然后做一个文档记录…

C语言中的难点

C语言把内存划分成四个区&#xff0c;它把一般的变量和数组等存在于内存中的栈区&#xff0c;全局变量在静态区若指针没有被初始化,那么指针可能会指向任何内存位置,这样可能会导致程序崩溃或者行为不确定&#xff0c;野指针就是指针指向的位置是不可知的&#xff08;随机的、不…

express的基础使用,利用postman模拟后端路由

简介 Node.js 使 JavaScript\TypeScript 脚本能够脱离浏览器环境在服务端&#xff08;后端&#xff09;运行&#xff08;实际上是对 Chrome V8 引擎进行了封装&#xff09;&#xff0c;为我们开发后端提供了一种选项。不像前端有统一的浏览器标准&#xff0c;如果不遵循的话浏…

【AI】人类视觉感知特性与深度学习模型(1/2)

目录 一、关于人类视觉感知 1.1 视觉关注 1.自上而下&#xff08;Top-down&#xff09;的视觉关注 ​编辑 2.自下而上&#xff08;Bottom-up&#xff09;的视觉关注 3.区别和记忆点 1.2 视觉掩盖 1.常见的视觉掩盖效应 2.恰可识别失真&#xff08;Just Noticeable Dif…

JavaScript 工具库 | PrefixFree给CSS自动添加浏览器前缀

新版的CSS拥有多个新属性&#xff0c;而标准有没有统一&#xff0c;有的浏览器厂商为了吸引更多的开发者和用户&#xff0c;已经加入了最新的CSS属性支持&#xff0c;这其中包含了很多炫酷的功能&#xff0c;但是我们在使用的时候&#xff0c;不得不在属性前面添加这些浏览器的…

Leetcode—1572.矩阵对角线元素的和【简单】

2023每日刷题&#xff08;七十三&#xff09; Leetcode—1572.矩阵对角线元素的和 实现代码 class Solution { public:int diagonalSum(vector<vector<int>>& mat) {int n mat.size();if(n 1) {return mat[0][0];}int sum 0;int i 0, j n - 1;while(i &…

UntiyShader(七)Debug

目录 前言 一、利用假彩色图像 二、利用Visual Studio 三、帧调试器 前言 Debug(调试),是程序员检查问题的一种方法,对于一个Shader调试更是一种噩梦,这也是Shade