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

时间:2009-09-08 06:50:26

标签: asp.net export-to-excel

我正在尝试将数据网格导出到excel文件。它正确转换了asp:BoundColumn,但是无法转换带有linkbutton或imagebutton的模板列。那些细胞被转换为空细胞。

以下是将控件导出为ex​​cel的代码。

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

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

1 个答案:

答案 0 :(得分:1)

基本上问题是在服务器端表单之外渲染非文字控件。

以下是一些专注于使用GridView执行此操作的文章,但也应该使用DataGrid。要看的主要内容是它们在渲染网格之前调用的函数,该网格遍历网格中的控件并将任何LinkBut​​tons等转换为文字。

第1条:A simpler, more "hard-coded" way, specifically for LinkButtons and Dropdownlists (查看 DisableControls 功能的底部)

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

相关问题