|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
problem exporting datagrid to excelpage: private void exportButton_Click(object sender, System.EventArgs e) { Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html); this.ClearControls(reportGrid); reportGrid.RenderControl(hw); hw.RenderEndTag(); Response.Write(tw); Response.End(); } When I run this code I only get the first column returned to the spreadsheet. The is no other control on the datagrid so I am at a loss. Any suggestions would be appreciated. Thanks, Dave I'm not too sure how to solve your problem.
I store the dataset, filters, sorts and pageindex of the datagrid in httpcache and session vars and then create a new datagrid using those stored objects. I then use that for my Excel output. It's in vb (easily translatable to c#, but I'm not gonna do it for you, sorry) Private Sub ExcelOut() Page.Response.Clear() Page.Response.AppendHeader("Content-Disposition", "attachment; filename=") Page.Response.ContentType = "application/vnd.ms-excel" 'Response.ContentType = "text/csv" ' Remove the charset from the Content-Type header. Page.Response.Charset = "" Dim dsAPL As DataSet = MyCustomClass.DataAccess.SQLDataSet(_strDataContext, "dbo." & _strDataContext & "_GET", True), _ ds As DataSet = New DataSet ds = dsAPL.Clone ds.Merge(dsAPL) If Not Page.Session(_strDataContext & "_Filter") Is Nothing Then ds.Tables(0).DefaultView.RowFilter = Page.Session(_strDataContext & "_Filter") End If If Not Page.Session(_strDataContext & "_Sort") Is Nothing Then ds.Tables(0).DefaultView.Sort = Page.Session(_strDataContext & "_Sort") End If Dim oItem As ListItem For Each oItem In lstDisplayFields.Items If Not oItem.Selected Then ds.Tables(0).Columns.Remove(oItem.Value) End If Next Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) Dim dgGrid As New DataGrid dgGrid.DataSource = ds.Tables(0).DefaultView dgGrid.DataBind() ' Get the HTML for the control. dgGrid.RenderControl(hw) ' Write the HTML back to the browser. Page.Response.Write(tw.ToString()) ' End the response. Page.Response.End() ds.Dispose() ds = Nothing End Sub
How can I display hashtable key values in a web page?
Move bound column to right of dynamic column in datagrid? Add text from listbox into datagrid column Print WebForm DataGrid To Eliyahu Dynamically Add Validation to Datagrid Add row to datagrid? ASP .Net Message box help Needed Very urgent User Control SortCommand and PageIndexChange event not firing |
|||||||||||||||||||||||