繁体   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