Microsoft.Office.Interop.Excel.dll – 服务器没有安装Excel
我使用“Microsoft.Office.Interop.Excel.dll”生成Excel文件,我的部署服务器不允许安装Excel?
我知道它的强制安装Excel,所以我的问题是:
有什么办法可以在不安装Excel的情况下部署相同的代码?
我知道它的强制安装Excel,所以我的问题是否有办法,我们可以部署相同的代码,而无需安装Excel或任何其他方式build议?
不,必须安装Excel。 但是你已经知道了,因为你是这样开始的。
库的名称(Microsoft.Office.Interop.Excel.dll)是一个很好的线索。 它说Interop ,这是互操作性的缩写。 而且你不能与不存在的东西进行互操作。 因此,必须安装Excel才能使用便于与Excel互操作的DLL。
即使你忽略了所有的法律问题,这也不合逻辑。
如果您确实无法安装Excel,则需要find其他方法来创buildExcel文件。 有一些图书馆声称要这样做,但他们有其局限性。 例如:
尝试这个
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()); } }