把Excel作为数据库,读到DataTable中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace Framework.Common
{
public class CommonClass
{
public static DataSet ExecleDs(string filenameurl, string table)
{

DataSet ds = new DataSet();

string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filenameurl + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();

string tableName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0][2].ToString().Trim();

OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + tableName + "]", conn);

odda.Fill(ds, table);
}

return ds;

}
}
}

你可能感兴趣的:(把Excel作为数据库,读到DataTable中)