最近用POI导出数据到excel,文件可以在浏览器下载,只是下载excel表格后缀名为.do形式。这里的do是我web.xml配置的filter过滤去的拦截方式,事实证明,这里配置什么拦截方式,后缀名就是什么。最后才发现是,我的响应头信息写错了。因为下载文件名为中文,各个浏览器的编码方式可能不同,文件名可能会乱码,所以网上搜了个工具类,于是写代码的时候没有正确导入,所以出现后缀名不正确的现象,以下附上完整代码。

工具类

public class fileUtilss {/*** 下载文件时,针对不同浏览器,进行附件名的编码** @param filename*            下载文件名* @param agent*            客户端浏览器* @return 编码后的下载附件名* @throws IOException*/public static String encodeDownloadFilename(String filename, String agent)throws IOException {if (agent.contains("Firefox")) { // 火狐浏览器filename = "=?UTF-8?B?"+ new BASE64Encoder().encode(filename.getBytes("utf-8"))+ "?=";filename = filename.replaceAll("\r\n", "");} else { // IE及其他浏览器filename = URLEncoder.encode(filename, "utf-8");filename = filename.replace("+"," ");}return filename;}

coltroller

 // 将数据导入到excle表格@RequestMapping("/customer/excel")@ResponseBodypublic void toExcel(HttpServletRequest request, HttpServletResponse response)throws Exception{System.out.print("=================================");List<Customer> customerList = customerService.getCustomerList();// 查询出 满足当前条件 结果数据//List<Customer> customerList = customerService.exportCustomer();// 生成Excel文件HSSFWorkbook hssfWorkbook = new HSSFWorkbook();HSSFSheet sheet = hssfWorkbook.createSheet("客户信息");sheet.setDefaultColumnWidth(10);sheet.setDefaultRowHeightInPoints(20);// 表头HSSFRow headRow = sheet.createRow(0);headRow.createCell(0).setCellValue("客户id");headRow.createCell(1).setCellValue("客户姓名");headRow.createCell(2).setCellValue("客户来源");headRow.createCell(3).setCellValue("客户所属行业");headRow.createCell(4).setCellValue("客户级别");headRow.createCell(5).setCellValue("固定电话");headRow.createCell(6).setCellValue("手机号");// 表格数据for (Customer customer : customerList) {HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);dataRow.createCell(0).setCellValue(customer.getCust_id());// 客户iddataRow.createCell(1).setCellValue(customer.getCust_name());// 客户姓名dataRow.createCell(2).setCellValue(customer.getCust_source());// 客户来源dataRow.createCell(3).setCellValue(customer.getCust_industry());// 客户所属行业dataRow.createCell(4).setCellValue(customer.getCust_linkman());    // 客户级别dataRow.createCell(5).setCellValue(customer.getCust_phone());  // 固定电话dataRow.createCell(6).setCellValue(customer.getCust_mobile()); // 手机号}// 下载导出// 设置头信息response.setContentType("application/vnd.ms-excel");String filename = "客户信息.xls";String agent = request.getHeader("user-agent");filename = fileUtilss.encodeDownloadFilename(filename, agent);response.setHeader("Content-Disposition","attachment;filename=" + filename);ServletOutputStream outputStream = response.getOutputStream();hssfWorkbook.write(outputStream);}

所需jar包

下载excel表格后缀名为.do形式相关推荐

  1. 前端下载excel表格文件——flie-saver导出类型为csv或者xlsx

    前言:这边导出表格数据是纯前端自己导出数据 下面方法可以导出格式为xlsx或者csv格式 首先要下载file-saver第三方依赖 npm install file-saver --save 然后直接 ...

  2. 点击按钮下载Excel表格

    网页点击按钮,下载Excel表格到本地 // 下载Excel表格 // table:页面需要下载的表格名 elem:下载按钮 tableName:下载后的报表名 function getExcelUr ...

  3. vue+element后台管理系统(下载excel表格)

    标题 vue后台管理系统下载excel表格 1.调用后台接口传入三个参数,url,data参数,第三个参数为一个对象,表格下载返回类型. 2.创建 Blob实列传入后台返回的数据和以对象形式声明类型参 ...

  4. 利用EasyExcel完整的springboot +vue前后端导出并下载excel表格

    文章目录 写在前面 正文 1. springboot后端引入easyexcel及使用 1.1 引入依赖 1.2 接口serviceImpl方法 1.3 提供一个对list集合去重的方法(根据相同key ...

  5. SpringBoot下载excel表格

    SpringBoot下载excel表格 git地址:https://gitee.com/benming-walnut/download-excel.git 1.目录结构 2.相关依赖 <pare ...

  6. vue下载excel表格模板和导入excel表格数据

    vue下载excel表格模板和导入excel表格数据 vue制作excel表格模板给前端下载 vue制作excel表格模板给前端下载 最近有个需求,需要导入excel表格,并且还需要制作模板给用户下载 ...

  7. Excel表格如何保存为网页形式

    Excel表格如何保存为网页形式 目录 Excel表格如何保存为网页形式 1.在"文件"选项卡中点击"另存为" 选择"其他格式" 2.在&q ...

  8. post方式下载excel表格

    post方式下载excel表格 request({method: 'post',url: this.exportUrl,data: data,responseType: 'blob' // 服务器返回 ...

  9. js下载excel表格,下载pdf

    常见我们会遇到下载一个excel表格模板或者下载一个可以查看的PDF文档. 下载excel表格或者下载一个pdf文档,主要是通过两点来实现的: 1- 在webpack里面配置loader 2- 设置下 ...

最新文章

  1. c++学习总结:extern声明全局变量
  2. 第12章:项目采购管理(2)-章节重点
  3. java scanner_Java Scanner nextLong()方法与示例
  4. JDK1.6版添加了新的ScriptEngine类,允许用户直接执行js代码。
  5. 【POJ - 1269 】Intersecting Lines (计算几何,直线间的位置关系)
  6. 如何实现两列等高效果?
  7. Windows核心编程条件变量
  8. C++ Web 编程 CGI公共网关接口(CGI):第一个 CGI 程序
  9. 关于同网段内主机之间通信细节
  10. 什么是 iPhone 和 Mac 上的“优化电池充电”功能?
  11. 计算机公式sin,三角函数计算公式大全
  12. U盘中毒文件被隐藏?U盘中毒了怎么恢复文件
  13. Python程序设计题库
  14. win7系统提示此windows副本不是正版怎么办?
  15. 【通信原理】揭开傅里叶级数与傅里叶变换的神秘面纱
  16. Mac OSX的开机启动配置
  17. greenplum 添加mirror
  18. 消逝的Coin:Stratum协议劫持技术
  19. php动态网站开发测评试题,智慧职教PHP动态网站开发单元测试答案
  20. iOS 8 Change List版本说明 What's New

热门文章

  1. 读懂 指令、程序、微命令、微操作、微指令、微程序、微地址
  2. 如果你想专升本那就一定要看的文学常识完整版(二)
  3. 浙江大学翁恺老师的城堡游戏源代码
  4. ***/BandwagonHost选择Linux操作系统的技巧
  5. Centos配置iptables开放ftp服务
  6. urho3D 运动逆解Inverse Kinematics
  7. perl实现根据序列ID从提取fasta文件序列
  8. uniapp 小程序实现 RSA 加密
  9. QString汉字个数检测
  10. 刷主板bios改变机器码_主板BIOS升级超完整教程,一学就会!