Excel to dataTable -
i need fetch sheet excel datatable. first tried linqtoexcel library, fetched large numbers excel sheet exponential numbers. i'm talking big numbers "2352143523453452334544". if formated text work ok. after i've tried :
oledbconnection con = null; system.data.datatable dt = null; system.data.datatable datatable1 = new system.data.datatable(); string constr = "provider=microsoft.jet.oledb.4.0;" + "data source=" + uploadfilename + ";extended properties=excel 8.0;"; string sql_xls; con = new oledbconnection(constr); con.open(); //oracledataadapter oda = new oracledataadapter(); //oraclecommand cmd = new oraclecommand("select * [sheet1$]", con); dt = con.getoledbschematable(oledbschemaguid.tables, null); string[] excelsheetnames = new string[dt.rows.count]; int = 0; foreach (system.data.datarow row in dt.rows) { excelsheetnames[i] = row["table_name"].tostring(); i++; } sql_xls = "select * [" + excelsheetnames[0] + "]"; oledbdataadapter dataadapter = new oledbdataadapter(sql_xls, constr); system.data.dataset mydataset = new system.data.dataset(); dataadapter.fill(mydataset, "excelinfo"); datatable1 = mydataset.tables["excelinfo"];
this 1 returned same values in same conditions null. isn't there simple way fetch data excel file is? no conversions, no nothing. take string, , put datatable ?
this used , worked me:
private datatable loadxls(string strfile, string sheetname) { datatable dtxls = new datatable(sheetname); try { string strconnectionstring = ""; if(strfile.trim().endswith(".xlsx")) { strconnectionstring = string.format("provider=microsoft.ace.oledb.12.0;data source={0};extended properties=\"excel 12.0 xml;hdr=yes;imex=1\";", strfile); } else if(strfile.trim().endswith(".xls")) { strconnectionstring = string.format("provider=microsoft.jet.oledb.4.0;data source={0};extended properties=\"excel 8.0;hdr=yes;imex=1\";", strfile); } oledbconnection sqlconn = new oledbconnection(strconnectionstring); sqlconn.open(); oledbdataadapter sqladapter = new oledbdataadapter(); string sql = "select * [" + sheetname + "$]"; oledbcommand selectcmd = new oledbcommand(sql, sqlconn); sqladapter.selectcommand = selectcmd; sqladapter.fill(dtxls); sqlconn.close(); } catch (exception) { throw; } return dtxls; }
but can try export csv well: linqtocsv
Comments
Post a Comment