public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Path+";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [Sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
try
{
myCommand.Fill(ds, "table");
}
catch (Exception ex)
{
strExcel = ex.ToString();
}
conn.Close();
return ds;
{
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Path+";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [Sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
try
{
myCommand.Fill(ds, "table");
}
catch (Exception ex)
{
strExcel = ex.ToString();
}
conn.Close();
return ds;
}
我用的是office2010版本,所以连接字符串用: "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ Path+";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
如果是office10以下的,应该用这个:Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Path+";Persist Security Info=False
PS:Path为文件路径。