Search code examples
c#exceloffice-interop

Microsoft.Office.Interop.Excel Adding cell is very slow


public async Task<HttpResponseMessage> GetImage()
    {

         excelApp.Application app = new excelApp.Application();

        excelApp.Workbook wb = app.Workbooks.Add(excelApp.XlWBATemplate.xlWBATWorksheet);

        excelApp.Worksheet ws = (excelApp.Worksheet) wb.Worksheets.get_Item(1);

        for (int i = 0; i < 21000; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                
                excelApp.Range aRange = ws.Cells[i + 2,j+1];
                
                aRange.Value2 = i + j;

            }
        }
       
        wb.SaveAs("d:\\example2.xlsx");

        wb.Close();        }

I need to create xlsx file without opening excel.. That for works very slow .. What I am doing wrong ??


Solution

  • To generate an Excel file from your .NET code, you might prefer to use a library like ClosedXML (https://github.com/ClosedXML/ClosedXML) that works with the underlying file format directly.