国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# Excel 交互類

2019-11-17 04:07:59
字體:
來源:轉載
供稿:網友
view plaincopy to clipboardPRint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
using System;   
using System.Collections.Generic;   
using System.Text;   
using System.Data.OleDb;   
using System.Data;   
  
namespace MyExcel   
{   
    public class ExcelConnector   
    {   
        string connString;   
        public ExcelService(string path)   
        {   
            connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";   
        }   
  
        private OleDbConnection conn;   
        public OleDbConnection Connection   
        {   
            get  
            {   
  
                if (conn == null)   
                {   
                    conn = new OleDbConnection(connString);   
                    conn.Open();   
                }   
                else if (conn.State == ConnectionState.Broken)   
                {   
                    conn.Close();   
                    conn.Open();   
                }   
                else if (conn.State == ConnectionState.Closed)   
                {   
                    conn = new OleDbConnection(connString);   
                    conn.Open();   
                }   
                return conn;   
            }   
  
        }   
        /// <summary>   
        /// 獲取Excel 中的工作表   
        /// </summary>   
        /// <returns></returns>   
        public List<string> GetDataFromExcelWithAppointSheetName()   
        {   
            DataTable dtSheetName = null;   
            try  
            {   
                dtSheetName = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });   
                List<string> strTableNames = new List<string>();   
                for (int k = 0; k < dtSheetName.Rows.Count; k++)   
                {   
                    string s = dtSheetName.Rows[k]["TABLE_NAME"].ToString();   
                    Console.WriteLine(s);   
                    //過濾一下沒用的表,Excel 默認生成的隱藏文件   
                    if (!s.Contains("_FilterDatabase") && s.LastIndexOf('_') + 1 != s.Length)   
                    {   
                        strTableNames.Add(s);   
                    }   
                }   
                return strTableNames;   
            }   
            catch (Exception)   
            {   
                return null;   
            }   
            finally  
            {   
                Connection.Dispose();   
            }   
        }   
        /// <summary>   
        /// 通過工作表名 獲取數據   
        /// </summary>   
        /// <param name="name"></param>   
        /// <returns></returns>   
        public DataTable GetContentBySheetName(string name)   
        {   
            DataTable dt = new DataTable();   
            OleDbDataAdapter myCommand = null;   
            string strExcel = "select * from [" + name + "]";   
            try  
            {   
                myCommand = new OleDbDataAdapter(strExcel, Connection);   
                dt = new DataTable();   
                myCommand.Fill(dt);   
                return dt;   
            }   
            catch (Exception)   
            {   
                return null;   
            }   
            finally  
            {   
                myCommand.Dispose();   
                Connection.Dispose();   
  
  
            }   
  
        }   
  
  
  
    }   
}  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黎川县| 碌曲县| 金平| 盱眙县| 班戈县| 石柱| 北海市| 乡城县| 湖北省| 安阳县| 吐鲁番市| 安图县| 沈丘县| 萨嘎县| 和龙市| 蓬安县| 冀州市| 汉沽区| 拜泉县| 本溪市| 宜城市| 思茅市| 工布江达县| 威远县| 大余县| 嫩江县| 临江市| 历史| 周口市| 定安县| 南涧| 安图县| 利辛县| 城口县| 县级市| 临湘市| 北安市| 禄丰县| 正蓝旗| 施甸县| 阳高县|