简体   繁体   中英

how to read data from the 3 row from excel sheet and convert to datatable

i am able read the data fine. but now i have an issue. i need to read data staring from 3 row in an excel sheet. and then convert the data to datatable

how can i set the row postion to start reading data from an excel sheet[to reading starting from 3 row] example:

 excel sheet
 1 list of names of the people
 2  Employee Name
 3 kumar
 4 kiran
 5 manu
 6 manju

so i should start reading data from 2 row in excel. so that my datatable will have

  Employee Name
     kumar
     kiran
     manu
     manju

i am using excel 2007. this is below code i am using is ther any thing tat i need to change.

public static DataTable ExcelToDataTable(string strfilelocation)
{
    OleDbConnection excelConn= new  OleDbConnection();
    DataTable dtPatterns = new DataTable();;
    try
    {
        DataSet ds = new DataSet();   
        OleDbCommand excelCommand = new OleDbCommand(); OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter();    
        string excelConnStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strfilelocation + "; Extended Properties =Excel 8.0;";    
        excelConn =new  OleDbConnection(excelConnStr);    
        excelConn.Open();    
        excelCommand = new OleDbCommand("SELECT `Employee Name` as PATTERN FROM [sheet1$]", excelConn);    
        excelDataAdapter.SelectCommand = excelCommand;    
        excelDataAdapter.Fill(dtPatterns);    
        //"dtPatterns.TableName = Patterns";    
        ds.Tables.Add(dtPatterns);    
    }    
    catch (Exception ex)    
    {    
        WriteError(ex.Message);    
    }    
    finally    
    {    
        excelConn.Close();             
    }    
    return dtPatterns;             
}

any help would greatly appreicated. looking for an a solution

use this query

SELECT `Employee Name` as PATTERN FROM [sheet1$A2:A6]

with HDR=Yes in Connection string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM