Java Spring安全性和ApachePOI问题
我有一个具有Spring安全性的JavaSpringWeb应用程序,用户在其中输入日期 根据该日期查询数据库,并通过HSSF工作簿写入ServletOutputStream将结果作为excel文件发送回用户浏览器 如果选择的日期为2013年12月28日至2014年1月4日、2014年1月12日至2014年1月18日、2014年1月19日至2014年1月25日或2014年1月26日至2014年2月1日,则发送给用户下载的文件将转换为contentType“text/html”,即使该文件已明确设置为“application/x-ms-excel” 生成的Excel文件已损坏,并且只包含奇怪的符号。这仅在包含Spring Security时发生,并且到目前为止,仅在列出的日期发生 如果有人有这个问题,你能告诉我你是怎么解决的吗 这将生成工作簿:Java Spring安全性和ApachePOI问题,java,spring,excel,spring-security,apache-poi,Java,Spring,Excel,Spring Security,Apache Poi,我有一个具有Spring安全性的JavaSpringWeb应用程序,用户在其中输入日期 根据该日期查询数据库,并通过HSSF工作簿写入ServletOutputStream将结果作为excel文件发送回用户浏览器 如果选择的日期为2013年12月28日至2014年1月4日、2014年1月12日至2014年1月18日、2014年1月19日至2014年1月25日或2014年1月26日至2014年2月1日,则发送给用户下载的文件将转换为contentType“text/html”,即使该文件已明确设置
public static HSSFWorkbook getExcelRemittance(HashMap<String,List> items)
{
List<ItemView> remit = items.get("remit");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet;
sheet = workbook.createSheet("Final Remittance");
Row header = sheet.createRow(0);
Cell cell1 = header.createCell(0);
cell1.setCellValue("DISNEY INVOICE NUM");
Cell cell2 = header.createCell(1);
cell2.setCellValue("EBI AMOUNT");
Cell cell3 = header.createCell(2);
cell3.setCellValue("PAYING AMOUNT");
Cell cell4 = header.createCell(3);
cell4.setCellValue("RESIDUAL AMOUNT");
Cell cell5 = header.createCell(4);
cell5.setCellValue("BOOKING #");
Cell cell6 = header.createCell(5);
cell6.setCellValue("GUEST NAME");
Cell cell7 = header.createCell(6);
cell7.setCellValue("NOTES/Dispute Reason");
int count = 1;
double total = 0.0;
for(ItemView v : remit)
{
Row row = sheet.createRow(count++);
Cell c1 = row.createCell(0);
c1.setCellValue(v.getDisneyInvoiceNum());
Cell c2 = row.createCell(1);
c2.setCellValue(v.getEbiAmount());
Cell c3 = row.createCell(2);
c3.setCellValue(v.getEbiAmount());
Cell c4 = row.createCell(3);
c4.setCellValue("-");
Cell c5 = row.createCell(4);
c5.setCellValue("");
Cell c6 = row.createCell(5);
c6.setCellValue("");
Cell c7 = row.createCell(6);
c7.setCellValue("");
total+=v.getEbiAmount();
}
Row subTotalRemit = sheet.createRow(sheet.getLastRowNum()+1);
int subTotalRow1 = sheet.getLastRowNum()+1;
Cell t = subTotalRemit.createCell(0);
t.setCellValue("subtotal");
Cell tAmt = subTotalRemit.createCell(1);
tAmt.setCellFormula("SUM(B2:B"+sheet.getLastRowNum()+")");
List<ItemView> past = items.get("prev");
Row prevHead = sheet.createRow(count+6);
Cell remitTitle = prevHead.createCell(0);
remitTitle.setCellValue("Invoices from previous batches");
CellStyle cs = workbook.createCellStyle();
Font f = workbook.createFont();
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
cs.setFont(f);
remitTitle.setCellStyle(cs);
Row prevCols = sheet.createRow(sheet.getLastRowNum()+1);
Cell ph1= prevCols.createCell(0);
ph1.setCellValue("Invoice Number");
Cell ph2= prevCols.createCell(1);
ph2.setCellValue("Invoice Amount");
Cell ph3= prevCols.createCell(2);
ph3.setCellValue("Paying Amount");
Cell ph4= prevCols.createCell(3);
ph4.setCellValue("Residual Amount");
Cell ph5= prevCols.createCell(4);
ph5.setCellValue("Booking #");
Cell ph6= prevCols.createCell(5);
ph6.setCellValue("Guest Name");
Cell ph7= prevCols.createCell(6);
ph7.setCellValue("Notes/Dispute Reason");
int sum1 = sheet.getLastRowNum()+1;
for(ItemView v : past)
{
Row row = sheet.createRow(sheet.getLastRowNum()+1);
Cell c1 = row.createCell(0);
c1.setCellValue(v.getDisneyInvoiceNum());
Cell c2 = row.createCell(1);
c2.setCellValue(v.getEbiAmount());
Cell c3 = row.createCell(2);
c3.setCellValue(v.getAmountPaid());
Cell c4 = row.createCell(3);
c4.setCellValue(v.getDiffEbiApAmt());
Cell c5 = row.createCell(4);
c5.setCellValue("");
Cell c6 = row.createCell(5);
c6.setCellValue("");
Cell c7 = row.createCell(6);
c7.setCellValue(v.getNotes());
}
Row subTotalPrev = sheet.createRow(sheet.getLastRowNum()+1);
Cell tP = subTotalPrev.createCell(1);
tP.setCellValue("subtotal");
Cell tAmtP = subTotalPrev.createCell(2);
tAmtP.setCellFormula("SUM(C"+sum1+":C"+sheet.getLastRowNum()+")");
Row gTotal = sheet.createRow(sheet.getLastRowNum()+1);
Cell gtP = gTotal.createCell(1);
gtP.setCellValue("Total");
Cell gtAmtP = gTotal.createCell(2);
gtAmtP.setCellFormula("B"+subTotalRow1+"+C"+(sheet.getLastRowNum()));
return workbook;
}
通过返回AbstractExcelView而不是重定向来修复此问题:
@RequestMapping(value="generateRemit")
protected View remitExcel( @RequestParam("date") String date)
{
final String finalDate = date;
return new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.addHeader("Content-Disposition", "attachment; filename=\""+finalDate+"Disney Remittance Final\"");
HashMap<String,List> items = jdbcBillingDao.getWeeklyRemit(finalDate);
HSSFSheet sheet = workbook.createSheet("Final Remittance");
@SuppressWarnings("unchecked")
List<ItemView> remit = items.get("remit");
Row header = sheet.createRow(0);
Cell cell1 = header.createCell(0);
cell1.setCellValue("DISNEY INVOICE NUM");
Cell cell2 = header.createCell(1);
cell2.setCellValue("EBI AMOUNT");
Cell cell3 = header.createCell(2);
cell3.setCellValue("PAYING AMOUNT");
Cell cell4 = header.createCell(3);
cell4.setCellValue("RESIDUAL AMOUNT");
Cell cell5 = header.createCell(4);
cell5.setCellValue("BOOKING #");
Cell cell6 = header.createCell(5);
cell6.setCellValue("GUEST NAME");
Cell cell7 = header.createCell(6);
cell7.setCellValue("NOTES/Dispute Reason");
//more spreadsheet generation
}
};
@RequestMapping(value=“generateRemit”)
受保护的Excel视图(@RequestParam(“日期”)字符串日期)
{
最终字符串finalDate=日期;
返回新的AbstractExcelView(){
@凌驾
受保护的无效文件(地图模型、HSSF工作簿、,
HttpServletRequest请求、HttpServletResponse响应)引发异常{
addHeader(“内容处置”、“附件;文件名=\”+finalDate+“最终版”);
HashMap items=jdbcBillingDao.getWeeklyRemit(finalDate);
HSSFSheet sheet=工作簿.createSheet(“最终汇款”);
@抑制警告(“未选中”)
列表汇款=项目。获取(“汇款”);
行标题=sheet.createRow(0);
Cell cell1=header.createCell(0);
cell1.设置CellValue(“迪士尼发票编号”);
Cell cell2=标头.createCell(1);
cell2.setCellValue(“息税前利润金额”);
Cell cell3=标头.createCell(2);
cell3.设置CellValue(“付款金额”);
Cell cell4=标头.createCell(3);
cell4.设置CellValue(“剩余金额”);
Cell cell5=标头.createCell(4);
cell5.设置CellValue(“预订”);
Cell cell6=标头.createCell(5);
cell6.setCellValue(“客人姓名”);
Cell cell7=标头.createCell(6);
cell7.setCellValue(“注释/争议原因”);
//更多电子表格生成
}
};
}
@RequestMapping(value="generateRemit")
protected View remitExcel( @RequestParam("date") String date)
{
final String finalDate = date;
return new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response) throws Exception {
response.addHeader("Content-Disposition", "attachment; filename=\""+finalDate+"Disney Remittance Final\"");
HashMap<String,List> items = jdbcBillingDao.getWeeklyRemit(finalDate);
HSSFSheet sheet = workbook.createSheet("Final Remittance");
@SuppressWarnings("unchecked")
List<ItemView> remit = items.get("remit");
Row header = sheet.createRow(0);
Cell cell1 = header.createCell(0);
cell1.setCellValue("DISNEY INVOICE NUM");
Cell cell2 = header.createCell(1);
cell2.setCellValue("EBI AMOUNT");
Cell cell3 = header.createCell(2);
cell3.setCellValue("PAYING AMOUNT");
Cell cell4 = header.createCell(3);
cell4.setCellValue("RESIDUAL AMOUNT");
Cell cell5 = header.createCell(4);
cell5.setCellValue("BOOKING #");
Cell cell6 = header.createCell(5);
cell6.setCellValue("GUEST NAME");
Cell cell7 = header.createCell(6);
cell7.setCellValue("NOTES/Dispute Reason");
//more spreadsheet generation
}
};