Home » excel » Reading multiple Excel worksheets inside a single xlsx using c#

Reading multiple Excel worksheets inside a single xlsx using c#

Posted by: admin May 14, 2020 Leave a comment

Questions:

Using c# I can successfully open an excel document and read the data in the first worksheet with the code below. However, my .xlsx has multiple worksheets so I would like to loop through the worksheet collection rather than hard coding the name of each worksheet. Many thanks.

       string path = @"C:\Extract\Extract.xlsx";
       string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

       string sql = "SELECT * FROM [Sheet1$]";

       using (OleDbDataAdapter adaptor = new OleDbDataAdapter(sql, connStr))
       {
           DataSet ds = new DataSet();
           adaptor.Fill(ds);

           DataTable dt = ds.Tables[0];
       }
How to&Answers:

I’m about to work on almost the same problem.
I found the guide at http://www.dotnetperls.com/excel quite useful.

In short, to open worksheet no. 3, add the following code after opening the excel workbook:

var worksheet = workbook.Worksheets[3] as
        Microsoft.Office.Interop.Excel.Worksheet;

Hope this answered your question.

Answer:

I used most of the code in the answer here [Reading multiple excel sheets with different worksheet names that was kindly pointed out to me in a comment on my question.
It wouldn’t compile for me in VS 2013 though as the DataRow object does not have have the property Item (- r.Item(0).ToString in that code). So I just changed that little bit. It also brought back some worksheet that had Print_Area in its name which wasn’t valid so I took that out of my loop. Here is the code as it worked for me.

       string path = @"C:\Extract\Extract.xlsx";
       string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

       DataTable sheets = GetSchemaTable(connStr);

       string sql = string.Empty;
       DataSet ds = new DataSet();
       foreach (DataRow dr in sheets.Rows)
       {  //Print_Area
           string WorkSheetName = dr["TABLE_NAME"].ToString().Trim();

           if (!WorkSheetName.Contains("Print_Area"))
           {
               sql = "SELECT * FROM [" + WorkSheetName + "]";
               ds.Clear();
               OleDbDataAdapter data = new OleDbDataAdapter(sql, connStr);
               data.Fill(ds);

               DataTable dt1 = ds.Tables[0];

               foreach (DataRow dr1 in dt1.Rows)
               {
                   //parsing work
               }
           }
       }

    static DataTable GetSchemaTable(string connectionString)
    {
        using (OleDbConnection connection = new
                   OleDbConnection(connectionString))
        {
            connection.Open();
            DataTable schemaTable = connection.GetOleDbSchemaTable(
                OleDbSchemaGuid.Tables,
                new object[] { null, null, null, "TABLE" });
            return schemaTable;
        }
    }

Answer:

I’d recommend using EPPlus (available via Nuget https://www.nuget.org/packages/EPPlus/ ) it’s a great wrapper tool for working with .xlsx spreadsheets in .Net .In it worksheets are a collection and so you can do what you want by just looping round them, regardless of name or index.

For example,

using (ExcelPackage package = new ExcelPackage(new FileInfo(sourceFilePath)))
{
   foreach (var excelWorksheet in package.Workbook.Worksheets)
   ...
}

Answer:

You should try the Open XML Format SDK (Nuget: Link). The link below explains both reading and writing Excel documents:
http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK

Oh by the way, office doesn’t have to be installed to use…