Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a java script function that will export the data present in gridview into excel file .I am using activeXObjext to do so but I am getting the following error(Automation server can't create object).I am using internet explorer 11 and i have changed the internet option to allow activex but i ma getting the same error.

I have done this using htmltextwriter(asp.net) before but here I have to make use of ActiveXObject .I am not getting any solution for this .Please help.

thanks in advance.

What I have tried:

I am fetching the data from database into gridview and then trying to export it into excel sheet.Here is what I have tried.

HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"  runat="server">
    <title></title>
    <script type="text/javascript">
function writeToExcel2() {
            var i, j, str,
                myTable = document.getElementById('GridView1'),
                rowCount = myTable.rows.length,
                excel = new ActiveXObject('Excel.Application');// Activates Excel
            excel.Workbooks.Add(); // Opens a new Workbook
            excel.Application.Visible = true; // Shows Excel on the screen
            for (i = 0; i < rowCount; i++) {
                for (j = 0; j < myTable.rows[i].cells.length; j++) {
                    str = myTable.rows[i].cells[j].innerText;
                    excel.ActiveSheet.Cells(i + 1, j + 1).Value = str; // Writes to the sheet
                }
            }
            return;
        }
 </script>
</head>
<body>
    <form id="form1"  runat="server" style="text-align:center;">
        <div >
            <h1><asp:Label ID="Label1" runat="server" Text="Label">EXPORTING GRIDVIEW</asp:Label></h1>
        </div>
        <div id="printme" align="center">
          <asp:GridView ID="GridView1" runat="server">
          </asp:GridView>
        </div>
        <br />        
        <input type="submit" onclick="writeToExcel2();" value="EXPORT TO EXCEL USING ACTIVEX" />         
    </form>
</body>
</html>
Posted
Updated 19-Aug-16 2:45am
v3

1 solution

You can't just create any activex object from javascript, the object has to be configured as suitable for scripting otherwise you can't create it. Excel is not suitable for scripting so can't be automated from javascript.
 
Share this answer
 
Comments
pranav8265 19-Aug-16 8:49am    
can you help me out on how to achieve such a functionality using j query/java script or any other method from UI side.
F-ES Sitecore 19-Aug-16 8:53am    
You'd be better generating the excel file on the server and downloading it to the client. You can create Excel files using EPPlug, Excel ODBC Driver, Microsoft XML SDK, Aspose and others.
Richard Deeming 19-Aug-16 10:32am    
"EPPlug"? That's a new one. :)

I assume you meant EPPlus[^].
F-ES Sitecore 19-Aug-16 10:57am    
Pfff, EPPlus is the MooTools of Excel creation, get with the times :P

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