如出现此错误信息:未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序 。
请下载http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabaseEngine.exe 安装
01
|
public static DataSet ReadExcel_ds(string FilePath, string companycd)
|
07
|
string subfile = FilePath.Substring(FilePath.LastIndexOf(".") + 1);
|
09
|
DataSet ds = new DataSet();
|
13
|
if (subfile.ToUpper() == "XLS")
|
17
|
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + FilePath;
|
23
|
if (subfile.ToUpper() == "XLSX")//excel2007读取
|
27
|
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
|
35
|
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
|
37
|
//string strCom = "SELECT * FROM [Sheet1$]";
|
41
|
System.Data.DataTable sTable = Conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);//新添加 2010-10-14
|
43
|
string tableName = sTable.Rows[0][2].ToString().Trim();//新添加 2010-10-14 获取工作表名称
|
45
|
string strCom = "SELECT * FROM [" + tableName + "]";
|
47
|
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
|
51
|
myCommand.Fill(ds, "[" + tableName + "]");
|