读取Excel文件内容
/// <summary>
/// 读取Excel方法,HDR=Yes表示从被读入的Excel中第二行开始进行数据读取
/// IMEX=1表示所有数据当作文本处理
/// </summary>
/// <param name="Path">Excel文件路径</param>
/// <returns>读取出的DataSet</returns>
private DataSet GetExcel(string Path)
{
OleDbConnection objConn=null;
DataSet data=new DataSet();
try
{
string strConn= "Provider=Microsoft.Jet.OleDb.4.0;" + "data source="+Path+ ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
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,"indata");
objConn.Close();
}
catch
{
objConn.Close();
throw;
}
return data;
}
/// 读取Excel方法,HDR=Yes表示从被读入的Excel中第二行开始进行数据读取
/// IMEX=1表示所有数据当作文本处理
/// </summary>
/// <param name="Path">Excel文件路径</param>
/// <returns>读取出的DataSet</returns>
private DataSet GetExcel(string Path)
{
OleDbConnection objConn=null;
DataSet data=new DataSet();
try
{
string strConn= "Provider=Microsoft.Jet.OleDb.4.0;" + "data source="+Path+ ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
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,"indata");
objConn.Close();
}
catch
{
objConn.Close();
throw;
}
return data;
}