public static DataTable GetDataFromExcelByConn()
{
var filePath = "D:\\1.xlsx";
string fileType = System.IO.Path.GetExtension(filePath);
if (string.IsNullOrEmpty(fileType)) return null;
using (DataSet ds = new DataSet())
{
//string strFormat = "Provider=Microsoft.JET.OLEDB.{0}.0;Extended Properties=\"Excel {1}.0;HDR=Yes;IMEX=1;\";data source={2};";
string strFormat = "Provider=Microsoft.ACE.OLEDB.{0}.0;Extended Properties=\"Excel {1}.0;HDR=Yes;IMEX=1;\";data source={2};";
string strCon = string.Format(strFormat, fileType == ".xls" ? 4 : 12, fileType == ".xls" ? 8 : 12, filePath);
string strCom = " SELECT * FROM [Sheet1$]";
using (OleDbConnection myConn = new OleDbConnection(strCon))
{
using (OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn))
{
myConn.Open();
myCommand.Fill(ds);
}
}
if (ds == null || ds.Tables.Count <= 0) return null;
return ds.Tables[0];
}
这里面要区别的是 Microsoft.JET.OLEDB和Microsoft.ACE.OLEDB 的区别
有关内容 下载 ,请到官网上下载 相关安装包 http://www.microsoft.com/en-us/download/details.aspx?id=13255