DataTable importData = this.GetDataFromExcel(fileName); 为啥不可以用
DataTable importData = this.GetDataFromExcel(fileName); 为什么不可以用
this.GetDataFromExcel() 不可以用 为什么
我已经加上了reference了 using SYSTEM.REFLECTIONS; using Excel=Microsoft.office.Interop.Excel;using system.data.oledb;求教啊~~~~
------解决方案--------------------
this.GetDataFromExcel() 不可以用 为什么
我已经加上了reference了 using SYSTEM.REFLECTIONS; using Excel=Microsoft.office.Interop.Excel;using system.data.oledb;求教啊~~~~
------解决方案--------------------
- C# code
GetDataFromExcel() 这是个方法,,没写当然不能调用 /// <summary> /// 解析Excel,根据OleDbConnection直接连Excel /// </summary> /// <param name="filePath"></param> /// <param name="name"></param> /// <returns></returns> public static DataTable GetDataFromExcel(string filePath, string name) { try { string strConn; // strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=Excel 8.0"; strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\""; OleDbConnection OleConn = new OleDbConnection(strConn); OleConn.Open(); string sql = "SELECT * FROM [" + name + "$]";//可是更改Sheet名称,比如sheet2,等等 OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); DataSet OleDsExcle = new DataSet(); OleDaExcel.Fill(OleDsExcle, name); OleConn.Close(); return OleDsExcle.Table[0]; } catch (Exception err) { MessageBox.Show("数据绑定Excel失败! 失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } }