C#与Excle

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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测量员遇上程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值