I want to export data table in Excel sheet and saving in server directory in MVC application. Here is my code-
//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);
}
The directory gets created but file does not saved here. But if I use commented code( obj.WriteDataTableToExcel(dt, "Person Details", "D:\\\\testPersonExceldata.xlsx", "Details");
) the file gets saved on my local directory 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.