C#读取Excel2007的文件

转载自:http://blog.csdn.net/zhangnan20100811/article/details/6458158

以读取access数据集的方式读取

For excel 2007:

private DataSet GetExcelData(string str)
{
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str + ";Extended Properties=/"Excel 12.0;HDR=YES/"";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close();
return myDataSet;
}

For excel2003:

连接字符串改为:

string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+str+";Extended Properties=Excel 8.0";

原文地址:https://www.cnblogs.com/EggKiller/p/2879146.html