如何在服务器目录中保存导出的 excel
How to save exported excel at server directory
我想在 Excel sheet 中导出数据 table 并保存在 MVC 应用程序的服务器目录中。这是我的代码-
//ManagEmployeeController.cs
public JsonResult ExportToExcel()
{
Excel.ExcelUtlity obj = new Excel.ExcelUtlity();
DataTable dt = ConvertToDataTable(ListEmployee());
string dir = string.Format("~/Clients/ExportedData/");
var directoryToSaveFile = Server.MapPath(dir);
string uniqueNumber = DateTime.Now.ToString("yyyyMMddHHmmss");
string file = "ContactExportData.xlsx";
string newFileName = string.Format("{0}{1}", uniqueNumber, file);
if (!Directory.Exists(directoryToSaveFile))
{
Directory.CreateDirectory(directoryToSaveFile);
}
string fullFilePath = string.Format("{0}/{1}",dir,newFileName); ;
//obj.WriteDataTableToExcel(dt, "Person Details", "D:\testPersonExceldata.xlsx", "Details");
obj.WriteDataTableToExcel(dt, "Person Details", fullFilePath, "Details");
var result = new { Success = "Success", Messaage = "SuccessMessage" };
return Json(result,JsonRequestBehavior.AllowGet);
}
目录已创建,但文件未保存在此处。但是如果我使用注释代码(obj.WriteDataTableToExcel(dt, "Person Details", "D:\testPersonExceldata.xlsx", "Details");
),文件将保存在我的本地目录 D.
//ExcelUtility.cs
public bool WriteDataTableToExcel(System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string ReporType)
{
Microsoft.Office.Interop.Excel.Workbook excelworkBook;
//
//
excelworkBook.SaveAs(saveAsLocation);;
}
What is missing in my code in order to save Excel to mentioned directory on server?
string fullFilePath = string.Format("{0}/{1}",dir,newFileName);
应该是:
string fullFilePath = string.Format("{0}/{1}",directoryToSaveFile,newFileName);
我想在 Excel sheet 中导出数据 table 并保存在 MVC 应用程序的服务器目录中。这是我的代码-
//ManagEmployeeController.cs
public JsonResult ExportToExcel()
{
Excel.ExcelUtlity obj = new Excel.ExcelUtlity();
DataTable dt = ConvertToDataTable(ListEmployee());
string dir = string.Format("~/Clients/ExportedData/");
var directoryToSaveFile = Server.MapPath(dir);
string uniqueNumber = DateTime.Now.ToString("yyyyMMddHHmmss");
string file = "ContactExportData.xlsx";
string newFileName = string.Format("{0}{1}", uniqueNumber, file);
if (!Directory.Exists(directoryToSaveFile))
{
Directory.CreateDirectory(directoryToSaveFile);
}
string fullFilePath = string.Format("{0}/{1}",dir,newFileName); ;
//obj.WriteDataTableToExcel(dt, "Person Details", "D:\testPersonExceldata.xlsx", "Details");
obj.WriteDataTableToExcel(dt, "Person Details", fullFilePath, "Details");
var result = new { Success = "Success", Messaage = "SuccessMessage" };
return Json(result,JsonRequestBehavior.AllowGet);
}
目录已创建,但文件未保存在此处。但是如果我使用注释代码(obj.WriteDataTableToExcel(dt, "Person Details", "D:\testPersonExceldata.xlsx", "Details");
),文件将保存在我的本地目录 D.
//ExcelUtility.cs
public bool WriteDataTableToExcel(System.Data.DataTable dataTable, string worksheetName, string saveAsLocation, string ReporType)
{
Microsoft.Office.Interop.Excel.Workbook excelworkBook;
//
//
excelworkBook.SaveAs(saveAsLocation);;
}
What is missing in my code in order to save Excel to mentioned directory on server?
string fullFilePath = string.Format("{0}/{1}",dir,newFileName);
应该是:
string fullFilePath = string.Format("{0}/{1}",directoryToSaveFile,newFileName);