简体   繁体   English

如何在C#中保存Excel文件

[英]How to save an excel file in C#

I'm creating an MVC controller action where JSON data passed in to the method is to be written to an excel file. 我正在创建一个MVC控制器操作,在该操作中,传递给该方法的JSON数据将被写入到Excel文件中。 Right now, I'm testing out the functionality by using hardcoded data from a data table based on the example from this blog post . 现在,我正在根据本博客文章中的示例使用数据表中的硬编码数据来测试功能。

Here is the code I have: 这是我的代码:

[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;  
            }  

Right now, the code works up until the point where the save happens. 现在,代码会一直工作到保存发生为止。 For some reason, the file location is not found. 由于某种原因,找不到文件位置。 I was assuming that the name of the file had to be listed at the end of the path after the containing folder in order to provide the name, but maybe this isn't the correct way to specify the file path. 我假设必须在包含文件夹之后的路径末尾列出文件名才能提供名称,但是也许这不是指定文件路径的正确方法。

What I actually need to do is to allow the user to choose the file location in file explorer, provide a name, and then save the file. 我实际上需要做的是允许用户在文件浏览器中选择文件位置,提供一个名称,然后保存文件。 Since this is the case, the file path would have to be provided dynamically. 由于是这种情况,因此必须动态提供文件路径。 I've looked at many SO posts and articles but I haven't seen a clear example of how to do this. 我看过许多SO帖子和文章,但没有看到一个清晰的示例。

How should the code be modified for the user to be able to specify the file name and path? 如何修改代码以使用户能够指定文件名和路径?

You cannot choose to save the file from their browser. 您不能选择从他们的浏览器中保存文件。 You need to serve up the file and let them download it and save it where they like. 您需要提供文件,然后让他们下载并保存到他们喜欢的位置。

Also, the server you want a production ASP.NET application deployed to probably doesn't have a copy of Excel installed (and even if it does interop gets a little messy IMHO) so you probably want to use a openXml library such as EPPlus instead. 此外,您要部署到生产ASP.NET应用程序的服务器可能未安装Excel副本(即使互操作时也会出现一些混乱的恕我直言),因此您可能想使用openXml库(例如EPPlus)来代替。

This would let you do something like this: 这将使您可以执行以下操作:

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
    };
}

Which sends a file like this named 'export.xlsx' for the user to save from their browser: 它将发送一个名为“ export.xlsx”的文件供用户从浏览器保存:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM