我有这段代码,我试图在excel文件的名称中添加时间戳
resp.ContentType = "application/msexcel";
resp.Cache.SetCacheability(HttpCacheability.NoCache);
resp.AddHeader("content-disposition", "attachment;filename=AllUsersList.xlsx");
我希望文件名变成AllUserList_2016.xlsx
。
答案 0 :(得分:2)
您可以使用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}");
或者在C#6之前的年龄:
string filename = string.Format("AllUsersList_{0:yyyy}.xlsx", DateTime.Now);
resp.AddHeader("content-disposition", string.Format("attachment;filename={0}", filename));