Exporting DataGrid As Excel

B

batista

Hi,

I have a web datagrid and i want to give user option
to export it as excel file.

How is it implemented?

Bye.
 
T

Tasos Vogiatzoglou

exportToExcel = function() {
var i;
var j;
var mycell;
var objXL;
try {
objXL = new ActiveXObject("Excel.Application");
if (objXL==null) {
alert("Excel must be installed");
return;
}
}
catch(e){
alert("Error creating Excel ActiveX ");
return;
}
var objWB = objXL.Workbooks.Add();
var objWS = objWB.ActiveSheet;

var tbl = actualTable; // here goes your table object

for (i=0; i < tbl.rows.length; i++) {
for (j=0; j < tbl.rows(i).cells.length; j++) {
mycell = tbl.rows(i).cells(j);
objWS.Cells(i+1,j+1).Value = mycell.innerText;

if ((mycell.innerText.indexOf("/") >= 0) && (mycell.innerText.length
<= 10))
{
objWS.Cells(i+1,j+1).NumberFormat = "dd/mm/yyyy";
objWS.Cells(i+1,j+1).HorizontalAlignment = -4108;
objWS.Cells(i+1,j+1).Font.color = mycell.style.color;
}
}
}

objWS.Range("A1", "L1").Font.Bold = true;
objWS.Range("A1", "Z1").EntireColumn.AutoFit();

objXL.Visible = true;
};

Something like this ...
 
B

batista

thnks for reply,

I have an aspnet datagrid. now at this line

var tbl = actualTable; // here goes your table object

i can get the object of aspdtagrid but how do i access
the rows length, cells length etc in the remaining lines.
I mean js is giving error.

And wat if i want to open a save file dialog when the user
clicks on the excel link.How do i implemet this?

Bye.
 
T

Tasos Vogiatzoglou

actualTable = the DOM object representing the table ...
document.getElementById('tableId');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top