Click here to Skip to main content
15,914,014 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to select diffrent sheets of a single excel file, but the next OleDbDataAdapter uses the previous sheet data (my excel file has two sheets namly Rv and NumOfmachine).


this is my code:
the DataNumOfmachine uses the Rv data sheet.




C#
OleDbConnection Amir = new OleDbConnection();
    OleDbCommand AmirCom = new OleDbCommand();
    OleDbDataAdapter DataRv, DataNumOfmachine = new OleDbDataAdapter();
    DataSet AmirDataSet = new DataSet();
    DataTable AmirDataTable = new DataTable();
    
    Amir.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\DataSource.xlsx;Extended Properties='Excel 12.0;HDR=YES';";
    //Amir.Open();
    
    

    DataRv = new OleDbDataAdapter("select * from [Rv$]", Amir);
    //AmirData.Fill(AmirDataSet);
    DataRv.Fill(AmirDataTable);

    Double[,] Rv = new Double[G, O];
    for (g = 0; g < G; g++)
        for (o = 0; o < O; o++)
            Rv[g, o] = int.Parse(AmirDataTable.Rows[g][o]+ "");

    

 
    DataNumOfmachine = new OleDbDataAdapter("select * from [NumOfmachine$]", Amir);
    //DataNumOfmachine.Fill(AmirDataSet);
    DataNumOfmachine.Fill(AmirDataTable);
    
    int[] NumOfmachine = new int[S];
    for (s = 0; s < S; s++)
        NumOfmachine[s] = int.Parse(AmirDataTable.Rows[0][s] + "");


What I have tried:

How can I select different Excel sheets?
Posted
Updated 17-Jun-18 22:30pm
v2
Comments
Richard MacCutchan 16-Jun-18 10:05am    
"select * from [NumOfmachine$]"
Just use the correct sheet name in your SQL statement.
Amir2018 16-Jun-18 17:20pm    
how is the the correct sheet name?
[no name] 17-Jun-18 9:46am    
Make sure to refer correct worksheet name in your select query for second work sheet. Also why are you dumping data in same table holding previously populated worksheet data, if you need to make sure to make it null before you assign second worksheet data or create seperate table for new worksheet.

1 solution

If you would like to enum Excel sheets via OleDb, you can use OleDbConnection.GetSchema Method (String) (System.Data.OleDb)[^]

Usage:
string sFileName = @"FullFileNmae.xlsx";
string sConStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES';", sFileName);

using (OleDbConnection connection = new OleDbConnection(sConStr))
{
    connection.Open();
    DataTable dt = new DataTable();
    dt = connection.GetSchema("TABLES");
    var sheets = dt.AsEnumerable()
        .Select(x=>x.Field<string>("TABLE_NAME"))
        .ToList();

}


Above code should return a list of sheet names, for example:
Sheet2$ 
Sheet3$ 
Sheet1$


Good luck!
 
Share this answer
 
Comments
Amir2018 21-Jun-18 17:18pm    
Thanks a lot
Maciej Los 21-Jun-18 17:36pm    
You're very welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900