Skip to main content Skip to footer

Exporting Wijmo GridView to Excel

Wijmo GridView does not provide a method to export excel file. This blog explains a simple approach to save Wijmo GridView in a excel file. You can use the same approach for C1WebUI GridView as well.

Step 1 : Bind C1GridView to a DataSource

The first step is to bind C1GridView with a datasource. For simplicity, we will bind it to Customers table of C1Nwind.mdb.

Step 2 : Export C1GridView to Excel

Exporting to excel is a two step process. First step is to save the gridview into a html string. Web controls have a RenderControl() method which outputs the server control content to a provided HtmlTextWriter object and stores tracing information about the control if tracing is enabled. This HtmlTextWriter is then output to a StringWriter object. Following method is used to create a string :

Public Function DataGridToExcel(ByVal dgExport As C1.Web.Wijmo.Controls.C1GridView.C1GridView) As String  
    'create a string writers  
    Dim stringWrite As New System.IO.StringWriter()  
    'create an htmltextwriter which uses the stringwriter  
    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  

    'Make the header text bold  
    dg.HeaderStyle.Font.Bold = True  

    'If needed, here's how to change colors/formatting at the component level  
    dg.HeaderStyle.ForeColor = System.Drawing.Color.Black  
    dg.RowStyle.ForeColor = System.Drawing.Color.Black  

    'bind the modified datagrid  
    'tell the datagrid to render itself to our htmltextwriter  
    dg.AllowSorting = False  
    dg.AllowPaging = False  
    dg.AllowCustomPaging = False  

    'new code  
    Dim parent As Control = dg.Parent  
    parent.Controls.Remove(dg)  

    dg.RenderControl(htmlWrite)  

    'new code  
    parent.Controls.Add(dg)  

    'output the html  
    Return stringWrite.ToString()  
 End Function

Next we will call the DownloadToExcel method on a button click to create excel file from the saved string.

 Public Sub DownloadToExcel(ByVal content As String, ByVal response As HttpResponse)  
 'clean up the response.object  
 response.Clear()  
 response.Buffer = True  
 response.Charset = ""  
 'set the response mime type for excel  
 response.ContentType = "application/vnd.ms-excel"  
 response.ContentEncoding = New System.Text.UTF8Encoding()  
 response.Write(content)  
 response.End()  
 End Sub

Download Sample

Implementation Problems

In quite a few cases, you may face few errors while exporting. You may get an error message is "RegisterForEventValidation can only be called during Render();". In such a case please try the following approach : 1. You can overload the VerifyRenderingInServerForm method as described here : http://connect.microsoft.com/VisualStudio/feedback/details/118285/rendercontrol-doesnt-work-for-gridview

Public Overrides Sub VerifyRenderingInServerForm(control As Control)  

End Sub

2. To avoid the "RegisterForEventValidation can only be called during Render()" exception turn off the Page.EnableEventValidation or put the RenderControl call in a try-catch block. Also, if the gridview contains a checkbox or a template column, you will get the above error. This issue is observed with MS GridView as well. Since C1GridView inherit MSGridView, so it is a design behavior in C1GridView.

MESCIUS inc.

comments powered by Disqus