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

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

4

1 回答 1

1

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

这里有几篇文章专注于使用 GridView 执行此操作,但也可以使用 DataGrid。主要要看的是他们在渲染网格之前调用的函数,该函数通过网格中的控件并将任何 LinkBut​​tons 等转换为文字。

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

第 2 条:处理不同类型控制的更稳健、更灵活的方法

于 2009-09-08T08:47:54.570 回答