Asp.net mvc ASP.NET MVC单击按钮即可打开Excel文件
我在页面上显示一个包含数据的表格,然后当用户单击“导出”时,我将数据放入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
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调用,找出问题所在。