如何使用c#动态创建的excel文件名添加今天日期 [英] How to add today date with an excel file name created dynamically using c#

查看:151
本文介绍了如何使用c#动态创建的excel文件名添加今天日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我想用'clientstatement.xls'添加今天的日期。我希望将.xls保存为'clientstatement08022014.xls'。日期格式可以不同。在我的代码中,我已将日期分配给变量v_todaydt。







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.



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;

推荐答案

试试这个



Try this

string filename = "clientstatement" + DateTime.Now.ToString("ddMMyyyy") + ".xls";
ExcelApp3.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\" + filename);
ExcelApp3.ActiveWorkbook.Saved = true;


ExcelApp3.ActiveWorkbook.SaveCopyAs("C:\\Users\\RAJENDRAN\\Desktop\\clientstatement"+DateTime.Now.ToShortDateString()+".xls");
          ExcelApp3.ActiveWorkbook.Saved = true;







使用以下内容:






use the below istead of that :

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();


这篇关于如何使用c#动态创建的excel文件名添加今天日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆