Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code I would like to add today's date with 'clientstatement.xls'. I want this .xls to be saved as 'clientstatement08022014.xls'. The date format can differ. In my code I have assigned date to a variable v_todaydt.



C#
v_todaydate = (DateTime.Today);
           string v_todaydt = v_todaydate.Date.ToString("dd/MM/yyyy");
           ExcelApp3.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\clientstatement.xls");
           ExcelApp3.ActiveWorkbook.Saved = true;
Posted

Try this

C#
string filename = "clientstatement" + DateTime.Now.ToString("ddMMyyyy") + ".xls";
ExcelApp3.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\" + filename);
ExcelApp3.ActiveWorkbook.Saved = true;
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 8-Feb-14 2:31am    
Thanks.It worked.
Karthik_Mahalingam 8-Feb-14 3:52am    
Welcome Rajendran Thambi:)
C#
ExcelApp3.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\clientstatement"+DateTime.Now.ToShortDateString()+".xls");
          ExcelApp3.ActiveWorkbook.Saved = true;



use the below istead of that :

C#
Response.ClearContent();
       Response.Buffer = true;
       Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "clientstatement"+DateTime.Now.ToShortDateString()+".xls"));
       Response.ContentType = "application/ms-excel";
       StringWriter sw = new StringWriter();
       HtmlTextWriter htw = new HtmlTextWriter(sw);

       GridView1.AllowPaging = false;
       btnview_Click(this, null);
       GridView1.DataBind();
       GridView1.RenderControl(htw);
       Response.Write(sw.ToString());
       Response.End();
 
Share this answer
 
v2
Comments
S.Rajendran from Coimbatore 8-Feb-14 0:29am    
I altered accordingly but it throws an exception for this new file:
Microsoft Office Excel cannot access the file 'C:\Users\RAJENDRAN\Desktop\clientstatement2/8/2014.xls'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
King Fisher 8-Feb-14 0:42am    
use this istead of that code :
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "clientstatement"+DateTime.Now.ToShortDateString()+".xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;
btnview_Click(this, null);
GridView1.DataBind();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
Hello World !

I'm new here and I'm so much interested on anything as per skills learning and training with to gained more experiences and work with experts to becomes expert in due times frame.

Thanks for your co-operation
 
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