如何保存在C#中的Excel文件

我正在创build一个MVC控制器的行动,其中传递给该方法的JSON数据将写入一个Excel文件。 现在,我通过基于本博客文章中的示例使用来自数据表的硬编码数据来testingfunction。

这里是我有的代码:

[HttpPost] public ActionResult ExportData() { Microsoft.Office.Interop.Excel.Application excel; Microsoft.Office.Interop.Excel.Workbook worKbooK; Microsoft.Office.Interop.Excel.Worksheet worKsheeT; Microsoft.Office.Interop.Excel.Range celLrangE; try { excel = new Microsoft.Office.Interop.Excel.Application(); excel.Visible = false; excel.DisplayAlerts = false; worKbooK = excel.Workbooks.Add(Type.Missing); worKsheeT = (Microsoft.Office.Interop.Excel.Worksheet)worKbooK.ActiveSheet; worKsheeT.Name = "StudentRepoertCard"; worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[1, 8]].Merge(); worKsheeT.Cells[1, 1] = "Student Report Card"; worKsheeT.Cells.Font.Size = 15; int rowcount = 2; foreach (DataRow datarow in ExportToExcel().Rows) { rowcount += 1; for (int i = 1; i <= ExportToExcel().Columns.Count; i++) { if (rowcount == 3) { worKsheeT.Cells[2, i] = ExportToExcel().Columns[i - 1].ColumnName; worKsheeT.Cells.Font.Color = System.Drawing.Color.Black; } worKsheeT.Cells[rowcount, i] = datarow[i - 1].ToString(); if (rowcount > 3) { if (i == ExportToExcel().Columns.Count) { if (rowcount % 2 == 0) { celLrangE = worKsheeT.Range[worKsheeT.Cells[rowcount, 1], worKsheeT.Cells[rowcount, ExportToExcel().Columns.Count]]; } } } } } celLrangE = worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[rowcount, ExportToExcel().Columns.Count]]; celLrangE.EntireColumn.AutoFit(); Microsoft.Office.Interop.Excel.Borders border = celLrangE.Borders; border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; border.Weight = 2d; celLrangE = worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[2, ExportToExcel().Columns.Count]]; worKbooK.SaveAs("\\root\\test.xlsx"); worKbooK.Close(); excel.Quit(); } catch (Exception ex) { return Json(new { saveSuccess = false }, JsonRequestBehavior.AllowGet); } finally { worKsheeT = null; celLrangE = null; worKbooK = null; } return Json(new { saveSuccess = true }, JsonRequestBehavior.AllowGet); } //private void Form1_Load(object sender, EventArgs e) //{ // dataGridView1.DataSource = ExportToExcel(); //} public System.Data.DataTable ExportToExcel() { System.Data.DataTable table = new System.Data.DataTable(); table.Columns.Add("ID", typeof(int)); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Sex", typeof(string)); table.Columns.Add("Subject1", typeof(int)); table.Columns.Add("Subject2", typeof(int)); table.Columns.Add("Subject3", typeof(int)); table.Columns.Add("Subject4", typeof(int)); table.Columns.Add("Subject5", typeof(int)); table.Columns.Add("Subject6", typeof(int)); table.Rows.Add(1, "Amar", "M", 78, 59, 72, 95, 83, 77); table.Rows.Add(2, "Mohit", "M", 76, 65, 85, 87, 72, 90); table.Rows.Add(3, "Garima", "F", 77, 73, 83, 64, 86, 63); table.Rows.Add(4, "jyoti", "F", 55, 77, 85, 69, 70, 86); table.Rows.Add(5, "Avinash", "M", 87, 73, 69, 75, 67, 81); table.Rows.Add(6, "Devesh", "M", 92, 87, 78, 73, 75, 72); return table; } 

现在,代码运行直到保存发生的地步。 出于某种原因,找不到文件位置。 我假定文件的名称必须列在包含文件夹后面的path末尾以提供名称,但是这可能不是指定文件path的正确方法。

我真正需要做的是让用户select文件资源pipe理器中的文件位置,提供一个名称,然后保存该文件。 既然是这样,文件path将不得不dynamic地提供。 我看过很多SOpost和文章,但我还没有看到如何做到这一点的明确例子。

如何修改代码以使用户能够指定文件名和path?

您不能select从浏览器保存文件。 你需要提供文件,让他们下载并保存在他们喜欢的地方。

此外,您希望部署的生产ASP.NET应用程序的服务器可能没有安装Excel的副本(即使它的互操作性得到了一点混乱的恕我直言),所以你可能想要使用一个openPML库,如EPPlus 。

这会让你做这样的事情:

 public IActionResult ExportData() { using (var excel = new ExcelPackage()) { var wks = excel.Workbook.Worksheets.Add("StudentReportCard"); wks.Cells[1,1].LoadFromCollection(GetStudentRecords(), PrintHeaders:true); return File(excel.GetAsByteArray(),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "export.xlsx"); }; } private static IEnumerable<StudentRecord> GetStudentRecords() { yield return new StudentRecord { Id = 1, Name = "John", Subject = "Maths", Score = 77.9 }; yield return new StudentRecord { Id = 2, Name = "Jane", Subject = "Maths", Score = 78.9 }; yield return new StudentRecord { Id = 3, Name = "Jo", Subject = "Maths", Score = 99.9 }; } 

其中发送一个名为“export.xlsx”的文件,供用户从浏览器中保存:

在这里输入图像说明