Reading excel file using EPPlus package

Posted on August 22, 2018 by Suresh Kamrushi in asp.net

Here I am sharing how you can read excel file using EPPlus package. Many solution given on internet but nothing work for me, so I thought of sharing what worked for me.
In below example method required excel file name (full server path) and then it will read first sheet of the workbook. You can modify it according to your requirement.

public void readXLS(string FilePath)
{
    FileInfo existingFile = new FileInfo(FilePath);
    using (ExcelPackage package = new ExcelPackage(existingFile))
    {
        //get the first worksheet in the workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        int colCount = worksheet.Dimension.End.Column;  //get Column Count
        int rowCount = worksheet.Dimension.End.Row;     //get row count
        for (int row = 1; row <= rowCount; row++)
        {
            for (int col = 1; col <= colCount; col++)
            {
                Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
            }
        }
    }
}

If you want to insert excel data into table you can refer to my blog:

how to read data from excel sheet and insert into database table in C#

Tagged as