日期:2014-05-17 浏览次数:21013 次
private static DataSet ExcelToDataSet(string excelFile, params string[] excelSheets)
{
DataSet ds = new DataSet();
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties=\"Excel 12.0;HDR=YES\";";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
if (excelSheets == null) excelSheets = new string[1] { "Sheet1" };
foreach (string sheet in excelSheets)
{
string strExcel = string.Format("select * from [{0}$]", string.IsNullOrEmpty(sheet) ? "Sheet1" : sheet);
using (OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn))
{
myCommand.Fill(ds, sheet);
}
}
}
return ds;
}