0

hi am create excel sheet using extends AbstractExcelView i write following code

public ModelAndView exportToExcel(HttpServletRequest request, @RequestParam Map<String, ? extends Object> params, ShipmentDetailsSearchInput shipmentDetailsInputType) throws ParseException
    {

        Map<String, Object> excelMap = new HashMap<String, Object>();
        return new ModelAndView("ExcelReport", UIErrorMessages.DATA, excelMap);
    }

i wont to get Ajax response that file download success in Extjs Ajax response because ModelAndView Not Give any response in submit ajax call.

how to we can write restful excel-download component without modeandview in spring 3

LaurentG
  • 11,128
  • 9
  • 51
  • 66
jayesh
  • 2,422
  • 7
  • 44
  • 78

4 Answers4

0

If you want to create dynamic excel file sheet then you can use Apache POI. You can create ,update and modify excel sheet.

pravin
  • 151
  • 2
  • 3
  • 12
  • in that case i wont to download both file and get status on one AJAX call that i not able to get – jayesh Aug 23 '13 at 12:44
0

I am using Apache POI to generate an excel file and filling it. More details can be found here, example link and another example link.

Please note you don't have to use Ajax and can directly map a GET request to the method exportToExcel.

To avoid return ModelAndView object, you can directly write the generated file into response stream. You will have to set ContentType and HttpServletResponse header to appropriate format, as show below:

public void exportToExcel(HttpServletRequest request, @RequestParam Map<String, ? extends Object> params, ShipmentDetailsSearchInput shipmentDetailsInputType, 
HttpServletResponse response) throws ParseException
    {

 //  Map<String, Object> excelMap = new HashMap<String, Object>();
 //  return new ModelAndView("ExcelReport", UIErrorMessages.DATA, excelMap);
      String fileName = "RandomFile";
      HttpHeaders header = new HttpHeaders();
      header.setContentType(new MediaType("application", "vnd.ms-excel"));
      response.setHeader("Content-disposition", "attachment; filename=" + fileName);
//method to create Workbook
      Workbook book = createFileAndFillDetails();

       try {
        book.write(response.getOutputStream());
    }
    catch (IOException e) {
        //Handle error
    }
}
aces.
  • 3,902
  • 10
  • 38
  • 48
0

If you have to use Ajax there are no direct solutions. The choices you are left with are :

1) Using a hidden frame as mentioned in How to download file from server using jQuery AJAX and Spring MVC 3

2) Redirect the browser

3) Don't use ajax at all, because ajax/jquery is not going to be useful to handle this scenario. A url with GET request offers easy approach as mentioned in the other solution proposed.

Community
  • 1
  • 1
aces.
  • 3,902
  • 10
  • 38
  • 48
0

If I undestand correctly your question, you want to create an excel spreadsheet when an asynchronous call is made to your controller (so via AJAX).

First of all, I don't think it's a good idea to download file via ajax call. If your problem is a long time to generate your excel spreadsheet, I suggest you to generate it asynchronously (in a separate thread for example) and than to download the generated file via a classical GET request.

I'm doing something similar in a web application that generate a large CSV file. I'm using this simple method to get the generated file:

@RequestMapping(value = "/file", method = RequestMethod.GET)
public void getFile(
        @RequestParam(value = "token", required = false) String token,
        HttpServletResponse response) throws Exception {

        // my logic...

        String formattedDataString = exportTask.getResults());

        response.setHeader("Content-Disposition", "attachment; filename=\""
                + exportTask.getExportFileName() + "\"");
        response.setContentType("text/plain; charset=utf-8");
        IOUtils.copy(
                new ByteArrayInputStream(formattedDataString.getBytes()),
                response.getOutputStream());
        response.flushBuffer();
    }
}

In my page I have a simple link like this:

<c:url var="fileUrl" value="results/file">
        <c:param name="token" value="${token}" />
    </c:url>
<a href='<c:out value="${fileUrl}" />'>Get File</a>

In my case the token param identify the generated file, that is stored in the local DB.

However, if you still want to use ajax, I suggest you to take a look to @ResponseBody annotation.

davioooh
  • 23,742
  • 39
  • 159
  • 250