Microsoft.Office.Interop.Excel.dll - Server doesno

2019-06-03 15:52发布

问题:

I'm generating an Excel file using "Microsoft.Office.Interop.Excel.dll" and my deployment server does not permit to install Excel?

I know that its mandatory to install Excel, so my question is:

Is there any way that we can deploy the same code without installing Excel?

回答1:

I know that its mandatory to install Excel, so my question Is there way that we can deploy same code without installing Excel or any other way suggested?

No, it's mandatory to install Excel. But you already know that, since it was how you started the question.

The name of the library (Microsoft.Office.Interop.Excel.dll) is a good clue. It says interop, which is short for interoperability. And you can't interoperate with something that doesn't exist. Therefore, Excel must be installed in order to use a DLL that facilitates interoperation with Excel.

This doesn't make logical sense, even if you ignore all of the legal questions.

If you really are not able to install Excel, you will need to find some other way to create Excel files. There are some libraries that claim to do this, but they have their limitations. For example:

  • http://excelpackage.codeplex.com/
  • http://simpleooxml.codeplex.com/
  • http://closedxml.codeplex.com/
  • http://www.smartxls.com/
  • http://code.google.com/p/excellibrary/


回答2:

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());
            }
        }