1

我正在使用“Microsoft.Office.Interop.Excel.dll”生成一个 Excel 文件,而我的部署服务器不允许安装 Excel?

我知道必须安装 Excel,所以我的问题是:

有什么方法可以在不安装 Excel 的情况下部署相同的代码?

4

2 回答 2

3

我知道安装 Excel 是强制性的,所以我的问题是我们可以在不安装 Excel 或建议的任何其他方式的情况下部署相同的代码吗?

不,必须安装 Excel。但是您已经知道了,因为这就是您提出问题的方式。

库的名称 (Microsoft.Office.Interop.Excel.dll) 是一个很好的线索。它说interop,它是互操作性的缩写。而且你不能与不存在的东西进行互操作。因此,必须安装 Excel 才能使用便于与 Excel 互操作的 DLL。

即使您忽略所有法律问题,这也没有逻辑意义。

如果您确实无法安装 Excel,则需要找到其他方法来创建 Excel 文件。有一些图书馆声称可以这样做,但它们有其局限性。例如:

于 2012-08-02T04:47:46.700 回答
1

try this
http://epplus.codeplex.com


this code is from http://epplus.codeplex.com

you can save byte array into file with extension .xls

private void DumpExcel(DataTable tbl)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                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());
            }
        }
于 2012-08-02T05:49:26.247 回答