Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to export grid to excel and sometimes its result(usually for one or two rows) like below


excel file:
ردی٠واحد نوع اشتراک شماره اشتراک وضعيت
1 معاونت درمان گاز 456789 فعال

and sometimes it shows correct
grid data:
ردیف واحد نوع اشتراک شماره اشتراک وضعيت
معاونت درمان گاز 456789 فعال


Code:

C#
response.Clear();
       response.Buffer = true;
       response.AddHeader("content-disposition", "attachment;filename=ShowData.xls");
       response.ContentType = "application/ms-excel";
       //response.ContentEncoding = System.Text.Encoding.Default;
       response.Charset = "";
       StringWriter sw = new StringWriter();
       HtmlTextWriter hw = new HtmlTextWriter(sw);

       grd.AllowPaging = false;
       if (data != null)
       {
           grd.DataSource = data;
           grd.DataBind();

       }
       foreach (int Index in GridIndexLst)
       {
           grd.Columns[Index].Visible = false;

       }
       grd.RenderControl(hw);
       //string style = @"<style> .textmode { mso-number-format:\@; } </style>";
       //response.Write(style);
       response.Output.Write(sw.ToString());
       response.Flush();
       response.End();
Posted
Updated 1-Oct-12 21:50pm
v3
Comments
Sandeep Mewara 2-Oct-12 2:38am    
Can you share what is the text equivalent of the above coded text. It can suggest why it might be happening.
faezeh66 2-Oct-12 3:26am    
ردیف واحد نوع اشتراک شماره اشتراک وضعيت
معاونت درمان گاز 456789 فعال1
Sandeep Mewara 2-Oct-12 3:43am    
So it's a unicoded text! How can you fail to mention this in your question.

Add the follwing line:
C#
response.ContentType = "text/html";
response.Charset = "utf-8";   

Similar issue discussed here: ASP.Net - Export to Excel - Unicode characters are not being displayed correctly[^]
 
Share this answer
 
Comments
faezeh66 2-Oct-12 4:03am    
thanks,but not useful
See this article 9 Solutions to Export Data to Excel for ASP.NET[^]I think it may help you.
 
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