Microsoft.Office.Interop.Excel.dll - 服务器未安装Excel

时间:2012-08-02 04:45:05

标签: c# excel office-interop

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

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

有没有办法在不安装Excel的情况下部署相同的代码?

2 个答案:

答案 0 :(得分:3)

  

我知道它必须安装Excel,所以我的问题是否我们可以在不安装Excel或任何其他方式的情况下部署相同的代码?

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

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

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

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

答案 1 :(得分:1)

试试这个 http://epplus.codeplex.com


此代码来自http://epplus.codeplex.com

您可以将字节数组保存到扩展名为.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());
            }
        }