Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Lets say I have
datatable dt
with all my data inside 'dt'. How can I format the style of it, and export it as an xls file so that I can open it up in excel and it will be formatted correctly?

I have looked at other guides on here such as this one
-- Generating Excel (XML Spreadsheet) in C#[^]--
but they don't really explain much. Any help would be greatly appreciated!

Thanks ~MJ
Posted

Take a look the following link.
ASP.NET: Export a DataTable to Excel[^]

Just change the content type to application/xls
context.Response.ContentType = "application/xls";

I hope this helps you well.
 
Share this answer
 
v2
Comments
MJ_ 13-May-11 9:17am    
Thats CSV and the link to XML is broken.

I need XML XLS because I need to format the excel file.
 
Share this answer
 
Comments
Wonde Tadesse 12-Jun-11 16:34pm    
5+
RaviRanjanKr 13-Jun-11 2:03am    
Thanks :)
MJ_ 15-Jun-11 8:51am    
Pretty good, I'll check it out. 1 month later and I still have the same problem lol.
Wonde Tadesse 6-Dec-11 10:58am    
Thanks
write the following code under button click :


Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=DocumentReport.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvReports.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900