0

Buen dia, Tengo un problema: Estoy haciendo una aplicacion web con MVC, C#, JS.. En esta app creo un archivo XLSX con la libreria de interop, todo estaba perfecto hasta que puse la app en el servidor de despliege, me marca un error:

System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Al parecer el detalle es porque no tengo el Interop/office instalado en el servidor, ya que yo no tengo permisos para instalar en ese servidor, habría alguna manera de importar la librería en el proyecto o crear el excel desde otro método?

agrego el codigo..

public void ExportToExcel(List<Model> EXAMPLE)
        {
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            excel.Workbooks.Add();

            Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;
 try
            {

                workSheet.Cells[1, "A"] = "Nombre";
                workSheet.Cells[1, "B"] = "Apellido";

int row = 2; // start row (in row 1 are header cells)
                foreach (Model ex in EXAMPLE)
                {
                    workSheet.Cells[row, "A"] = ex.nombre;
                    workSheet.Cells[row, "B"] = ex.apellido;
     row++;
                }
   workSheet.Range["A1"].AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic1);
      string fileName = "ExcelData.xlsx";
      path = Server.MapPath("~/Uploads");
      path = Path.Combine(path, fileName);

                if (System.IO.File.Exists(path)){
                    System.IO.File.Delete(path);
                }

                workSheet.SaveAs(path);
  }
            catch (Exception exception)
            {
exception.Message,

            }
            finally
            {
                excel.Quit();
if (excel != null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

                if (workSheet != null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
                excel = null;
                workSheet = null;

                GC.Collect();
}
}

1 respuesta 1

0

Bueno encontre la solucion por si le funciona a alguien.. Me instale ClosedXML desde nuget..

Install-Package ClosedXML -version 0.80.1

y este fue el codigo..

        //Instanciamos un documento
        XLWorkbook documento = new XLWorkbook();
        //Agregamos una hoja a nuestro documento
        IXLWorksheet workSheet = documento.Worksheets.Add("Reporte");
    try
    {
        // ------------------------------------------------
        // Creamos Tittulos de las columnas
        // ------------------------------------------------
        workSheet.Cell(1, "A").Value = "Nombre";
        workSheet.Cell(1, "B").Value = "Apellido";

        // ------------------------------------------------
        // Insertamos la informacion en los renglones.
        // ------------------------------------------------
        int row = 2; // start row (in row 1 are header cells)
        foreach (Model ex in EXAMPLE)
        {
            workSheet.Cell(row, "A").Value = ex.nombre;
            workSheet.Cell(row, "B").Value = ex.apellido;



            row++;
        }
            path = Server.MapPath("~/Uploads");
            //Directory.CreateDirectory(path);
            path = Path.Combine(path, fileName);
            documento.SaveAs(path);
    catch (Exception exception)
        {
        }
           documento = null;
           workSheet = null;

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.