2009-09-08 75 views
0

我想将数据网格导出到excel文件。它正确地转换asp:BoundColumn,但无法转换具有linkbutton或imagebutton的模板列。这些单元格被转换为空单元格。将网格导出为ex​​cel时出现问题

这里是出口的控件到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 

问题出在哪里可以?有任何想法吗? 在此先感谢。

+0

你需要的ImageButton在Excel中呢? – 2009-09-08 06:58:31

+0

其实我的问题不是imagebutton的可见性。不显示它是可以的,但我想知道原因。但对于linkbutton是我需要它,至少是它的文本。 – 2009-09-08 07:03:35

回答

1

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

这里有几篇文章专注于使用GridView做这件事,但也应该与DataGrid一起工作。主要看的是它们在渲染网格之前调用的函数,该网格通过网格中的控件并将任何LinkBut​​ton等转换为文字。

第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