Click here to Skip to main content
15,886,611 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am trying to send GridView to mail attachment(as excel). Everything works fine,but the problem is with dates in generated excel sheet.

I have dates in GridView header row like 11-08,12-08,13-08,14-08...

Problem is,the first two are showing as 8-Nov,08-Dec in generated excel sheet,other are showing as expected 13-08,14-08....

The code i used is as follows(Gridview as mail attachment)
C#
StringWriter stw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(stw); 
        GridView1.RenderControl(hw);
        MailMessage mail = new MailMessage();
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress("abc"));
        mail.Subject = "report";
        System.Text.Encoding Enc = System.Text.Encoding.ASCII;
        byte[] mBArray = Enc.GetBytes(stw.ToString());
        System.IO.MemoryStream mAtt = new System.IO.MemoryStream(mBArray, false);
        mail.Attachments.Add(new Attachment(mAtt, "def.xls"));
        mail.Body = "Please find the attached document for the date" + DateTime.Now.ToString("dd-MMM-yyyy");
        SmtpClient smtp = new SmtpClient();
        mail.From = new MailAddress(efg, hij);
        smtp.Host = "smtp.gmail.com";
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential(***,***);
        smtp.EnableSsl = true;
        smtp.Send(mail);

Why this dates are being modified this way? Any help please?
Posted
Updated 9-Sep-13 21:05pm
v3
Comments
Sergey Alexandrovich Kryukov 10-Sep-13 3:02am    
No, in your code you are not exporting anything to Excel...
—SA
12345@john 10-Sep-13 3:06am    
Sorry,i modified question. Any help please?
12345@john 10-Sep-13 5:17am    
Do you have any idea? I have try different ways,even with exporting to excel behaves the same way. I don't understand why dates are being changed.? Please help me to over come these.
Sergey Alexandrovich Kryukov 10-Sep-13 10:31am    
Still, not clear. If looks you have some Excel file. Who knows, what's in it? It's not in your code.
And not mention of the grid view in the code...
—SA
12345@john 11-Sep-13 0:24am    
Let me explain once again. I have two textboxes where i am selecting dates. Suppose i selected in first,11th August in second 17th August,so Gridview will display records between this dates. That is,there will be column(Column name) for each date.11-08,12-08,13-08...17-08.There is no any excel sheet,i am generating totally new excel sheet. Lets say i am exporting this to excel. The problem is whenever excel can,it is converting dates. e.g. 13-08,so 13 can't be month,so it keep this as it is,while 11-08,so 11 can be month,it is converting this as 8-Nov(11 taken as month). Why this is so? We have not any excel sheet. And also that dates(11-08...) are strings. I think its clear now,if still not,let me know.

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