Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向响应流上发送的文件添加时间戳_C#_Asp.net - Fatal编程技术网

C# 向响应流上发送的文件添加时间戳

C# 向响应流上发送的文件添加时间戳,c#,asp.net,C#,Asp.net,我有一段代码,我试图在excel文件的名称中添加一个时间戳 resp.ContentType = "application/msexcel"; resp.Cache.SetCacheability(HttpCacheability.NoCache); resp.AddHeader("content-disposition", "attachment;filename=AllUsersList.xlsx"); 我希望文件名类似于AllUserList_2016.xlsx您可以使用DateTime

我有一段代码,我试图在excel文件的名称中添加一个时间戳

resp.ContentType = "application/msexcel";
resp.Cache.SetCacheability(HttpCacheability.NoCache);
resp.AddHeader("content-disposition", "attachment;filename=AllUsersList.xlsx");

我希望文件名类似于
AllUserList_2016.xlsx

您可以使用
DateTime.Now
和所需格式计算文件名

例如,仅本年度:

string filename = $"AllUsersList_{DateTime.Now:yyyy}.xlsx";
resp.AddHeader("content-disposition", $"attachment;filename={filename}");
或以今天的日期:

string filename = $"AllUsersList_{DateTime.Now:yyyyMMdd}.xlsx";
resp.AddHeader("content-disposition", $"attachment;filename={filename}");
或者在6岁之前:


您可以使用
DateTime.Now
和所需格式计算文件名

例如,仅本年度:

string filename = $"AllUsersList_{DateTime.Now:yyyy}.xlsx";
resp.AddHeader("content-disposition", $"attachment;filename={filename}");
或以今天的日期:

string filename = $"AllUsersList_{DateTime.Now:yyyyMMdd}.xlsx";
resp.AddHeader("content-disposition", $"attachment;filename={filename}");
或者在6岁之前:


您试图在哪里精确设置时间戳?在文件名的末尾。它应该看起来像AllUsersList_2016……您试图在哪里精确设置时间戳?在文件名的末尾。它应该看起来像AllUsersList_2016……美元在那里意味着什么?这给了我一个错误。哦,这是一种调用
string.Format
(从C#6开始)的新方法。我将更改答案以使用以前的版本。美元在那里意味着什么?这给了我一个错误。哦,这是一种调用
string.Format
(从C#6开始)的新方法。我将更改答案,以使用以前的版本。