2022-10-21 08:35来源:m.sf1369.com作者:宇宇
//excel文件的读取
public dataset exceltods(string path)
{
//path为excel文件的路径,取得服务器上的绝对路径
string strconn = provider=microsoft.jet.oledb.4.0; +data source=+ path +;+extended properties=excel 8.0;;
oledbconnection conn = new oledbconnection(strconn);
conn.open();
string strexcel = ;
oledbdataadapter mycommand = null;
dataset ds = null;
strexcel=select * from [sheet1$];
mycommand = new oledbdataadapter(strexcel, strconn);
ds = new dataset();
mycommand.fill(ds,table1);
return ds;
}
//对于excel中的表即sheet([sheet1$])如果不是固定的可以使用下面的方法得到
string strconn = provider=microsoft.jet.oledb.4.0; +data source=+ path +;+extended properties=excel 8.0;;
oledbconnection conn = new oledbconnection(strconn);
datatable schematable = objconn.getoledbschematable(system.data.oledb.oledbschemaguid.tables,null);
string tablename=schematable.rows[0][2].tostring().trim();
以上是简单的excel的读取操作,如有不详可以再查资料.
至于你所说的杂乱无章的数据插入多张表,可以在程序或者sql语句上作相应处理.应该是可以解决的.