poi

ExtJs 4 Export to Excel (server-side)

I currently have a few grids (and just forms) in my app with a requirement to export to them to Excel. I am doing the export server side with Apache POI.

The html I have for my app is minimal b/c this is a single page app, using the Ext JS viewport, but I did add a hidden form and iframe to handle the export.


<body>
	<iframe id="exportframe" name="exportframe" class="x-hidden"></iframe>
	<form id="exportform" method="post" target="exportframe">
		<input type="hidden" id="excelconfig" name="config" value="" />
	</form>
</body>

Then in my javascript, I did the following:

// Following code lifted from downLoadFile method of Saki filetree - http://filetree.extjs.eu/
exportToExcel : function (params, url) {
	var id, frame, form, hidden, callback;

	frame = Ext.fly('exportframe').dom;
	frame.src = Ext.SSL_SECURE_URL;

	form = Ext.fly('exportform').dom;
	form.action = url;
	hidden = document.getElementById('excelconfig');
	hidden.value = Ext.encode(params);

	form.submit();
 }
 

These links were helpful for me:

The code from those links is dynamically adding and removing the iframe and form, but for some reason that I didn’t spend time on, the load events were not being handled in IE and I was ending up with a lot of extra iframes and forms in my dom. So, I just put the one directly in my jsp instead.

The exportToExcel method is in part of my overall Ext.app.Application, and I call it from various other locations in the javascript. params is a JavaScript object that I then deserialize server-side using Jackson.

My Spring 3.0 controller handles the request (in the example SearchBy is my class that I know the config param will deserialize into:

@Controller
@RequestMapping("/users")
public class UserSearchController {
	private static final ObjectMapper localMapper = new ObjectMapper();

	@RequestMapping(value="/xls")
	public void getXLS(HttpServletResponse response, @RequestParam("config") String config) {
			SearchBy searchBy = localMapper.readValue(config, SearchBy.class);

			// get the data from the db and do what is needed by POI to 
			// create the excel workbook...
	}
}

I found this tutorial very helpful:

I also looked at the Spring AbstractExcelView but I was having issues with Spring trying to resolve to a jsp view and although I would have liked to figure it out, I just moved on w/the solution above.

Update:
IE is particular about headers when exporting to Excel using https.
Helpful link: http://stackoverflow.com/questions/4672073/export-to-excel-doesnt-work-on-ie-under-ssl-https
Which suggests these http headers (which worked for me):

    Cache-Control: cache, must-revalidate
    Pragma: public
    Content-Type: application/vnd.ms-excel  // MIME type
    Content-Disposition: attachment; filename="excelDownload.xls"
    Content-Transfer-Encoding: binary
    Content-Length: 1024  // size as appropriate
Advertisements