后台代码,这是基础方法,还要根据使用调用
public static void exportExcel(HttpServletResponse response, String sheetName, String fileName, String[] header, List content)
throws IOException {// 清空response
response.reset();
// 设置response的Header
response.setHeader("Content-disposition",
"attachment;
filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xlsx");
response.setContentType("application/vnd.ms-excel;
charset=gb2312");
Sheet sheet = new Sheet(1, 0);
sheet.setSheetName(sheetName);
// 设置自适应宽度
sheet.setAutoWidth(Boolean.TRUE);
// 设置表头
List list = new ArrayList<>();
for (int i = 0;
i < header.length;
i++) {
list.add(Collections.singletonList(header[i]));
}
sheet.setHead(list);
OutputStream outputStream = null;
ExcelWriter writer = null;
try {
outputStream = new BufferedOutputStream(response.getOutputStream());
writer = EasyExcelFactory.getWriter(outputStream);
List> data = https://www.it610.com/article/new ArrayList>();
for (int i = 0;
i < content.size();
i++) {
List
【java|Java导出Excel,提示格式与文件扩展名不一致】前端请求:
导出数据
download_file(){
const url = this.exportUrl+"?orderCode="+this.orderCode+"&productName="+this.productName+"&state"+this.state+"&productCode="+this.productCode+"&mobile="+this.mobile+"&commitTime="+this.commitTime+"&size=10000&page=1";
const token = store.getters.access_token
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader("Authorization", 'Bearer ' + token);
xhr.responseType = 'blob';
xhr.onload = function (e) {
if (this.status == 200) {
var blob = e.currentTarget.response;
var filename = `productOrderList.xls`;
//如123.xls
var a = document.createElement('a');
var url = URL.createObjectURL(blob);
a.href = https://www.it610.com/article/url;
a.download=filename;
a.click();
window.URL.revokeObjectURL(url);
}
};
xhr.send();
},
错误在于,,请求的数据时的名字为var filename = `productOrderList.xls`; //如123.xls 是.xls 而java 中的response.setHeader("Content-disposition",
"attachment; filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xlsx"); 为.xlsx 格式不统一,,我的问题就处在这,,,两个后缀统一就没问题了
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)