Exporting data grid to excel, also add the filter criteria on the first line on the excel workbook

I have an asp.net application where I would like to export the contents of the datagrid to excel.
I have seen a few examples where the datagrid is exported, but in my case I want a header row on the first line of the excel workseet with the filters that were used

Example
StartDate 10/01/2011 - EndDate 10/31/2011
Grid data follows

Any code sample would be great.

I saw this example
private void LinkButton1_Click(object sender, System.EventArgs e)
            {
                  Response.Clear();
                  Response.AddHeader("content-disposition", "attachment;filename=MyPicks.xls");
                  Response.Charset = "";
                  Response.ContentType = "application/vnd.ms-excel";
                  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                  System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

                  System.Web.UI.WebControls.DataGrid myGrid =  (System.Web.UI.WebControls.DataGrid)PlaceHolder1.FindControl("DataGrid1");
                  myGrid.RenderControl(htmlWrite);
                  Response.Write(stringWrite.ToString());
                  Response.End();
            }
LVL 1
countrymeisterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gopaltaydeCommented:
Not the actual solution, but the work around is -
use crystal reports for .net (which is free with .net). Build a report with your stored procedure, the one which you use to bind the gridview control. Now export the ReportDocument object to any format without any pain.
0
countrymeisterAuthor Commented:
I do not want crystal reports, also crystal reports is somthing my company will not purchase.
0
CodeCruiserCommented:
What version of .NET are you using? The mention of "DataGrid1" has got me worried. If its a GridView, set its Caption property to the filter text and then it would render on top in Excel (hopefully)

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.caption.aspx
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

countrymeisterAuthor Commented:
Hi ! CodeCruiser

I am fine to use a Gridview instead of using a DataGrid, but what I am facing is that Response.Write does not bode well with AJAXed pages.
Also I want to stay away from Excel Interop dll's any other suggestion would be geat
0
CodeCruiserCommented:
You can open a new window which does the response.write so it does not mess up with ajax.
0
countrymeisterAuthor Commented:
CodeCruiser

I did venture into that direction. It gives me an error stating Acces denied.
Please note that I do not have excel installed on the web server, Is this needed to be installed?
Code is attached inline.

I do see the excel file being created, and Everyone is granted Full permissions to the Excel folder.
Protected Sub ExportToExcel()
        Try


            Dim strFilePath As String = String.Empty
            strFilePath = Server.MapPath("~/Excel/") + "ExcelFileName" + ".xls"

            If (File.Exists(strFilePath)) Then
                File.Delete(strFilePath)
            End If

            Dim oStringWriter As System.IO.StringWriter = New StringWriter()
            Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(oStringWriter)
            Dim objStreamWriter As StreamWriter
            Dim strHeader As String = "XYZ Report"
            Dim strHeader1 As String = "StartDate: " & txtStartDate.Text & "  EndDate: " & txtEndDate.Text
            objStreamWriter = File.AppendText(strFilePath)
            dgCriticalResults.RenderControl(oHtmlTextWriter)
            objStreamWriter.WriteLine(strHeader)
            objStreamWriter.WriteLine(strHeader1)
            objStreamWriter.WriteLine(oStringWriter.ToString())
            objStreamWriter.Close()
           
            Dim sb As StringBuilder = New StringBuilder("")
            sb.Append("window.open('" & strFilePath & "','mywindow','width=400,height=400,resizable=1,top=50,left=50,scrollbars=1');")

            ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), Me.ClientID, sb.ToString(), True)
        Catch ex As Exception

            'Handle Exception
            Dim strError As String = ex.Message()
           

        End Try
    End Sub

Open in new window

0
CodeCruiserCommented:
I think it would be more suitable to have a button outside updatepanel that calls this export functionality.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
countrymeisterAuthor Commented:
Code cruiser

I did try that too.

I am using Master page which has AJAX and then my child page uses the master page.
I pulled out the button from the updatepanel and the same error still exits.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.