2017-06-04 98 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) –

相关问题