Featured Posts

Hibernate 3 Annotations Tutorial This tutorial will walk through how to implement a hello world project using Hibernate Annotations and MySQL database. Requirements Download and unzip Hibernate Core...

Readmore

My DeveloperWorks: What's life like for a female Java... Just wanted to share with you my interview on Valerie's My developerWorks blog: Interview with Loiane Groner, Java developer in Brazil. I'm very happy, because this interview...

Readmore

Getting started with ExtJS in your J2EE project This tutorial will walk through how to get an Ext JS installation up and running quickly in your java (J2EE) project. Level: Basic This is the screenshot of this tutorial: First,...

Readmore

Tutorial: Getting Started with Spring Security This tutorial will cover a basic scenario where it  integrates Spring Security, using database-backed authentication, into an existing Spring web application. Spring...

Readmore

Integrating Spring Security with ExtJS Login Page This tutorial will walk through how to configure ExtJS Login form (Ajax login form) instead of default Spring Security login.jsp. Instead of using login.jsp from spring...

Readmore

Loiane Groner Rss

ExtJS: How to Export DataGrid to Excel

Posted on : 08-02-2010 | By : Loiane | In : ExtJS, Grid

6

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!

  • Share/Bookmark

Comments (6)

[...] 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.

Write a comment