using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
using System.Data.OleDb;
namespace SR.Excle
{
public class SR_ExcleHelper
{
#region 属性
private static string m_excleconnectStr;
///
/// 连接excle的连接字符串
///
public static string ExcleConnectStr
{
get { return SR_ExcleHelper.m_excleconnectStr; }
set { SR_ExcleHelper.m_excleconnectStr = value; }
}
#endregion
#region 获得连接字符串
///
/// 获得连接excle的连接字符串
///
/// excle文件的路径
///
连接excle的字符串
public static void GetConnStr(string exclePath)
{
try
{
string excleType = System.IO.Path.GetExtension(exclePath);
if (string.IsNullOrEmpty(exclePath))
{
return;
}
if (excleType == ".xls") //低版本
{
ExcleConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + exclePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
}
if (excleType == ".xlsx") // 高版本
{
ExcleConnectStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + exclePath + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
}
else
{
return;
}
}
catch (Exception)
{
return;
}
}
#endregion
#region 建立excel连接对象
///
/// 获得连接对象
///
/// excle路径
///
连接对象conn
public static OleDbConnection GetConn(string exclePath)
{
GetConnStr(exclePath);
OleDbConnection conn = null;
try
{
conn = new OleDbConnection(ExcleConnectStr);
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
}
catch (Exception)
{
return null;
}
return conn;
}
#endregion
#region 读取excle得到DataSet
///
/// 得到excle的所有sheet,一个sheet是一个DataTable,一个excle形成一个DataSet
///
/// excle路径
///
DataSet
public static DataSet GetDataSet(string exclePath)
{
string sql = "";
OleDbConnection conn = null;
OleDbDataAdapter dtadapter = null;
DataSet dtset = new DataSet();
try
{
using (conn = GetConn(exclePath))
{
// OleDbDataAdapter 充当 DataSet 和数据源之间的桥梁,用于检索和保存数据。OleDbDataAdapter 通过以下方法提供这个桥接器:使用 Fill 将数据从数据源加载到 DataSet 中,并使用 Update 将 DataSet 中所作的更改发回数据源。
dtadapter = new OleDbDataAdapter();
DataTable schemaTables = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); //获取Excel中所有Sheet表的信息
if (schemaTables.Rows.Count > 0)
{
string[] tablesNames = new string[schemaTables.Rows.Count];
for (int i = 0; i < schemaTables.Rows.Count; i++)
{
tablesNames[i] = schemaTables.Rows[i][2].ToString().Trim(); //得到所有sheet的名字的集合
sql = "select * from [" + tablesNames[i] + "]";
dtadapter.SelectCommand = new OleDbCommand(sql, conn);
DataSet dtItem = new DataSet();
dtadapter.Fill(dtItem, tablesNames[i]); // 在 Fill 过程中使用的 OleDbCommand,用于从数据源中选择要放在 DataSet 中的记录。
dtset.Tables.Add(dtItem.Tables[0].Copy());
}
}
} // using
} //try
catch (Exception ex)
{
return null;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
dtadapter.Dispose();
}
}
return dtset;
}
#endregion
public static DataTable GetDataTable(string exclePath, string tableName)
{
DataSet dt = GetDataSet(exclePath);
if (dt.Tables.Count < 1)
{
return null;
}
for (int i = 0; i < dt.Tables.Count; i++)
{
if (dt.Tables[i].TableName == tableName+"$")
{
return dt.Tables[i];
}
}
return null;
}
}
}
C#与Excle
最新推荐文章于 2024-06-07 16:19:41 发布