views:

686

answers:

3

I would like to export a GridView to excel, which is easy enough. But above the grid, in Excel, I would like some other information for identification. Can I somehow export things other than gridviews while then putting in the gridview below?

Edit: For some reason when the GridView1 is visible and I try to export, the entire page exports and not just the gridview. Not sure why!

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
    'Create a StringWriter and HtmlTextWriter
    Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
    Dim htw As New System.Web.UI.HtmlTextWriter(sw)

    'Clear the Response object's content and specify the header for the HTML response and type of application file to create
    Response.ClearContent()
    Response.AddHeader("content-disposition", "attachment; filename=SaveFile.xls")
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    EnableViewState = False

    htw.WriteLine("Test, test, test")

    Try
        'Check for the number of GridView rows
        If GridView1.Rows.Count < 65535 Then
            'Turn sorting and paging off and rebind the GridView control
            GridView1.AllowSorting = False
            GridView1.AllowPaging = False
            GridView1.PageSize = GridView1.Rows.Count
            GridView1.AutoGenerateSelectButton() = False
            GridView1.DataBind()


            'Render the GridView1 as HTML - this will cause an error that will fire the VerifyRenderingInServerForm event -- this event is trapped by the Overriding sub procedure given at the end of the program listing
            GridView1.RenderControl(htw)

            'Write the response
            Response.Write(sw.ToString())
            Response.End()

            'Turn sorting and paging on and rebind the GridView control
            GridView1.AllowSorting = True
            GridView1.AllowPaging = True
            '.GridView1.PageSize = 10
            GridView1.AutoGenerateSelectButton() = True
            GridView1.DataBind()
        End If
    Catch ex As Exception

    End Try

End Sub
+2  A: 

Yes you can.

Do something like this:

HttpContext.Current.Response.Write("some string value")

before you pass your gridview.

Eppz
System.Web.HttpResponse is what my current code uses so I'll try and adapt
Joe Philllips
It's what mine uses as well. For your string value, try and write it as html. <p><b><center>Report Title</center></b>...
Eppz
+1  A: 

If you want to export your content to ExcelML check out the RadGrid from Telerik

You can also insert header info into the grid etc

flalar
+1  A: 

Here is my code for doing the same

protected void ExportExcel_OnClick(object sender, EventArgs e) {
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=brugere.xls");
    Response.Charset = "windows-1252";
    Response.ContentType = "application/vnd.xls";
    using (StringWriter stringWrite = new StringWriter())
    using (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite)) {
     GridView1.AllowPaging = false;
     GridView1.DataBind();
     GridView1.RenderControl(htmlWrite);
     string html = stringWrite.ToString();
     string result = Replacer.Replace(html, "");
     Response.Write(result);
    }
    Response.End();
}

Notice that im trimming the resulting html using a regular expression to avoid formatting, images, divs and whatnots.

static readonly Regex Replacer = new Regex("(<input[^<>]*>)|"+
  "(class=\"[^\"]*\")|(style=\"[^\"]*\")|"+
  "(<a[^]*>)|(</a>)|(<div>)|(</div>)|" +
  "(cellspacing=\"[^\"]*\")|(cellpadding=\"[^\"]*\")|" +
  "(id=\"[^\"]*\")|(border=\"[^\"]*\")", RegexOptions.IgnoreCase);

Remember to override the following to ensure that grid will render outside a Page

public override void VerifyRenderingInServerForm(Control control) {
    return; 
}
Christian Ernst Rysgaard