/// <summary> |
/// 连接Excel 读取Excel数据 并返回DataSet数据集合 |
/// </summary> |
/// <param name="filepath">Excel服务器路径</param> |
/// <returns></returns> |
public System.Data.DataSet ExcelSqlConnection( string filepath) |
{ |
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'" ; |
OleDbConnection ExcelConn = new OleDbConnection(strCon); |
try |
{ |
ExcelConn.Open(); |
DataTable dt=ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object [] { null , null , null , "TABLE" }); //获取工作表名称(excel里面的工作簿) |
string strCom = string .Format( "select * from [{0}]" , dt.Rows[0][ "Table_Name" ]); |
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn); |
DataSet ds = new DataSet(); |
myCommand.Fill(ds); |
ExcelConn.Close(); |
return ds; |
} |
catch |
{ |
ExcelConn.Close(); |
return null ; |
} |
} |