Excel数据导入到SQL

//连接串
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + [EXCEL文件,含路径] + ";";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
         
DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
DataSet ds = new DataSet();
//一个EXCEL文件可能有多个工作表,遍历之
foreach( DataRow dr in dtSchema.Rows )
{
   string table = dr["TABLE_NAME"].ToString();
   string strExcel = "SELECT * FROM [" + table + "]";
   ds.Tables.Add(table);

   OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel,conn);
   myCommand.Fill(ds,table);
}

conn.Close();

你可能感兴趣的:(Excel)