【问题标题】:Generating Excel Documents with ASP.NET Website使用 ASP.NET 网站生成 Excel 文档
【发布时间】:2015-10-23 22:03:03
【问题描述】:

我有一个 ASP.NET 应用程序,它可以帮助用户创建一个包含某些数据的 Gridview。生成此表后,我希望用户按下按钮并能够将表保存为 Excel 文档。我知道有两种不同的方法:

  1. 使用带有 ContentType“application/vnd.ms-excel”的 HtmlTextWriter 将文件作为 HttpResponse 发送。我使用 GridView1.RenderControl(htmlTextWriter) 来呈现网格视图。这几乎可以工作,但是当文件打开时,excel文件总是显示警告,因为内容与扩展名不匹配。我尝试了各种内容类型均无济于事。我想这是有道理的,因为我使用的是 HtmlWriter。这似乎也不是一个好习惯。

  2. 我尝试的第二件事是使用 Office 自动化生成 Excel 文件。但是要生成文件,我需要将其保存到磁盘然后再次读取。根据我的阅读,这是唯一的方法,因为 Excel 对象只有在保存后才会成为真正的 Excel 文件。我发现 Excel 类中的 .saveas 方法会因为写入权限而引发异常,即使我尝试保存在 App_Data 文件夹中也是如此。所以我做了一些研究,发现 Web 服务显然不鼓励使用 Office 自动化:https://support.microsoft.com/en-us/kb/257757

微软目前不推荐也不支持, 无人值守的 Microsoft Office 应用程序自动化, 非交互式客户端应用程序或组件(包括 ASP、 ASP.NET、DCOM 和 NT 服务),因为 Office 可能表现出不稳定 Office 在此环境中运行时出现的行为和/或死锁。

肯定有一种保存方法可以让网站生成 Excel 文件并将其提供给用户!?我无法想象这个问题没有得到解决或者如此罕见以至于没有人关心它,但是我找不到任何好的解决方案。

【问题讨论】:

    标签: asp.net excel office-automation


    【解决方案1】:

    创建 excel 文件最简单(也是最好)的方法是使用 epplus

    Epplus sample for webapplication

                using (ExcelPackage pck = new ExcelPackage())
                {
                    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
    
                    //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                    ws.Cells["A1"].LoadFromDataTable(tbl, true);
    
                    //Format the header for column 1-3
                    using (ExcelRange rng = ws.Cells["A1:C1"])
                    {
                        rng.Style.Font.Bold = true;
                        rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                        rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));  //Set color to dark blue
                        rng.Style.Font.Color.SetColor(Color.White);
                    }
    
                    //Example how to Format Column 1 as numeric 
                    using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
                    {
                        col.Style.Numberformat.Format = "#,##0.00";
                        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    }
    
                    //Write it back to the client
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
                    Response.BinaryWrite(pck.GetAsByteArray());
    }
    

    【讨论】:

    • 这看起来是一个有用的工具,但我正在为我的公司纠正这个网络应用程序,我认为 GNU 许可证不会奏效。由于这里和我搜索过的其他线程中缺乏答案,我得到的印象是没有 3rd 方工具就没有好方法!?真的是这样吗?
    • epplus 是最好的方法,如果你想要一个产品,我可以推荐软工匠 excel / office writer,但它很贵
    • 没有第三方,是的,epplus 是用 c# 和 .net 编写的,所以看看源代码并自己做同样的事情......
    • 我查看了 Epplus 页面,但在页面上的所有源代码中都找不到我需要的代码。我现在正在使用 Excel 类。它可以工作,但由于 Microsoft 的通知以及我一般不是 Office 自动化工具的粉丝,我仍然对此不太满意。仍然感谢您提供帮助。
    • 你不应该在服务器上使用 excel 自动化
    猜你喜欢
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多