2017-06-04 97 views
0

我想導出Excel表中的數據表並將其保存在MVC應用程序的服務器目錄中。這裏是我的代碼 -如何將導出的excel保存在服務器目錄中

//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? 

回答

1

string fullFilePath = string.Format("{0}/{1}",dir,newFileName);

應該是:

string fullFilePath = string.Format("{0}/{1}",directoryToSaveFile,newFileName);

+0

謝謝,它現在工作。由於名聲較差而無法投票(15) –

相關問題