How to enhance Performance set Data from SQL to Excel sheet by C# .net core?

Mahmoud elsayed 0 Reputation points
2024-10-29T15:49:05.1133333+00:00

I recieve 900000 row from database and I entire it in Excel sheet by XLWorkbook Liberary but it take More 20 minute it is very very slow and in sometime give my TimeOut Error , So what the solution or another tech to enhance performance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,604 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,954 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,001 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,436 Reputation points
    2024-10-30T11:17:39.04+00:00

    You might consider fetching data in chunks, each chunk goes to a new worksheet so not to hit max rows.

    SELECT col1, col2, ...
     FROM ...
     ORDER BY -- this is a MUST there must be ORDER BY statement
    -- the paging comes here
    OFFSET     10 ROWS       -- skip 10 rows
    FETCH NEXT 10 ROWS ONLY; -- take 10 rows
    

    I favor SpreadSheetLight NuGet package to do the import.

    public static void ExportToExcel(DataTable table, string fileName, bool includeHeader, string sheetName)
    {
        using var document = new SLDocument();
    
        // import to first row, first column
        document.ImportDataTable(1, SLConvert.ToColumnIndex("A"), table, includeHeader);
    
        // give sheet a useful name
        document.RenameWorksheet(SLDocument.DefaultFirstSheetName, sheetName);
    
        document.SaveAs(fileName);
    }
    

    Another option is using EF Core, Skip/Take then using ExcelMapper NuGet package to export to Excel.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.