Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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# 将{0}变量添加到Excel电子表格名称_C#_Asp.net Mvc_Excel - Fatal编程技术网

C# 将{0}变量添加到Excel电子表格名称

C# 将{0}变量添加到Excel电子表格名称,c#,asp.net-mvc,excel,C#,Asp.net Mvc,Excel,我正在为我的内联网创建Excel电子表格,显示当前显示在我页面上的数据。它工作顺利,但我希望有不同的文件名(取决于用户登录、创建时间等) 下面是确定Excel文件名的位置: Response.AddHeader("content-disposition", "attachment; filename=Activity.xls"); 如您所见,每次我按下Excel按钮,它都会到达负责检索数据/保存/创建名为Activity的.xls文件的控制器 我是这项技术的新手,听说过使用{0}在C#中为字

我正在为我的内联网创建Excel电子表格,显示当前显示在我页面上的数据。它工作顺利,但我希望有不同的文件名(取决于用户登录、创建时间等)

下面是确定Excel文件名的位置:

 Response.AddHeader("content-disposition", "attachment; filename=Activity.xls");
如您所见,每次我按下Excel按钮,它都会到达负责检索数据/保存/创建名为
Activity
.xls
文件的控制器

我是这项技术的新手,听说过使用
{0}
在C#中为字符串添加参数是否会有类似的东西,以便我可以像这样命名我的文件:

Activity_UserName_DateOfCreation
以下是我的控制器的Excel部分,而不是数据收集:

[Authorize]
public ActionResult Excel(string user)
{
    var products = new System.Data.DataTable("Activity"); // Creates the DataTable
    products.Columns.Add("Project", typeof(string)); // Adds a column
    IEnumerable<Project> lp = _service.List(strUser, strYear, strMonth); // Creates a list of the projects
    UserActivityDb db = new UserActivityDb(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
    lp = lp.Where(p => (true == db.ExistUserActivity(int.Parse(strYear), int.Parse(strMonth), ListDaysOfMonth, p.ProjectId, strUser)));

    List<string[]> lstInts = new List<string[]>();

    foreach (Project p in lp)
    {
        string strInts = db.getFormattedData(int.Parse(strYear), int.Parse(strMonth), ListDaysOfMonth, p.ProjectId, strUser, null, 0);
        string[] ints = strInts.Split(';');

        lstInts.Add(ints);              
        products.Rows.Add(p.Name, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // Adds an empty row to be filed with the correct values later

    }

    int i = 0;
    int y = 1;
    int z = 0;
    foreach (string[] tab in lstInts) // Adds the activity data cell by cell
    {
        while (tab[z] != "")
        {
            products.Rows[i][y] = tab[z];
            y++;
            z++;
        }            
        i++;
        y = 1;
        z = 0;

    }                  

    var grid = new GridView();
    grid.DataSource = products;
    grid.DataBind();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=Activity.xls");
    Response.ContentType = "application/ms-excel";

    Response.Charset = "";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();

    return View("Excel");
}
[授权]
公共操作结果Excel(字符串用户)
{
var products=new System.Data.DataTable(“活动”);//创建数据表
products.Columns.Add(“Project”,typeof(string));//添加一列
IEnumerable lp=\u service.List(strUser、strYear、strMonth);//创建项目列表
UserActivityDb db=新的UserActivityDb(System.Configuration.ConfigurationManager.ConnectionString[“DefaultConnection”].ConnectionString);
lp=lp.Where(p=>(true==db.existorActivity(int.Parse(strYear)、int.Parse(strMonth)、ListDaysOfMonth、p.projectd、strUser));
List lstInts=新列表();
foreach(lp中的项目p)
{
string strInts=db.getFormattedData(int.Parse(strYear),int.Parse(strMonth),ListDaysOfMonth,p.ProjectId,strUser,null,0);
字符串[]int=strInts.Split(“;”);
添加(整数);
products.Rows.Add(p.Name、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0、0);//添加一个空行,稍后用正确的值归档
}
int i=0;
int y=1;
int z=0;
foreach(lstInts中的string[]tab)//逐个单元格添加活动数据
{
而(制表符[z]!=“”)
{
产品.行[i][y]=制表符[z];
y++;
z++;
}            
i++;
y=1;
z=0;
}                  
var grid=new GridView();
grid.DataSource=产品;
grid.DataBind();
Response.ClearContent();
Response.Buffer=true;
AddHeader(“内容处置”、“附件;文件名=Activity.xls”);
Response.ContentType=“应用程序/ms excel”;
响应。Charset=“”;
StringWriter sw=新的StringWriter();
HtmlTextWriter htw=新的HtmlTextWriter(sw);
网格渲染控制(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
返回视图(“Excel”);
}

当然,您可以这样做:

Response.AddHeader("content-disposition", string.Format("attachment; filename=Activity_{0}_{1}.xls",strUser, DateTime.Now.Date.ToString("MMddyyyy")));

当然,您可以这样做:

Response.AddHeader("content-disposition", string.Format("attachment; filename=Activity_{0}_{1}.xls",strUser, DateTime.Now.Date.ToString("MMddyyyy")));

我只是想在这里为Drewjordan的方法添加一个替代方案,尽管这没有任何问题。与此方法一样,为了提高可读性:

Response.AddHeader("content-disposition", $"attachment; filename=Activity_{strUser}_{DateTime.Now.Date.ToString("MMddyyyy")}.xls");

如果您想了解更多有关插值字符串的信息,只需添加即可。

这里只需添加一种替代Drewjordan方法的方法,即使这种方法没有任何问题。与此方法一样,为了提高可读性:

Response.AddHeader("content-disposition", $"attachment; filename=Activity_{strUser}_{DateTime.Now.Date.ToString("MMddyyyy")}.xls");

如果您想了解更多有关插值字符串的信息,只需添加即可。

我已经尝试过了,但由于
响应,它给了我一个错误。AddHeader
的参数数量有限。您是否使用文件名周围的
string.Format()
进行了尝试?上面的调用在
Response.AddHeader
调用中只有2个参数。@ChristopherK。欢迎你能接受这个作为你问题的答案吗?欢迎来到SO!我已经试过了,它给了我一个错误,因为
响应。AddHeader
的参数数量有限。您是不是用文件名周围的
string.Format()?上面的调用在
Response.AddHeader
调用中只有2个参数。@ChristopherK。欢迎你能接受这个作为你问题的答案吗?欢迎来到SO!是的,我得从6.0开始,我更喜欢它。是的,我得从6.0开始,我更喜欢它。