Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
Asp.net mvc ASP.NET MVC单击按钮即可打开Excel文件_Asp.net Mvc_Export To Excel - Fatal编程技术网

Asp.net mvc ASP.NET MVC单击按钮即可打开Excel文件

Asp.net mvc ASP.NET MVC单击按钮即可打开Excel文件,asp.net-mvc,export-to-excel,Asp.net Mvc,Export To Excel,我在页面上显示一个包含数据的表格,然后当用户单击“导出”时,我将数据放入excel文件中。现在我想打开这个文件,以便用户可以保存它或只是查看它。这是我的密码。它会提示用户保存一个文件,但该文件就是整个页面!!这里怎么了 string filename = filePath.Substring(12); System.IO.File.Copy(filePath, "C:/Work/MCTestSuiteCertificate/TS.ToDoVi

我在页面上显示一个包含数据的表格,然后当用户单击“导出”时,我将数据放入excel文件中。现在我想打开这个文件,以便用户可以保存它或只是查看它。这是我的密码。它会提示用户保存一个文件,但该文件就是整个页面!!这里怎么了

   string filename = filePath.Substring(12);
                        System.IO.File.Copy(filePath, "C:/Work/MCTestSuiteCertificate/TS.ToDoViewer/Content/" + filename, true);
                        Response.Clear();
                        Response.ContentType = @"application\xls";
                       // FileInfo file = new FileInfo(Server.MapPath("~/Content/" + filename));
                        Response.AddHeader("Filename", filename);
                        Response.ContentType = "application/xls";
                        Response.TransmitFile("C:/Work/MCTestSuiteCertificate/TS.ToDoViewer/Content/" + filename);
                        //Response.WriteFile(file.FullName);
                        Response.Flush();

使用
File
方法(返回
FileResult
)。您不应该在MVC应用程序中直接使用
响应

public ActionResult YourAction()
{
    ...
    string filename = filePath.Substring(12);
    return File(Path.Combine(@"C:\Work\MCTestSuiteCertificate\TS.ToDoViewer\Content", filename), "application/xls");
}

PS还请注意使用了
路径。组合
而不是字符串连接。

@Serg当我这样做时,我没有得到任何错误-但是excel也没有打开。我用:***检查客户端和服务器之间的HTTP调用,找出问题所在。