将网格导出到 Excel 时出现问题
我正在尝试将数据网格导出到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,问题在于在服务器端表单之外呈现非文字控件。
这里有几篇文章重点介绍如何使用 GridView 执行此操作,但也应该可以与 DataGrid 配合使用。主要要注意的是它们在渲染网格之前调用的函数,该函数遍历网格中的控件并将任何 LinkButton 等转换为文字。
第1条:一个更简单、更“硬-编码”方式,专门针对 LinkButtons 和 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