/// <summary> /// Return dataset representing attached excel file /// </summary> /// <param name="ds" /> /// <returns> public DataSet FillDataSet(DataSet ds,int rows_to_ignore) { int max_empty_allowed = 2; if (ds == null) ds = new DataSet(); DataSet _tmpds = new DataSet(); this._dc = new System.Data.OleDb.OleDbConnection(this.Connection); this._dcomm = new System.Data.OleDb.OleDbCommand(); this._dcomm.Connection = this._dc; this._dcomm.CommandText = this.CommandText; this._adapter = new System.Data.OleDb.OleDbDataAdapter(this._dcomm); try { this._adapter.Fill(ds); if (ds.Tables.Count == 0) return ds; if (rows_to_ignore > 0) { while(rows_to_ignore > 0) { ds.Tables[0].Rows[0].Delete(); rows_to_ignore--; } ds.Tables[0].AcceptChanges(); } _tmpds = ds.Clone(); ///Ignore empty rows int empty_row = 0; foreach(DataRow _dr in ds.Tables[0].Rows) { int emptyCount = 0; foreach(DataColumn _dc in ds.Tables[0].Columns) { if (_dr[_dc] == DBNull.Value) emptyCount++; } if (emptyCount != ds.Tables[0].Columns.Count) _tmpds.Tables[0].ImportRow(_dr); else { empty_row++; /// Reached empty allowed count ignore rest of excel sheet if (empty_row >= max_empty_allowed) break; } } this._dc.Dispose(); return _tmpds; } catch(System.Data.OleDb.OleDbException oex) { throw oex; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)