Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All ,
I want to export html table to excel using JavaScript and i am done .
But when i am exporting it is directly showing to excel page, actually i want it to first it should show a dialog box with "Open","Save" and "Cancel" option . But i am not able to find the solution . please help me out for this .
JavaScript code is given below :
JavaScript
function write_to_excel() {
    str = "";
    var mytable = document.getElementById("tbExport");
    var rowCount = mytable.rows.length;
    var colCount = mytable.getElementsByTagName("tr")[0].getElementsByTagName("th").length;
    var ExcelApp = new ActiveXObject("Excel.Application");
    var ExcelSheet = new ActiveXObject("Excel.Sheet");
    //ExcelSheet.Application.Visible = true;
    for (var i = 0; i < rowCount; i++) {
        for (var j = 0; j < colCount; j++) {
            if (i == 0) {
                str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerText;
            }
            else {
                str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerText;
            }
            ExcelSheet.ActiveSheet.Cells(i + 1, j + 1).Value = str;
        }
    }
    ExcelSheet.autofit;
    ExcelSheet.Application.Visible = true;
    DisplayAlerts = true;
    CollectGarbage();
}

Thanks & Regards
Posted
v2
Comments
Suvabrata Roy 2-Aug-12 2:39am    
Hi,

Please don't use Active X Control directly from your web page.
some antivirus will black list your site.

you can use Ajax request...
do you need the code
Member 8273312 2-Aug-12 2:41am    
ya please send me that..

Thanks
gautamamit8 1-May-13 6:13am    
Hi Suvabrata, pls send me the code as I need to complete this with AJAX.
Thanks in advance.
Amit
gautamamit8@gmail.com
nicks2 29-Aug-13 2:07am    
Hi Suvabrata ,can You please send me the same code ? I also want to export mt html table data to excel file witout using active X Control.
EmailID : deepaligupta321@gmail.com
Thanks,
TS11 9-May-14 6:41am    
please send me the code
thanks in advance
zulfiker.zli786@gmail.com

Following discussion should help: How to export html table to excel using javascript[^]
 
Share this answer
 
Comments
Member 8273312 2-Aug-12 2:56am    
I have used almost same code ..
Sandeep Mewara 2-Aug-12 3:36am    
Almost or exact? What happens when you do as it says?
Member 8273312 2-Aug-12 3:48am    
It is working perfectly .Actually that excel sheet opening directly without asking to "Open","Save" and "Cancel" .
I want it should show first dialog box where it will ask for open,save and cancel and then we choose a option .
Suvabrata Roy 9-May-14 8:48am    
write you custom code before opening of the file and follow the command.
Hi,

This link will help you in creating Excel via ASP.

9 Solutions to Export Data to Excel for ASP.NET[^]

Now create a Asp page where you will send a Http X request to that particular page

and get the response and write it on a javascript popup.

If any problem revert me.

[Update]

http://stackoverflow.com/questions/2274173/how-to-pull-data-and-export-to-excel-within-an-ajax-framework?answertab=votes#tab-top[^]
http://npoi.codeplex.com/[^]
 
Share this answer
 
v2
Hi All,

First create a page where you will generate that file using your business logic.

Use this script as they assist : Link[^]

[Update] http://www.w3schools.in/jquery/export-to-excel/[^]

Enjoy :)
 
Share this answer
 
v2
Comments
[no name] 29-Aug-13 6:27am    
Really? You answered this over a year ago and now you are answering again? Why? Do you really think that he is still having the same problem from a year ago? Why not simply update your previous answer if you had some more information?
Suvabrata Roy 29-Aug-13 8:50am    
there was a comment thats why i posted again..
[no name] 29-Aug-13 10:20am    
And? So why did you not update your existing answer then? Why reply to the OP and not reply to the commenters? Do you think that they got a notification?
Suvabrata Roy 29-Aug-13 12:02pm    
Yes, I dont know if i modify the same solution then they may dont get any notification so I add another solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900