将动态创建的表从 javascript 导出到 Excel

发布于 2024-12-27 19:24:39 字数 2252 浏览 2 评论 0原文

我正在开发一个使用 javascript 动态创建 html 表的项目。创建此表后,我需要能够通过单击按钮将其导出到 Excel。我已经尝试了一些方法,但它们对我不起作用。

  1. 我尝试通过创建 Active X 对象从 javascript 进行简单导出,但该设置在 IE 中被锁定,因此我们的浏览器无法与 Active X 一起使用。

  2. 我尝试使用 HttpContext 类从代码隐藏函数导出到 Excel,但由于表是动态创建的,服务器没有看到

  3. 我的最后一个方法(我确信这会起作用)是通过 pagemethod 使用 AJAX 将表导出到 Excel。我打算在 javascript 中创建一个表数组并将其传递给 pagemethod。但在进行这一步之前,我创建了一个 pagemethod,将简单的“测试”文件导出到 Excel。它作为一种通过单击按钮调用的方法起作用,所以我认为它也可以通过页面方法调用。它没有:(它运行并完成了 pagemethod,但不打开 excel 或导出任何内容。没有错误,并且执行了成功函数。

这是代码:

<System.Web.Service.WebMethod()>
Public Shared Function exportTable(ByVal title As String) As String
HttpContext.Current.Response.ClearContext()
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename="TEST.xls")
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.Write("<?xml version='1.0'?>")
HttpContext.Current.Response.Write("<ss:Workbook xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'>")
HttpContext.Current.Response.Write("<ss:Worksheet ss:Name='sheet1'>")
HttpContext.Current.Response.Write("<ss:Table>")
HttpContext.Current.Response.Write("<ss:Row>")
HttpContext.Current.Response.Write("<ss:Cell><ss:Data ss:Type='String'>TEST</ss:Data></ss:Cell>")
HttpContext.Current.Response.Write("</ss:Row>")
HttpContext.Current.Response.Write("</ss:Table>")
HttpContext.Current.Response.Write("</ss:Worksheet>")
HttpContext.Current.Response.Write("</ss:Workbook>")
HttpContext.Current.ApplicationInstance.CompleteRequest()

Return 0
End Function

我的页面方法调用是:

function exportToExcel(title) {
PageMethods.exportTable(title, exportSuccess, exportFailure)
}

exportSuccess 函数是一个简单的警报,我认为这意味着 pageMethod 正在执行而没有错误。打不开excel 或......

我最终想传递一个数组并循环它以添加行和数据。我已经用非动态表完成了此操作并且它有效。我还将使用标题来定义文件名,但出于测试原因,我将其命名为 TEST.xls。

我将 HttpContext 代码复制并粘贴到按钮单击事件中,它在静态表中工作得很好,所以我认为这没有任何问题。但众所周知我错了。

我想我的问题是 PageMethod 可以导出到 Excel 或使用 HttpContext 类吗?有没有更好/更简单的方法来做到这一点?

请记住,我无法使用 ActiveX 对象,并且所有客户都必须使用 IE。

感谢您的帮助,如果这已经被涵盖,我很抱歉。我搜索了但没有找到任何东西。如果有的话,能帮我指一下那个帖子吗?

I am working on a project that dynamically creates an html table with javascript. Once this table is created I need to be able to export it to excel through a button click. I have tried a few things already, but they haven't worked for me.

  1. I tried doing a simple export from javascript by creating the Active X object, but that setting is locked down in IE so our browsers will not work with Active X.

  2. I tried exporting to excel from a code behind function using the HttpContext class but since the table is created dynamically, the server doesn't see it.

  3. My final method, and I was sure that this was going to work, was I used AJAX via a pagemethod to export the table to excel. I was going to create an array of the table in javascript and pass it to the pagemethod. But before I got to this step, I created a pagemethod that exported a simpe "Test" file to excel. It worked as a method called from a button click, so I figured it would from a pagemethod as well. It didn't :( It runs and completes the pagemethod, but doesn't open excel or export anything. No errors and the success function is executed.

Here is the code:

<System.Web.Service.WebMethod()>
Public Shared Function exportTable(ByVal title As String) As String
HttpContext.Current.Response.ClearContext()
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename="TEST.xls")
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.Write("<?xml version='1.0'?>")
HttpContext.Current.Response.Write("<ss:Workbook xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'>")
HttpContext.Current.Response.Write("<ss:Worksheet ss:Name='sheet1'>")
HttpContext.Current.Response.Write("<ss:Table>")
HttpContext.Current.Response.Write("<ss:Row>")
HttpContext.Current.Response.Write("<ss:Cell><ss:Data ss:Type='String'>TEST</ss:Data></ss:Cell>")
HttpContext.Current.Response.Write("</ss:Row>")
HttpContext.Current.Response.Write("</ss:Table>")
HttpContext.Current.Response.Write("</ss:Worksheet>")
HttpContext.Current.Response.Write("</ss:Workbook>")
HttpContext.Current.ApplicationInstance.CompleteRequest()

Return 0
End Function

My page method call is:

function exportToExcel(title) {
PageMethods.exportTable(title, exportSuccess, exportFailure)
}

The exportSuccess function is a simple alert, which is firing. I take that to mean that the pageMethod is executing without errors, but that it can't open excel or ... something.

I eventually want to pass in an array and loop through it to add the rows and data. I've done this with a non dynamic table and it works. I'm also going to use the title to define the filename, but for testing reasons, I've named it TEST.xls.

I copied and pasted the HttpContext code into a button click event and it worked fine with a static table, so I don't think there are any problems with that. But I have been known to be wrong.

I guess my question is can a PageMethod export to excel or use the HttpContext class? Is there a better/easier way to do this?

Please keep in mind that I cannot use ActiveX objects and all customers are required to use IE.

Thanks for your help, and I'm sorry if this has already been covered. I searched but didn't find anything. If it has, could you please point me to the post?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2025-01-03 19:24:39

如果您发送 Excel MIME 标头,后跟 HTML 表,EXCEL 应该像电子表格一样加载它。

If you send out the Excel MIME header, followed by an HTML table, EXCEL should load it as it it were a spreadsheet.

三五鸿雁 2025-01-03 19:24:39

好吧,我在压力下屈服了,决定以不同的方式来做。我认为ajax 与response.write 配合得不好。

我创建了一个 asp:hiddenfield 控件。然后,当调用 ajax 来生成表时,我用表示表的行和列的管道分隔字符串填充它的值。所以cell1|cell2||cell1|cell2||cell1|cell2。

然后我为按钮创建了一个 on_click 方法,并且能够使用上面函数中的相同代码。为了填充表,我只需通过解析隐藏字段值来替换“TEST”。

我决定发布此内容,以防其他人遇到问题。

Well, I've buckled under pressure and decided to do it a different way. I think that ajax does not play well with response.write.

I created an asp:hiddenfield control. Then when the ajax was called to generate the table, I populated it's value with a pipe delimeted string representing the rows and columns of the table. So cell1|cell2||cell1|cell2||cell1|cell2.

Then I created an on_click method for a button and was able to use the same code from the function above. To populate the table I just replace the "TEST" by parsing the hiddenfield value.

I decided to post this just in case anyone else was having an issue with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文