Using ExcelDataReader to read Excel data starting from a particular cell

后端 未结 8 1867
感情败类
感情败类 2020-12-02 22:46

I am using ExcelDataReader to read data from my Excel workbook in C#.
But structure of my Excel sheet is such that data to be read can start from any particular cell and

8条回答
  •  盖世英雄少女心
    2020-12-02 23:20

    You could use the .NET library to do the same thing which i believe is more straightforward.

    string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; data source={path of your excel file}; Extended Properties=Excel 12.0;";
    
            OleDbConnection objConn = null;
            System.Data.DataTable dt = null;
            //Create connection object by using the preceding connection string.
            objConn = new OleDbConnection(connString);
            objConn.Open();
            //Get the data table containg the schema guid.
            dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string sql = string.Format("select * from [{0}$]", sheetName);
            var adapter = new System.Data.OleDb.OleDbDataAdapter(sql, ConnectionString);
            var ds = new System.Data.DataSet();
            string tableName = sheetName;
            adapter.Fill(ds, tableName);
            System.Data.DataTable data = ds.Tables[tableName];
    

    After you have your data in the datatable you can access them as you would normally do with a DataTable class.

提交回复
热议问题