Public Function DataGridToExcel(ByVal dgExport As C1.Web.Wijmo.Controls.C1GridView.C1GridView) As String '创建一个stringwriter Dim stringWrite As New System.IO.StringWriter() '创建一个使用该stringwriter的htmltextwriter Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite) Dim dg As C1.Web.Wijmo.Controls.C1GridView.C1GridView 'just set the input datagrid = to the new dg grid dg = dgExport '将header的字体加粗 dg.HeaderStyle.Font.Bold = True '如果需要,这里是在组件级别改变颜色/格式 dg.HeaderStyle.ForeColor = System.Drawing.Color.Black dg.RowStyle.ForeColor = System.Drawing.Color.Black '绑定修改后的datagrid '告诉datagrid将自己呈现到我们提供的htmltextwriter dg.AllowSorting = False dg.AllowPaging = False dg.AllowCustomPaging = False '新的代码 Dim parent As Control = dg.Parent parent.Controls.Remove(dg) dg.RenderControl(htmlWrite) '新的代码 parent.Controls.Add(dg) '输出HTML Return stringWrite.ToString() End Function
Public Sub DownloadToExcel(ByVal content As String, ByVal response As HttpResponse) '清理 response.object response.Clear() response.Buffer = True response.Charset = "" '设置响应的MIME类型为excel response.ContentType = "application/vnd.ms-excel" response.ContentEncoding = New System.Text.UTF8Encoding() response.Write(content) response.End() End Sub
http://connect.microsoft.com/VisualStudio/feedback/details/118285/rendercontrol-doesnt-work-for-gridview
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub
此外,如果gridview包含一个复选框或者一个模板列,你会收到上面的错误。目前已发现微软发布的GridView会发生同样的错误。由于C1GridView继承自微软发布的GridView,所以它是C1GridView的已知设计问题。
下载示例 Wijmo下载,请进入Studio for ASP.NET Wijmo 2012 v1正式发布(2012.03.22更新)!