从Excel返回DataSet
这是个很简单的技术点,有人问起,偶就发一下:
public DataSet GetExcel(string Path)
{
//DirectoryInfo parsedirect=new System.IO.DirectoryInfo(Path);
//FileInfo[] xlsfile=parsedirect.GetFiles("*.xls");
OleDbConnection objConn=null;
DataSet data=new DataSet();
try
{
string strConn= "Provider=Microsoft.Jet.OleDb.4.0;" + "data source="+Path+ ";Extended Properties=Excel 8.0;";
objConn=new OleDbConnection(strConn);
objConn.Open();
DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null);
string tableName=schemaTable.Rows[0][2].ToString().Trim();
string strSql= "Select * From ["+tableName+"]";
OleDbCommand objCmd=new OleDbCommand(strSql, objConn);
OleDbDataAdapter sqlada=new OleDbDataAdapter();
sqlada.SelectCommand=objCmd;
sqlada.Fill(data,"BuyPrice");
objConn.Close();
}
catch
{
objConn.Close();
throw;
}
return data;
}
public DataSet GetExcel(string Path)
{
//DirectoryInfo parsedirect=new System.IO.DirectoryInfo(Path);
//FileInfo[] xlsfile=parsedirect.GetFiles("*.xls");
OleDbConnection objConn=null;
DataSet data=new DataSet();
try
{
string strConn= "Provider=Microsoft.Jet.OleDb.4.0;" + "data source="+Path+ ";Extended Properties=Excel 8.0;";
objConn=new OleDbConnection(strConn);
objConn.Open();
DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null);
string tableName=schemaTable.Rows[0][2].ToString().Trim();
string strSql= "Select * From ["+tableName+"]";
OleDbCommand objCmd=new OleDbCommand(strSql, objConn);
OleDbDataAdapter sqlada=new OleDbDataAdapter();
sqlada.SelectCommand=objCmd;
sqlada.Fill(data,"BuyPrice");
objConn.Close();
}
catch
{
objConn.Close();
throw;
}
return data;
}
posted on 2004-08-24 08:33 HelloSnoopy 阅读(438) 评论(0) 编辑 收藏 举报