如何在MVC中将创建的excel文件保存到本地服务器

时间:2015-04-06 02:43:09

标签: asp.net-mvc excel

我的控制器中有一个代码,让我创建一个excel文件。现在我想要的是将excel文件保存在我的本地服务器或服务器中。怎么做?

我希望将excel文件保存在此位置:http://localhost/reportrepository/filename.xlsx

以下是我的代码算法:

try
{
    var workbook = new Workbook();
    // some other codes that creates the workbook....

    // save workbook here to local server....

}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}

1 个答案:

答案 0 :(得分:0)

您的代码可以正常访问服务器的文件系统。棘手的部分是弄清楚如何访问您的Web应用程序的目录结构。尝试这样的事情:

string workingDirectory = Request.Params["APPL_PHYSICAL_PATH"];
string workbookPath = workingDirectory + @"\ReportRepository\filename.xls";
workbook.SaveAs(workbookPath, ...);