PDA

View Full Version : Importing Data From Grid to Ms-Excel in Extjs 3.2



vineetmangal
6 Oct 2011, 6:49 AM
Hi
I want to Export data into Excel from Grid using Extjs 3.2.1 and latest browsers. I was able to get work this functionality in Extjs 3.0 and FF4 but not in latest browsers.
Can anyone help me to solve this.

daddie888
6 Oct 2011, 7:37 AM
Hi, see my answer on http://www.sencha.com/forum/showthread.php?145915-Can-Anyone-please-help-in-transfering-the-data-in-a-Ext-JS-grid-to-text-format
or
http://www.sencha.com/forum/showthread.php?116553-Urgent-Export-to-Excel-IE7&p=657026#post657026

I'm curious to your solution in the previous version for FF, can you post the code for that ?
Grtz

vineetmangal
6 Oct 2011, 10:26 PM
Hi,
Appreciate your efforts for "Importing data to Excel" development .I looked at your code ,I want to ask that will it support Extjs3.2.1 and IE8.
I am attaching the folder which is working in earlier browsers and Extjs
.28556

daddie888
7 Oct 2011, 12:32 AM
Hi, thanks, i looked at exporter.js before but id didn't work out of the box for Ext 3.3.1, clearly it does with 3.0.0.
My code works with Ext 3.3.1 and is tested with IE8.

I'll use the exporter approach tu produce a CSV file and see what it givers in 3.3.1, i keep you informed

Grtz

vineetmangal
7 Oct 2011, 2:14 AM
Hi daddie,
I checked that your code is working fine with Extjs3.0.0 , 3.1 and IE8 but when i tried it with Extjs 3.2.1 and 3.3.1 it saying that
" Automation server can't create object".
This problem is occurring when i trying to create object of ActiveXObject as-
var xlApp = new ActiveXObject("Excel.Application");

Can you help me for this.

daddie888
7 Oct 2011, 2:58 AM
Hi, that's very strange since that line has nothing to do with ExtJs or the version it is in.
Isolate and adapt the code to just make the ActiveX call and test it
I'm busy with a non ActiveX solution for both IE and FF

testActiveX.js



if (window.ActiveXObject){
var xlApp = new ActiveXObject("Excel.Application");
xlApp.Visible = true;
var xlBook = xlApp.Workbooks.Add();
xlBook.worksheets(1).activate();
var XlSheet = xlBook.activeSheet;
xlApp.visible = true;
XlSheet.cells(1, 1).value = 'test';
}

vineetmangal
7 Oct 2011, 3:35 AM
Hi,
Thanks a lot .I got solution this is happening bcz of security measure of Browser. If I will set security low then it works fine.
I am thinking that this will not be good effort to solve the problem instead using exporter.js will be good that will provide formatting the excel's cells also.
Give me your views.

daddie888
7 Oct 2011, 4:14 AM
You can do formatting this way too, actually is VBA code you are using so everyting you do in Excel itself should be possible. Exporter.js if fine but doesn't work in IE6 and 7 and you don't realy need it, here is a version that works without ActiveX in FF and IE6 and up. It's much easier to implement but lacks formatting.



bbar : [{
id : 'grid-excel-button',
text : 'Export to Excel...',
handler : function() {
var xd = toCSV(grid);
if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8) { //i'm not sure about IE8
window.clipboardData.setData('Text', xd); //confirmation will be asked
alert('Now, switch to Excel, position the cursor to where the data are to be inserted and paste the data.');
} else if (Ext.isSafari || Ext.isSafari2 || Ext.isSafari3){
//you are on your own here
} else {
document.location = 'data:application/vnd.ms-excel;base64,' + Base64.encode(xd);
}
}
}]
function toCSV(grid) {
var store = grid.getStore(), xlCol, field, result = '', separator = '';
for (xlCol = 0; xlCol < store.fields.length; xlCol++) {
field = store.fields.itemAt(xlCol).name;
result += separator+'"'+field+'"';
separator = '\t';
}
result += '\r';
store.each(function(record) {
separator = '';
for (xlCol = 0; xlCol < record.fields.length; xlCol++) {
var field = store.fields.itemAt(xlCol).name;
//you could make a difference for other types than text
result += separator+'"'+record.get(field)+'"';
separator = '\t';
}
result += '\r';
});
return result;
}

vineetmangal
10 Oct 2011, 3:41 AM
Hi,
Thanks for your help. Here I want to ask 1 thing as u told after getting values in csv format , have to switch to Excel and point to position to paste the data. In this case I need to create Excel object by ActiveXObject or by workbook(exporter.js). In case of ActiveXObject it will be again security measurements. Can you tell me
what to do.

daddie888
10 Oct 2011, 4:42 AM
In IE6 and IE7 (don't know about IE8) but not in IE9 the data uri won't work, so the sollution of exporter.js and mine with CSV won't work. Instead for these versions, for my solution, the user just has to start Excel manually, position the cursor and press paste. For the other browser this should work just like exporter.js but without formatting (could be added in theory).
This has advantages too since you can paste at any location you want instead of having a worksheet created and filled for you. This uses the clipboard and the user will have to give his permission but no ActiveX is needed (haven't tested it yet without ActiveX desactivated though)

BTW removed the line var w = window.open(....), isn't necessarry

Grtz