jsp导出Excel的xlsx格式文件,打开提示文件格式或者扩展名错误

Caeser1248 2018-05-25 11:30:14
<!DOCTYPE html>
<%@ page contentType="application/msexcel;charset=utf-8" %>
<%@ page pageEncoding="UTF-8" language="java"%>
<%@ taglib uri="/tags/loushang-web" prefix="l"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<%@ page import="java.net.URLDecoder"%>
<meta http-equiv="Content-Type" content="application/msexcel; charset=utf-8">
<html lang="en">
<head>
<title>核验列表</title>
</head>
<body>
<table align="left" border="2">
<thead>
<tr >
<th>序号</th>
<th>机构名称</th>
<th>组织机构代码</th>
<th>统一社会信用码</th>
<th>机构库查询结果</th>
<th>代码中心查询结果</th>
</tr>
</thead>
<tbody>
<c:forEach items="${datacontextMap.data}" var="item" varStatus="status">
<tr>
<td><c:out value="${status.index+1}"/></td>
<td><c:out value="${item.organization}"/></td>
<td><c:out value="${item.organization_code}"/></td>
<td><c:out value="${item.organization_uscc}"/></td>
<c:choose>
<c:when test="${item.local_result== '1' }">
<td>不存在</td>
</c:when>
<c:when test="${item.local_result== '2' }">
<td>存在</td>
</c:when>
<c:otherwise>
<td>全部</td>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${item.unitcenter_result== '1' }">
<td>不存在</td>
</c:when>
<c:when test="${item.unitcenter_result== '2' }">
<td>存在</td>
</c:when>
<c:otherwise>
<td>全部</td>
</c:otherwise>
</c:choose>
</tr>
</c:forEach>
</tbody>
</table>
</body>
<% String ExcelName = "核验列表.xlsx";
ExcelName=new String(ExcelName.getBytes("GB2312"), "ISO_8859_1");//解决文件名为中文时文件错误的问题
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "inline; filename="
+ ExcelName);
%>
</html>

打开当前页面就会导出excel,报错信息如下图

如果导出xls格式是没有问题,header和ContentType这么写的话:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ ExcelName+".xls");
但是现在要求导出xlsx格式,求助大佬们,帮看看。
...全文
1761 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwn_2016 2018-05-25
  • 打赏
  • 举报
回复
用这个试试 response.setContentType("application/octet-stream");

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧