Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ASP.NET网站生成Excel文档_Asp.net_Excel_Office Automation - Fatal编程技术网

使用ASP.NET网站生成Excel文档

使用ASP.NET网站生成Excel文档,asp.net,excel,office-automation,Asp.net,Excel,Office Automation,我有一个ASP.NET应用程序,可以帮助用户创建包含某些数据的Gridview。生成此表后,我希望用户按下按钮,并能够将该表保存为Excel文档。我知道有两种不同的方法: 使用内容类型为“application/vnd.ms excel”的HtmlTextWriter将文件作为HttpResponse发送。我使用GridView1.RenderControl(htmlTextWriter)渲染gridview。这几乎可以工作,但excel文件在打开时总是显示警告,因为内容与扩展名不匹配。我尝试过

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

  • 使用内容类型为“application/vnd.ms excel”的HtmlTextWriter将文件作为HttpResponse发送。我使用GridView1.RenderControl(htmlTextWriter)渲染gridview。这几乎可以工作,但excel文件在打开时总是显示警告,因为内容与扩展名不匹配。我尝试过各种内容类型,但都没有用。我想这是有道理的,因为我使用的是HtmlWriter。这似乎也不是一个好的做法

  • 我尝试过的第二件事是使用Office Automation生成Excel文件。但是对于要生成的文件,我需要将其保存到磁盘,然后再次读取。据我所知,这是唯一的方法,因为Excel对象只有在保存后才会变成真正的Excel文件。我发现Excel类中的.saveas方法会因为写入权限而引发异常,即使我试图保存在App_数据文件夹中。因此,我做了一些研究,发现web服务显然不鼓励使用办公自动化:

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

    一定有一个保存的方法让网站生成一个Excel文件并提供给用户!?我无法想象这个问题没有解决,或者很少有人关心它,但是我找不到任何好的解决办法。

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


    这看起来是一个有用的工具,但我正在为我的公司纠正这个web应用程序,我认为GNU许可证不会奏效。从这里以及我在谷歌搜索的其他帖子中找不到答案,我得到的印象是,没有第三方工具就没有好办法!?真的是这样吗?ePlus是最好的方法,如果你想要一个产品,我可以推荐soft artisans excel/office writer,但是如果没有第三方,它相当昂贵,是的,epplus是用c#和.net编写的,所以看一下源代码,自己做同样的事情……我看了epplus页面,但在页面上的所有源代码中都找不到我需要的代码。我现在正在使用Excel类。它可以工作,但我还是不太高兴,因为微软的通知,因为我不是一个办公室自动化工具的球迷一般。仍然感谢您的帮助。您不应该在服务器上使用excel自动化
                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());
    }