将网格导出到 Excel 时出现问题

发布于 2024-08-04 13:16:17 字数 1090 浏览 8 评论 0原文

我正在尝试将数据网格导出到 Excel 文件。它可以正确转换 asp:BoundColumn,但无法转换带有链接按钮或图像按钮的模板列。这些单元格将转换为空单元格。

这是将控件导出到excel的代码。请问

Public Shared Sub Export(ByVal Mime As String, ByVal Filename As String, ByVal Response As HttpResponse, ByVal Control As Control)

    Dim stringWrite As New System.IO.StringWriter()
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    Response.Clear()
    Response.Buffer = True
    Response.Charset = String.Empty
    Response.ContentType = Mime
    Response.AddHeader("Content-Disposition", "filename=" & Filename)
    Control.RenderControl(htmlWrite)
    Response.Write("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><head id=""Head1"" runat=""server""><meta http-equiv=""content-type"" content=""text-html; charset=utf-8""></head><body>" & stringWrite.ToString & "</body></html>")
    Response.End()
End Sub

问题可能出在哪里?有什么想法吗? 提前致谢。

I am trying to export a datagrid to a an excel file. It converts the asp:BoundColumn correctly but the template columns with a linkbutton or an imagebutton cannot be converted. Those cells are converted as an empty cell.

Here is the code that exports the controls to excel.,

Public Shared Sub Export(ByVal Mime As String, ByVal Filename As String, ByVal Response As HttpResponse, ByVal Control As Control)

    Dim stringWrite As New System.IO.StringWriter()
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    Response.Clear()
    Response.Buffer = True
    Response.Charset = String.Empty
    Response.ContentType = Mime
    Response.AddHeader("Content-Disposition", "filename=" & Filename)
    Control.RenderControl(htmlWrite)
    Response.Write("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><head id=""Head1"" runat=""server""><meta http-equiv=""content-type"" content=""text-html; charset=utf-8""></head><body>" & stringWrite.ToString & "</body></html>")
    Response.End()
End Sub

Where the problem could be? Any ideas?
Thanks in advance.

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

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

发布评论

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

评论(1

憧憬巴黎街头的黎明 2024-08-11 13:16:17

基本上,问题在于在服务器端表单之外呈现非文字控件。

这里有几篇文章重点介绍如何使用 GridView 执行此操作,但也应该可以与 DataGrid 配合使用。主要要注意的是它们在渲染网格之前调用的函数,该函数遍历网格中的控件并将任何 LinkBut​​ton 等转换为文字。

第1条:一个更简单、更“硬-编码”方式,专门针对 LinkBut​​tons 和 Dropdownlists (查看底部的 DisableControls 功能)

第 2 条:一种更强大、更灵活的方法来处理不同类型的控件

Basically the problem is with rendering non-literal controls outside of a server-side form.

Here are a couple of articles that are focused on doing this with a GridView, but should work okay with a DataGrid as well. The main thing to look at is the function they call before rendering the grid that goes through the controls in the grid and converts any LinkButtons, etc. into literals.

Article 1: A simpler, more "hard-coded" way, specifically for LinkButtons and Dropdownlists (look towards the bottom for the DisableControls function)

Article 2: A more robust and flexible method to deal with different types of controls

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