重命名C#Windows Service中生成的Excel [英] Renaming the generated excels in C# Windows Service
问题描述
我做了一个Windows服务,可以将数据库中的数据导出到excel中,然后作为附件发送到邮件中.
我已经设置了Windows服务需要在星期一和星期四发送它的计时器.
我现在在文件名中遇到一个问题.因为在第一次调用中文件被保存为"C://abc.xlsx",但是在第二次调用中它说它无法访问文件"C://" abc.xlsx''.
我希望在第二次运行时将excel保存为不同的名称,例如"C://abc_date.xlsx",其中date是生成新excel的当前日期.
可以帮忙吗?
谢谢
Hi,
I have made a windows service that exports the data from database into an excel and then send it as an attachment in a mail.
I have set the timer that windows service needs to send it on Mon and Thursday.
I am facing an issue in the filename now..as in the file gets saved as ''C://abc.xlsx'' in first call but in second call it says that it cannot acess the file ''C://abc.xlsx''.
I want that in second run the excel should be saved with a different name say ''C://abc_date.xlsx'' where date is the current date on which the fresh excel is generated.
Can some help pls.
Thanks
推荐答案
您可以使用以下代码保存之前重命名文件名:
Hi,
you could just rename the filename before saving with this code:
string fileName = "C:\\abc.xlsx";
string renamedFile = Path.GetFileNameWithoutExtension(fileName);
renamedFile = renamedFile += "_" + DateTime.Now.ToShortDateString() + Path.GetExtension(fileName);
MessageBox.Show(renamedFile);
这篇关于重命名C#Windows Service中生成的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!