1

我正在关注mkyong Jexcel 和 Spring 教程,一切看起来都很好。它可以创建 excel 文件和写表,除了一件事是我不能更改我的 excel 文件名?它将显示与我的控制器链接相同的文件名。

这是示例

<li><h3><a href="report.html">Jexcel Showcase</a></h3></li>

它将始终创建 excel 文件名“report.html.xls”。谁知道怎么改文件名??

这是我的控制器

@RequestMapping(method=RequestMethod.POST)
protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        //dummy data
        revenueData.put("Jan-2010", "$100,000,000");
        revenueData.put("Feb-2010", "$110,000,000");
        revenueData.put("Mar-2010", "$130,000,000");
        revenueData.put("Apr-2010", "$140,000,000");
        revenueData.put("May-2010", "$200,000,000");
    return new ModelAndView("jexcelSuccess","revenueData",revenueData);
}

和 buildExcelDocument 方法

protected void buildExcelDocument(Map model, WritableWorkbook workbook,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

   Map<String,String> revenueData = (Map<String,String>) model.get("revenueData");
   WritableSheet sheet = workbook.createSheet("Revenue Report", 0);
   WritableSheet sheet2 = workbook.createSheet("Test Report", 1);
   WritableSheet sheet3 = workbook.createSheet("Hello Report", 2);

       sheet.addCell(new Label(0, 0, "Month"));
       sheet.addCell(new Label(1, 0, "Revenue"));

       int rowNum = 1;
   for (Map.Entry<String, String> entry : revenueData.entrySet()) {
    //create the row data
    sheet.addCell(new Label(0, rowNum, entry.getKey()));
        sheet.addCell(new Label(1, rowNum, entry.getValue()));
        rowNum++;
       }
   }

提前致谢, 玛特

4

1 回答 1

2

将此行添加到控制器:

response.setHeader("Content-Disposition", "attachment; filename=\"whatEver.xls\"");
于 2011-10-23T12:17:41.893 回答