Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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 :
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 1-Aug-12 20:36pm
v2
Comments
Suvabrata Roy at 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 at 2-Aug-12 2:41am
   
ya please send me that..
 
Thanks
gautamamit8 at 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 at 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 at 9-May-14 6:41am
   
please send me the code
thanks in advance
zulfiker.zli786@gmail.com
Member 8273312 at 2-Aug-12 2:56am
   
yes please...
Member 10304510 at 17-Jan-14 1:04am
   
hi Suvabrata, pls send me the code as I need to complete this with ajax . Thanks in advance.
Isabella
isabellakhinmaythu@gmail.com
Ravinder Singh Bhawer at 25-Mar-14 0:50am
   
hi buddies ,
Please anybody send me code .
Thanks for the help in advance .
Email me at: rvsngh87@gmail.com
Member 10408402 at 21-Jun-14 8:18am
   
hi ...
please send me code ..
thanks for help
Email me : s.manunggal@gmail.com
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Following discussion should help: How to export html table to excel using javascript[^]
  Permalink  
Comments
Member 8273312 at 2-Aug-12 2:56am
   
I have used almost same code ..
Sandeep Mewara at 2-Aug-12 3:36am
   
Almost or exact? What happens when you do as it says?
Member 8273312 at 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 at 9-May-14 8:48am
   
write you custom code before opening of the file and follow the command.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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/[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 Smile | :)
  Permalink  
v2
Comments
ThePhantomUpvoter at 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 at 29-Aug-13 8:50am
   
there was a comment thats why i posted again..
ThePhantomUpvoter at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 231
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 29 Aug 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100