百度谷歌一番發(fā)現(xiàn),其實(shí)只要安裝Microsoft Access 2010 數(shù)據(jù)庫引擎可再發(fā)行程序包即可實(shí)現(xiàn)2010 Microsoft Office System 文件與非 Microsoft Office 應(yīng)用程序之間傳輸數(shù)據(jù),支持現(xiàn)有的 Microsoft Office 文件(例如 Microsoft Office Access 2010(*.mdb 和 *.accdb)文件和 Microsoft Office Excel 2010(*.xls、*.xlsx 和 *.xlsb)文件)與其他數(shù)據(jù)源(例如 Microsoft SQL Server)之間傳輸數(shù)據(jù)。還支持與現(xiàn)有文本文件建立連接。 此外,還會(huì)安裝 ODBC 和 OLEDB 驅(qū)動(dòng)程序,供應(yīng)用程序開發(fā)人員在開發(fā)與 Office 文件格式連接的應(yīng)用程序時(shí)使用。該安裝包下載地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=13255 。
裝上這個(gè)包后,一段代碼就搞定:
代碼如下:
/// <summary>
/// 連接Excel 讀取Excel數(shù)據(jù) 并返回DataSet數(shù)據(jù)集合
/// </summary>
/// <param name="filepath">Excel服務(wù)器路徑</param>
/// <param name="tableName">Excel表名稱</param>
/// <returns></returns>
public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)
{
//string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
string strCon = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
OleDbConnection ExcelConn = new OleDbConnection(strCon);
try
{
string strCom = string.Format("SELECT * FROM [Sheet1$]");
ExcelConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[" + tableName + "$]");
ExcelConn.Close();
return ds;
}
catch
{
ExcelConn.Close();
return null;
}
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com