使用activexobject将gridview导出为ex​​cel [英] Exporting gridview to excel using activexobject

查看:46
本文介绍了使用activexobject将gridview导出为ex​​cel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个java脚本函数,它将gridview中存在的数据导出到excel文件中。我使用activeXObjext这样做但是我收到以下错误(自动化服务器无法创建对象)。我是使用Internet Explorer 11并且我已经更改了Internet选项以允许activex但是我得到了同样的错误。



我之前使用htmltextwriter(asp.net)做过这个但在这里我必须使用ActiveXObject。我没有得到任何解决方案。请帮助。



提前感谢。



我尝试了什么:



我从数据库中获取数据到gridview然后尝试导出它是excel sheet。这就是我的尝试。



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.

<!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>

推荐答案

您不能只从javascript创建任何activex对象,该对象必须配置为s非常适合脚本,否则你无法创建它。 Excel不适合脚本编写,因此无法通过javascript自动执行。
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.


这篇关于使用activexobject将gridview导出为ex​​cel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆