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

関連する問題