ExtJS: How to Export DataGrid to Excel
Posted on : 08-02-2010 | By : Loiane | In : ExtJS, Grid
8
This tutorial will walk through how to export data from ExtJS DataGrid directly to Excel.
Sometimes the user wants to export the data from the datagrid to an excel file. There is an ExtJS third party plugin that does it for you.
There are some “issues” you have to be aware of before you start:
- It needs a browser that supports data URLs, such as Firefox, Opera and IE8.
- I tested it with the following ExtJS versions: 2.2.1, 3.0, 3.0.3 and 3.1, but it only worked with ExtJS 3.0. If anyone is using other ExtJS version and the plugin worked, please, let me know.
- It only works on the data in the Store – if you are using server-side paging, then perform this processing on the server. For quick and dirty conversion of a small table to Excel, this might be useful.
- If the data in the Store is volatile (It gets reloaded or edited), the data URL will have to be recalculated.
Let’s get started:
First, create a file in your projet and paste this content in it (I called it exporter.js): http://github.com/loiane/extjs-export-excel/blob/master/WebContent/js/exporter.js (this file has too many lines, that’s why I’m not going to past its content here).
Then, in your datagrid, add this code:
var linkButton = new Ext.LinkButton({
id: 'grid-excel-button',
text: 'Export to Excel'
});
//create the Grid
var grid = new Ext.grid.GridPanel({
bbar: new Ext.Toolbar({
buttons: [linkButton]
})
});
linkButton.getEl().child('a', true).href = 'data:application/vnd.ms-excel;base64,' +
Base64.encode(grid.getExcelXml());
And if you try to export the data, it will look like this:
Feel free to change the color, fonts. Take a look in the code and try to understand it to make the changes you want.
Ed Spencer also developed a similar plugin. The source code is cleaner than this one. The output is the same, though.
You can download my sample app from my GitHub repository (JEE project): http://github.com/loiane/extjs-export-excel
Happy coding!






[...] This post was mentioned on Twitter by Javascript News, Loiane Groner and Vinny Carpenter, Paulo Jr.. Paulo Jr. said: RT: @loiane: ExtJS: How to Export DataGrid to Excel http://bit.ly/910DSV #blog #post [...]
Nice post. Thanks
Good!
but how can we do with IE6?
is there any solution?
[...] did not find any plugin on ExtJS forum that works like export excel from grid; I mean, a plugin I do not need any server side code – I could use file upload plugin and do the [...]
The solution to generate excel grid it’s to generate on server side and make a response of the data to the client side…other wise you be stuck on IE version ….
Good Luck!
Great post, thanks for the compact code base.
Any idea how to control output filename or extension? XL is complaining about file type not being correct, perhaps it should be xlsx – but I don’t see how to control that.
Thanks,
DB.
Hi, I’m not aware wether you had tested this in IE8。 I came across a problem whose descrition was that “The data area transferred to system and to be called is too small”. I didn’t know why this problem existed when I tested in IE8.
Notice: This third party plugin can function well in IE6, IE7,FireFox。
I’ll appreciate it that you’ll you reply to me.
Thanks!
Hi , I’m not aware how to contro l the output file name.
I had tried many approches to do it, but not successfullt in the end.
Could you please tell me how to control the output file name.
What you will do will be appreciated by not me, but also Db.
Thanks!