c# - Extract data from datatable -
i extract data sheets in workbook using following code :
foreach (var sheetname in getexcelsheetnames(connectionstring)) { if (sheetname.contains("_")) { } else { using (oledbconnection con = new oledbconnection(connectionstring)) { var datatable = new datatable(); string query = string.format("select * ,{0} sheetname [{0}]", sheetname); con.open(); oledbdataadapter adapter = new oledbdataadapter(query, con); try { adapter.fill(datatable); ds.tables.add(datatable); } catch { } } }
i can't figure how data stocked in datatable
: sheetname added column ? how can extract ?
foreach (datatable dt in ds.tables) { using (sqlconnection con = new sqlconnection(consstring)) { con.open(); (int = 0; < dt.rows.count; i++) { (int j = 0; j < dt.columns.count; j ++) { //what should write here ? } } }
in order sheet name, using oledb, need use code looks (thanks post , answer):
datatable dtsheets = con.getoledbschematable(oledbschemaguid.tables, null); list<string> sheets= new list<string>(); foreach (datarow dr in dtsheets.rows) { if (dr["table_name"].tostring().contains("$"))//checks whether row contains '_xlnm#_filterdatabase' or sheet name(i.e. sheet name ends $ sign) { sheets.add(dr["table_name"].tostring()); } }
below how access values datatable:
var somevalue = dt.rows[i][j]
you need item @ column index (j) of row, @ row index (i), of current datatable (dt).
conversely, can use name of column well.
var somevalue = dt.rows[i]["columnname"]
Comments
Post a Comment