Error generating excel file

2

When the system runs on the server this error occurs: ERROR:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

There are other systems on the same server that do not give error, this is the error generated:

Onmymachineitworksperfectly,thisisthecodethatgeneratestheexcelfile:

protectedvoidTESTE_GerarExcel(){#region::GERARARQUIVOEXCEL::varexcelApp=newExcel.Application();excelApp.Workbooks.Add();Excel._WorksheetworkSheet=(Excel.Worksheet)excelApp.ActiveSheet;workSheet.Cells[1,"A"] = "NOME CLIENTE";
            workSheet.Cells[1, "B"] = "DESCRIÇÃO";
            workSheet.Cells[1, "C"] = "QTDE PARCELA";
            workSheet.Cells[1, "D"] = "VALOR PARCELA";
            workSheet.Cells[1, "E"] = "DATA VENCIMENTO";
            workSheet.Cells[1, "F"] = "SITUAÇÃO";
            workSheet.Cells[1, "G"] = "BOLETO ENVIADO";
            var row = 1;

            row++;
            workSheet.Cells[row, "A"] = "IND. FARMACÊUTICA";
            workSheet.Cells[row, "B"] = "ALUGUE DE SALA";
            workSheet.Cells[row, "C"] = "2";
            workSheet.Cells[row, "D"] = "200,10";
            workSheet.Cells[row, "E"] = "30/05/2016";
            workSheet.Cells[row, "F"] = "PENDENTE";
            workSheet.Cells[row, "G"] = "NÃO ENVIADO";


            workSheet.Columns[1].AutoFit();
            workSheet.Columns[2].AutoFit();
            workSheet.Columns[3].AutoFit();
            workSheet.Columns[4].AutoFit();
            workSheet.Columns[5].AutoFit();
            workSheet.Columns[6].AutoFit();
            workSheet.Columns[7].AutoFit();

            workSheet.Range["A1", "G1"].AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatSimple);


            excelApp.Columns.AutoFit();

            string nome = "Contas_a_Receber_" + System.DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_");

            var path = Path.Combine(Server.MapPath("~/Content/Uploads"), nome);

            excelApp.ActiveWorkbook.SaveCopyAs(path + ".xlsx");

            #endregion

        }
    
asked by anonymous 20.05.2016 / 17:54

1 answer

1

Avoid using Microsoft.Office.Interop.Excel in ASP.NET MVC projects. It depends on registration as COM library.

Instead, use the EPPlus library. It does not need to register with COM and is quite similar to use.

See here examples of use .

    
20.05.2016 / 23:17