簡體   English   中英

如何在服務器目錄中保存導出的Excel

[英]How to save exported excel at server directory

我想在Excel工作表中導出數據表並保存在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM