我有一个 jsf 应用程序,当用户单击一个按钮时,有一个支持 bean 方法调用这个 servlet。
问题是在IE7和IE8中,文件无法下载。它给出了错误消息:
Internet Explorer cannot download ExcelGenerator from mydomain.com
我需要做什么才能让它在 IE7/IE8 中工作?我需要设置一些额外的标题吗?
调用 servlet 的支持 bean 方法:
public void generateExcel()
{
FacesContext context = FacesContext.getCurrentInstance();
String url = context.getExternalContext().getRequestContextPath() + "/ExcelGenerator";
context.getExternalContext().redirect(url);
}
生成 excel 文件的 Servlet:
public class ExcelGenerator extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(ExcelGenerator.class);
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SXSSFWorkbook wb = new SXSSFWorkbook();
Sheet sheet = wb.createSheet("Results");
List<List<TableData>> results = (List<List<TableData>>) request.getSession().getAttribute("results");
String tableName = (String) request.getSession().getAttribute("tableName");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String filename = tableName+"-"+df.format(date)+".xlsx";
OutputStream out = null;
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Expires:", "0"); // eliminates browser caching
response.setHeader("Content-Disposition", "attachment; filename="+filename);
int rowCounter = 0;
// Create the rows
for(List<TableData> l : results) {
int counter = 0;
Row row = sheet.createRow(rowCounter);
for(TableData td : l) {
Cell cell = row.createCell(counter);
// if we're on the first row, get the column description, else get the data
if(rowCounter == 0) {
cell.setCellValue(td.getColumnDescription());
} else {
cell.setCellValue(td.getData());
}
counter++;
}
rowCounter++;
}
try {
out = response.getOutputStream();
wb.write(out);
out.flush();
} catch (Exception e) {
LOG.debug(e.getStackTrace().toString());
} finally {
out.close();
wb.dispose();
}
}
}