Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I�m using this code to export data from a datagrid to an Excel sheet:

 

Response.Clear();

Response.Buffer= true;

Response.ContentType = "application/vnd.ms-excel";

Response.AddHeader("Content-Disposition", "inline;filename=Clientes.xls");

Response.Charset = "";

this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

DataGrid1.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();

 

And this is working fine.

But now I want to do same with a datalist and thus I changed RenderControl line thus:

 

DataList1.RenderControl(oHtmlTextWriter);

 

Yet when Excel sheet is showing on screen, all data are displayed in one simple column �A�, with one row for each data in datalist.

 

Does somebody know how can I attain it work as I did as datagrd (each column in datalist into its respective column in Excel)?

 

I�ll appreciate your suggestions.

 

A.L.

  • 8 months later...
Posted

Hi A.L.

 

I am using the same code as yourself for taking the data stored in my datagrid an putting it in an excel file. I am however encountering a problem.

 

"I believe the problem is related to Microsoft Windows Service Pack 2." I have surrounded the above code with a try catch statement, and for testing purposes I threw in a "Throw Ex" statement after the catch block. From what I can figure out, the code seems to throw an exception once it reachs the final line. Any suggestions on how to prevent this error from occuring.

 

In addition if I transfer my code to a web server and run it, if I hit the save button, the excel file will be save to my web server?

 

Mike55

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...