1

I'm following with mkyong Jexcel with Spring tutorial and everything looks fine.it can create excel file and write sheet except one thing is I can't change my excel file name? It will display file name same as my link to controller.

Here is example

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

It will always create excel file name "report.html.xls". Anyone know how to change file name??

This is my controller

@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);
}

and buildExcelDocument method

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++;
       }
   }

Thank in advance, Mart

Vtanathip
  • 109
  • 2
  • 11

1 Answers1

2

Add this line to the controller:

response.setHeader("Content-Disposition", "attachment; filename=\"whatEver.xls\"");
Ralph
  • 118,862
  • 56
  • 287
  • 383