Jaikrat
Jaikrat

Reputation: 1154

response.flushBuffer() giving me an error while opening excel file programmatically at client side

I have to export data from my jqdrid to excel file. I am able to prepare the data in string format in my javaascript code like below

"1\tRamu Kaka\tShanta Ben\tN Modi\tSome clerk\tCustomer\tECP\tFunding\t80.00\tTBill\tMinus\t1,111\t\t\t01-01-1900\t01-01-1900\t\t\tSubmitted\t01-01-1900\tYes\t\t;2\tRamu Kaka\tShanta Ben\tN Modi\tSome clerk\tCustomer\tECP\tFunding\t80.00\tTBill\tMinus\t1,111\t\t\t01-01-1900\t01-01-1900\t\t\tSubmitted\t01-01-1900\tYes\t\t;....."

and able to get the same in my MVC controller like below.

@RequestMapping(value = "/exportToXLS", method = RequestMethod.GET)
public ModelAndView exportToXL(@RequestParam("data") String data,
        HttpServletRequest request, HttpServletResponse response,
        Model model) throws Exception {

    response.setHeader("Cache-Control", "public");
    response.setHeader("Pragma", "public");
    response.setHeader("Expires", "0");
    response.setHeader("Content-Length", Integer.toString(data.length()));
    String dateString = dtFormat.format(new Date());
    response.setHeader("Content-Disposition","attachment;    filename=\"FileName-" +dateString+ ".xls\";");
    response.setContentType("application/vnd.ms-excel");

    Workbook workbook =  myExcelUtility.exportTasks(Arrays.asList(data.split(";")));
    workbook.write(response.getOutputStream());     

    response.flushBuffer();//<--- here

    return null;
}

In my myExcelUtility.exportTasks(Arrays.asList(data.split(";"))); method I am preparing Workbook by populating all the cellItem, Style, values etc.

Upto here things are fine.

But the time I do response.flushBuffer();, My application's page get navigated to some URL and says

"Internet Explorer cannot display the webpage"

There url URL where it goes is

http://:5080/myApp/exportToXLS?data=1\tRamu Kaka\tShanta Ben\tN Modi\tSome clerk\tCustomer\tECP\tFunding\t80.00\tTBill\tMinus\t1,111\t\t\t01-01-1900\t01-01-1900\t\t\tSubmitted\t01-01-1900\tYes\t\t;2\tRamu Kaka\tShanta Ben\tN Modi\tSome clerk\tCustomer\tECP\tFunding\t80.00\tTBill\tMinus\t1,111\t\t\t01-01-1900\t01-01-1900\t\t\tSubmitted\t01-01-1900\tYes\t\t;.....

There is no error logs in my server console.

There can not be anything wrong in creating Workbook becase I also tried with

Workbook workbook =  new HSSFWorkbook();
workbook.write(response.getOutputStream());     
response.flushBuffer();

But this is also of no use.

Could you please help me to find out what and where I am making any mistake.

Upvotes: 1

Views: 631

Answers (2)

Joeblade
Joeblade

Reputation: 1743

Your content disposition contains a semicolon (;) at the end based on this link I think you should change:

response.setHeader("Content-Disposition","attachment;    filename=\"FileName-" +dateString+ ".xls\";");

it should be

response.setHeader("Content-Disposition","attachment; filename=\"FileName-" +dateString+ ".xls\"");

Also depending on how old the instance of IE is, you may need to change the content-type

Ancient browsers also required the following (not needed nowadays, but for a fool proof solution might be worth doing):

Content-Type header should be before Content-Disposition.
Content-Type header should refer to an unknown MIME type (at least until the older browsers go away).

This might not be needed anymore but it can be worth trying out. If the header is a known mime-type it might cause the browser to try to execute it.

Upvotes: 2

John Smith
John Smith

Reputation: 1579

One thing that I can see is that you're sending the wrong Content-Length. What you're doing is sending the character length of the "data" parameter, which is definitely not the length of the excel file binary size, which is what the Content-Length should actually be. Make sure you calculate it and send that instead.

The return type of the exportToXL method should be void, because you're not going to be using a spring view/model. You're writing the binary output yourself.

Upvotes: 0

Related Questions