DataTable dt = new DataTable();
System.Data.DataTable ExcelTable;
DataSet dst = new DataSet();
string strconn = "";
if (this.currFileExtension==".xls")//这样判断不太好 暂时未找出判断Excel版本的解决办法
{
strconn = "provider=microsoft.jet.oledb.4.0;" + "data source=" + currFilePath + ";" + "extended properties=excel 8.0;";
}
else {
strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + currFilePath + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
}
OleDbConnection objConn = new OleDbConnection(strconn);
objConn.Open();
System.Data.DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
for (int i = 0; i < schemaTable.Rows.Count; i++)
{
string tableName = schemaTable.Rows[i][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1
string strSql = "select * from [" + tableName + "]";
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
myData.Fill(dst, tableName);//填充数据
ExcelTable = dst.Tables[tableName];
dt = dst.Tables[tableName];
objConn.Close();
--循环调用写入数据库
}