The easiest way to export to MS Excel
March 18th, 2008
If you want a quick and dirty way to export some data to excel, you can use this nifty trick: generate an HTML file with a table containing the data, and change the file's extension to xls. And that's it! You'll open the file in excel.
You can also have a page rendering in excel using this trick. For exemple, if you're using ASP.NET, the following Page will start a download for a Report.xls file:
C#:
-
public class ToExcel : Page {
-
-
protected override void OnLoad(EventArgs e)
-
{
-
base.OnLoad(e);
-
Response.AddHeader("Content-disposition",
-
"attachment; filename=Report.xls");
-
Response.AddHeader("Content-type",
-
"excel/ms-excel; name=Report.xls");
-
}
-
-
protected void Render(HtmlTextWriter writer)
-
{
-
writer.Write("<table>");
-
writer.Write("<tr>");
-
writer.Write("<td>Some</td>");
-
writer.Write("<td>Data</td>");
-
writer.Write("</tr>");
-
writer.Write("</table>");
-
}
-
-
};
Related Posts
- Ballon Bliss Game
- Application Evolution using Google Charts
- Best way to load XML configuration
- Maximum lines per method
- Dynamic generics with reflection




March 19th, 2008 at 10:19 am
In one word…BEAUTIFUL
April 3rd, 2008 at 8:02 am
[...] the The easiest way to export to MS Excel, I came across with another problem: if we make a request to download a big file, the server will [...]
January 27th, 2009 at 6:58 pm
I don’t think this little trick works for OSX/Mac versions of MS Excel, unfortunately.