25 thoughts on “C# NPOI – How to Read and Write Excel files in .NET

  1. Thanks for this, it was a big help. I know it’s an old post but there is also an issue where an empty cell in the data will be skipped, putting the cells in the DataTable out of sync with the headers (try an excel sheet with a missing cell and see what I mean).

    You can fix this with an ELSE statement, to ensure that the cell is added with a default empty value

    if (row.GetCell(j) != null)
    {
    if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
    {
    rowList.Add(row.GetCell(j).ToString());
    }
    }else
    {
    //add a default value if cell is empty, otherwise it will skip the cell and will be out of sync with the header
    rowList.Add(“”);
    }

  2. Thank you for this excellent article. However, you should always add all essentials coding parts. Without the required ‘using’ clauses, one won’t get the classes definitions identified by the IDE.

    1. Hey Rajesh- Thanks for the inputs! Glad it helped you! Sure will update the ‘using’ statements used in the article.

  3. hi thanks for the exemples.
    i have question : what is the best way to do to use a template xlsx file, fill it without saving the template, and save the work in a new xlsx file?
    thanks for the help

    1. Hi Khalid- Thanks for your query. You can use the read method above for reading the template and fill it in memory as table or dataset (if file size is not big ) later use in-memory data to save it as new excel.

  4. Hello Sir .. I got a very big help from this article . Thumbs up.
    Can you please a code snippet where i can format cell

    1. Hello Naved, Thank you. glad it helped you. do you have any specific requirements for cell formatting, let me know I shall put some samples on it.

  5. Do you know how I can get help on using NPOI from vba? I’ve downloaded and built NPOI.dll, but I don’t know how to call it and I can’t find any examples of vba usage. My purpose is to build an excel file from an Access runtime on a machine where office is not installed.

    1. Hi- Thanks for your query. I have not tried NPOI using VBA yet. I shall post more on it if happen to get more details.

  6. Thanks for your examples. I just want to point out that this is a coding issue:

    if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
    {
    dtTable.Columns.Add(cell.ToString());
    }

      1. I have tried but not getting the correct result , If you know request to help
        Need to copy Excel Macro in object using C# ?

  7. Hello, I want to excel library with NPOI, EEPLUS, OpenXML..
    can i free to use it?

    i think that EEPLUS for a fee.

    1. Hey John- Thanks for your query! As I am aware OpenXML is free and Open-sourced from Microsft. For EPPULUS and NPOI kindly check their licensing terms and conditions. Have a good day!

  8. Hello, this is an excellent article, can you please show us example how to export byte[] array image to excel?

Leave a Reply

Your email address will not be published. Required fields are marked *