【问题标题】:how to read data from the 3 row from excel sheet and convert to datatable如何从excel表的3行中读取数据并转换为数据表
【发布时间】:2018-06-17 14:25:39
【问题描述】:

我能够很好地读取数据。但现在我有一个问题。 我需要从 Excel 表中的 3 行开始读取数据。然后将数据转换为数据表

如何设置行位置以开始从 excel 表中读取数据[从 3 行开始读取] 示例:

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

所以我应该开始从 excel 中的 2 行读取数据。以便 我的数据表会有

  Employee Name
     kumar
     kiran
     manu
     manju

我正在使用 excel 2007。 这是我正在使用的代码下面是我需要更改的任何内容。

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;             
}

任何帮助将不胜感激。寻找解决方案

【问题讨论】:

    标签: c#


    【解决方案1】:

    使用这个查询

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

    在连接字符串中使用 HDR=Yes

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2015-02-18
      • 1970-01-01
      相关资源
      最近更新 更多