Exporting data to Excel in asp.net 4.0

Remmensquinny

New Member
I wanted to see the issues I'm getting is by the nature of things or if it is my erroneous code. I have gridview that exports fine using this code:\[code\]public void btnExport_Click(object sender, System.EventArgs e){ string attachment = string.Empty; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); HtmlForm frm = new HtmlForm(); frm.Attributes["runat"] = "server"; attachment = "attachment; filename=MyExcelFile.xls"; gvSystemTotals.Parent.Controls.Add(frm); frm.Controls.Add(gvSystemTotals); Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; frm.RenderControl(htw); Response.Write(sw.ToString()); Response.End();}\[/code\]In a different page I have asp.net label that contains data built in string builder which is basically a html table and some data like this (showing a much simplified and reduced code):\[code\] sbCountByYear.Append("<table><tr><td align='right'>Total Entries</td><td align='right'>"); sbCountByYear.Append(_totalEntries.ToString("#,##0")); sbCountByYear.Append("</td></tr><tr><td align='right'>On Equipment Entries</td><td align='right'>"); sbCountByYear.Append(_totalEquimentEntries.ToString("#,##0")); sbCountByYear.Append("<td></tr><tr><td align='right'>On Equipment Total Repair Time</td><td align='right'>"); sbCountByYear.Append(_totalRepairTime.ToString("#,##0")); sbCountByYear.Append("</td></tr></table>");\[/code\]When I run the app in debug mode with VS2010 on Win 7, IE 9, it tells me that the .xls file could not be downloaded. When I try again, I get a popup with this error "The file you are trying to open, is in a different file format then specified by the file extension." If I click Yes, another popup box lists two missing files. Both files are the sites .css files but listed in two different path, one in the \temporary internet files\Content.IE5\UA2LSN21\file.css folder and the other in \temporary internet files\Content.IE5\file.css. And so what I get is unformatted webpage with my text and controls.I should note that where is indicated \[quote\] gvSystemTotals\[/quote\]in the code above, I had replace that with the name of the label.Any ideas as to what's going on here?Thanks,Risho
 
Back
Top