我是GWT的新手,在我的公司,我被要求在我们的某个应用程序中进行一些更改。 目前,该计划以.xls格式生成报告。现在我们要将其更改为.xlsx
public class Download extends HttpServlet {
private static final long serialVersionUID = 5580666921970339383L;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String filename = (String)request.getSession().getAttribute(CrossReportConstants.ATTR_FILENAME);
byte[] data = (byte[])request.getSession().getAttribute(CrossReportConstants.ATTR_REPORT);
request.getSession().setAttribute(CrossReportConstants.ATTR_FILENAME, null);
request.getSession().setAttribute(CrossReportConstants.ATTR_REPORT, null);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setContentLength(data.length);
try {
InputStream in = new ByteArrayInputStream(data);
ServletOutputStream out = response.getOutputStream();
byte[] outputByte = new byte[4096];
// copy binary contect to output stream
while (in.read(outputByte, 0, 4096) != -1) {
out.write(outputByte, 0, 4096);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我将response.setContentType("application/vnd.ms-excel");
更改为
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
我将文件名更改为exampleReport.xlsx 当我测试应用程序时,我可以下载报告,但我无法打开它 - Excel告诉我文件已损坏。 (文件大小合适,当我在文本编辑器中打开它时,我可以看到内容) 我错过了什么? 提前致谢