exporting to excel
-
hi all, in my application im having code to convert the datagrid values to excel sheet but when it is saving it is taking .html extension instead if .xls, pls help me to sort out. Response.Clear() Response.Buffer = True Response.AddHeader("content-position", "attachment;filename=FileName.xls") Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.ms-Excel" Me.EnableViewState = False Dim stringwrite As New System.IO.StringWriter Dim htmlwrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringwrite) Dim da_GetAllBooks As SqlDataAdapter = New SqlDataAdapter("GetAllBooks_Proc", Con) da_GetAllBooks.SelectCommand.CommandType = CommandType.StoredProcedure da_GetAllBooks.Fill(_dataSet, "AllBooks(0)") Dim index As Integer = 0 For index = 0 To dg_AllBooks.Columns.Count - 1 dg_AllBooks.Columns(index).SortExpression = True Next dg_AllBooks.DataSource = (_dataSet.Tables("AllBooks")) dg_AllBooks.DataBind() dg_AllBooks.RenderControl(htmlwrite) Response.Write(stringwrite.ToString()) Response.End() help me
-
hi all, in my application im having code to convert the datagrid values to excel sheet but when it is saving it is taking .html extension instead if .xls, pls help me to sort out. Response.Clear() Response.Buffer = True Response.AddHeader("content-position", "attachment;filename=FileName.xls") Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.ms-Excel" Me.EnableViewState = False Dim stringwrite As New System.IO.StringWriter Dim htmlwrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringwrite) Dim da_GetAllBooks As SqlDataAdapter = New SqlDataAdapter("GetAllBooks_Proc", Con) da_GetAllBooks.SelectCommand.CommandType = CommandType.StoredProcedure da_GetAllBooks.Fill(_dataSet, "AllBooks(0)") Dim index As Integer = 0 For index = 0 To dg_AllBooks.Columns.Count - 1 dg_AllBooks.Columns(index).SortExpression = True Next dg_AllBooks.DataSource = (_dataSet.Tables("AllBooks")) dg_AllBooks.DataBind() dg_AllBooks.RenderControl(htmlwrite) Response.Write(stringwrite.ToString()) Response.End() help me
Your code is confusing. You seem to be trying to i) output an Excel file and ii) display a DataGrid at the same time. You can't do both - your page can't return binary content and HTML at the same time. To dynamically build an Excel spreadsheet, do a search on CodeProject or Google for the JET OLE Provider. You should find plenty of examples.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush