public DataSet ExcelToDS(string Path, string fileName)
{
string strConn = "";
// strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + "; Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1;\"";
// strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + Path + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此连接只能操作Excel2007之前(.xls)文件
//0 ---输出模式;1---输入模式;2----链接模式(完全更新能力)
strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'"; //此连接可以操作.xls与.xlsx文件
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strExcel = "";
strExcel = "select * from [" + fileName + "$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet();
ds.Clear();
myCommand.Fill(ds, "[" + fileName + "$]");
return ds;
}
catch (Exception e)
{
Response.Write("读取数据源出错!请确保版本及其工作表名正确!");
return null;
}
finally
{
conn.Close();
}
}