Search code examples
c#epplus

How do you append data to an existing Excel file?


How do I append data to an already existing Excel file.

Let's say there can be a variable amount of rows already written to a file and I need to get the next row to write on.

I was thinking check for 2 blank rows and then write on the 2nd row or something like that.

How would I do this? Is there a way in EPPlus to open an Excel file and find the last line or something?


Solution

  • The Worksheet.Dimension should get you what you need. So if you have a sheet like this:

    enter image description here

    You can does this:

    using (var package = new ExcelPackage(excelFile))
    {
        var ws = package.Workbook.Worksheets.First();
        var lastRow = ws.Dimension.End.Row;
        var lastColumn = ws.Dimension.End.Column;
    
        Console.WriteLine($"Last Row: {lastRow}");
        Console.WriteLine($"Last Column: {lastColumn}");
    }
    

    Which gives in console:

    Last Row: 9
    Last Column: 6