Click here to Skip to main content
15,895,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void ReadExcelFile(int InstanceId, int ProjectID, string file, int NumberOfCrawlInstances)
        {
            string fileType = Path.GetExtension(file);
            string SourceConstr;
            System.Data.DataTable dtExcel = new System.Data.DataTable();
            dtExcel.TableName = "MyExcelData";
            if (fileType == ".xls")
            {
                SourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + file + "';;Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
            }
            else
            {
                SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + file + "';;Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1'";
            }
            OleDbConnection con = new OleDbConnection(SourceConstr);
           <big> string query = "Select * from [Sheet1$]";</big>
            OleDbDataAdapter data = new OleDbDataAdapter(query, con);
            data.Fill(dtExcel);

            int RowsCount = dtExcel.Rows.Count;
            if (RowsCount > 0)
            {
                DataTable dt = ConvertInput(dtExcel, ProjectID);
                AttributesBulkCopy(dt, RowsCount, InstanceId);
                SplitInstances(InstanceId, NumberOfCrawlInstances);
                ManageInstance(InstanceId, RowsCount, 1);
            }
        }



This is console application...Here the Select querey is fetch data from sheet 1..i want to fetch even the data in sheet2 or sheet3 how can i change that code..pls help me
Posted

string query = "Select * from [Sheet2$]";
 
Share this answer
 
Firstly you should get names of the workbook's sheets, them cycle through these names and do your selects
C#
...
// OleDbConnection is built already
// Getting datatable with sheets names
System.Data.DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if(dt != null){
    string[] esheets = new String[dt.Rows.Count];
    int i = 0;

    // Add the sheets to an array
    foreach(DataRow row in dt.Rows){
        esheets[i] = row["TABLE_NAME"].ToString();
        i++;
    }
    // Read all the sheets you wish to select from...
   for(int j=0; j < esheets.Length; j++){
        // Use System.Data.OleDb.OleDbParameter instead of this:
        string query = "Select * from " + esheets[j];
        ...
   }
}

Good luck :)
 
Share this answer
 

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