简体   繁体   English

如何在服务器目录中保存导出的Excel

[英]How to save exported excel at server directory

I want to export data table in Excel sheet and saving in server directory in MVC application. 我想在Excel工作表中导出数据表并保存在MVC应用程序的服务器目录中。 Here is my code- 这是我的代码-

//ManagEmployeeController.cs //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);
        }

The directory gets created but file does not saved here. 目录已创建,但文件未保存在此处。 But if I use commented code( obj.WriteDataTableToExcel(dt, "Person Details", "D:\\\\testPersonExceldata.xlsx", "Details"); ) the file gets saved on my local directory D. 但是,如果我使用注释代码( obj.WriteDataTableToExcel(dt, "Person Details", "D:\\\\testPersonExceldata.xlsx", "Details"); )),文件将保存在本地目录D中。

//ExcelUtility.cs //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.

相关问题 如何使用Stimulsoft Reports将导出的报告保存在客户端而不是服务器中 - How to save an exported report in client not server using Stimulsoft Reports 如何将从datagridview导出的Excel文件保存到具有唯一名称的特定文件夹中? - How to save Excel file exported from datagridview in a particular folder with unique name? 如何在另一台服务器上保存文件(pdf/excel)? - How to save files (pdf/excel) in another server? 打开导出的Excel文件并尝试保存,并另存为网页格式 - open exported excel file and try to save it, is saving as web page format 如何格式化导出到Excel的DataGridView的背景? - How to format background of DataGridView exported to Excel? 如何自控制台应用程序自定义导出的Excel? - How to Customize Exported Excel from Console Application? 无法将导出的Excel文件上载到SQL Server 2008 - Unable to upload the exported excel file to sql server 2008 如何将两个SQL Server表的比较保存到Excel文件? - How to save comparison of two SQL Server tables to Excel file? C#:尝试保存导出的excel文件,但将其另存为网页(.html) - C#: Try to save Exported excel file but it save as Web Page(.html) 将DataTable导出到Excel并保存到本地目录 - Export DataTable to Excel and save to local directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM