麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[WinForm]項(xiàng)目開(kāi)發(fā)中Excel使用小計(jì)

2019-11-14 16:45:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

實(shí)際項(xiàng)目開(kāi)發(fā)中,所涉及的Excel會(huì)比較復(fù)雜,而且列中會(huì)帶有一些計(jì)算公式,這給讀取帶來(lái)困難,嘗試過(guò)一些免費(fèi)的第三方dll,譬如Myxls,NPOI,IExcelDataReader都會(huì)出現(xiàn)一些問(wèn)題,最后采用OLEDB形式讀取,再x64操作系統(tǒng)上有點(diǎn)問(wèn)題,不過(guò)采用小技巧即可解決,參考鏈接:Connecting to Excel and access Files using .Net on a 64-bit Server

封裝代碼:

namespace DBUtilHelpV2{    public class OLEDBExcelToolV2    {        static readonly string xls = ".xls";        static readonly string xlsx = ".xlsx";        string _ExcelExtension = string.Empty;//后綴        string _ExcelPath = string.Empty;//路徑        string _ExcelConnectString = string.Empty;//鏈接字符串        static bool _X64Version = false;//是否強(qiáng)制使用x64鏈接字符串,即xlsx形式        public OLEDBExcelToolV2(string excelPath, bool x64Version)        {            if (string.IsNullOrEmpty(excelPath))                throw new ArgumentNullException("excelPath");            if (!File.Exists(excelPath))                throw new ArgumentException("excelPath");            string _excelExtension = Path.GetExtension(excelPath);            _ExcelExtension = _excelExtension.ToLower();            _ExcelPath = excelPath;            _X64Version = x64Version;            _ExcelConnectString = BuilderConnectionString();        }        /// <summary>        /// 創(chuàng)建鏈接字符串        /// </summary>        /// <returns></returns>        PRivate string BuilderConnectionString()        {            Dictionary<string, string> _connectionParameter = new Dictionary<string, string>();            if (!_ExcelExtension.Equals(xlsx) && !_ExcelExtension.Equals(xls))            {                throw new ArgumentException("excelPath");            }            if (!_X64Version)            {                if (_ExcelExtension.Equals(xlsx))                {                    // XLSX - Excel 2007, 2010, 2012, 2013                    _connectionParameter["Provider"] = "Microsoft.ACE.OLEDB.12.0;";                    _connectionParameter["Extended Properties"] = "'Excel 12.0 xml;IMEX=1'";                }                else if (_ExcelExtension.Equals(xls))                {                    // XLS - Excel 2003 and Older                    _connectionParameter["Provider"] = "Microsoft.Jet.OLEDB.4.0";                    _connectionParameter["Extended Properties"] = "'Excel 8.0;IMEX=1'";                }            }            else            {                _connectionParameter["Provider"] = "Microsoft.ACE.OLEDB.12.0;";                _connectionParameter["Extended Properties"] = "'Excel 12.0 XML;IMEX=1'";            }            _connectionParameter["Data Source"] = _ExcelPath;            StringBuilder _connectionString = new StringBuilder();            foreach (KeyValuePair<string, string> parameter in _connectionParameter)            {                _connectionString.Append(parameter.Key);                _connectionString.Append('=');                _connectionString.Append(parameter.Value);                _connectionString.Append(';');            }            return _connectionString.ToString();        }        /// <summary>        /// Excel操作        /// DELETE不支持        /// </summary>        /// <param name="sql"></param>        /// <returns></returns>        public int ExecuteNonQuery(string sql)        {            int _affectedRows = -1;            using (OleDbConnection sqlcon = new OleDbConnection(_ExcelConnectString))            {                try                {                    sqlcon.Open();                    using (OleDbCommand sqlcmd = new OleDbCommand(sql, sqlcon))                    {                        _affectedRows = sqlcmd.ExecuteNonQuery();                    }                }                catch (Exception)                {                    return -1;                }            }            return _affectedRows;        }        /// <summary>        /// Excel操作        ///獲取EXCEL內(nèi)sheet集合        /// </summary>        /// <param name="sql"></param>        /// <returns></returns>        public string[] GetExcelSheetNames()        {            DataTable _schemaTable = null;            using (OleDbConnection sqlcon = new OleDbConnection(_ExcelConnectString))            {                try                {                    sqlcon.Open();                    _schemaTable = sqlcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);                    String[] _excelSheets = new String[_schemaTable.Rows.Count];                    int i = 0;                    foreach (DataRow row in _schemaTable.Rows)                    {                        _excelSheets[i] = row["TABLE_NAME"].ToString().Trim();                        i++;                    }                    return _excelSheets;                }                catch (Exception)                {                    return null;                }                finally                {                    if (_schemaTable != null)                    {                        _schemaTable.Dispose();                    }                }            }        }        /// <summary>        /// 讀取sheet        /// eg:select * from [Sheet1$]        /// </summary>        /// <param name="sql"></param>        /// <returns></returns>        public DataTable ExecuteDataTable(string sql)        {            using (OleDbConnection sqlcon = new OleDbConnection(_ExcelConnectString))            {                try                {                    using (OleDbCommand sqlcmd = new OleDbCommand(sql, sqlcon))                    {                        using (OleDbDataAdapter sqldap = new OleDbDataAdapter(sqlcmd))                        {                            DataTable _dtResult = new DataTable();                            sqldap.Fill(_dtResult);                            return _dtResult;                        }                    }                }                catch (Exception)                {                    return null;                }            }        }        /// <summary>        /// 獲取excel所有sheet數(shù)據(jù)        /// </summary>        /// <returns>DataSet</returns>        public DataSet ExecuteDataSet()        {            DataSet _excelDb = null;            using (OleDbConnection sqlcon = new OleDbConnection(_ExcelConnectString))            {                try                {                    sqlcon.Open();                    DataTable _schemaTable = sqlcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);                    if (_schemaTable != null)                    {                        int i = 0;                        _excelDb = new DataSet();                        foreach (DataRow row in _schemaTable.Rows)                        {                            string _sheetName = row["TABLE_NAME"].ToString().Trim();                            string _sql = string.Format("select * from [{0}]", _sheetName);                            using (OleDbCommand sqlcmd = new OleDbCommand(_sql, sqlcon))                            {                                using (OleDbDataAdapter sqldap = new OleDbDataAdapter(sqlcmd))                                {                                    DataTable _dtResult = new DataTable();                                    _dtResult.TableName = _sheetName;                                    sqldap.Fill(_dtResult);                                    _excelDb.Tables.Add(_dtResult);                                }                            }                            i++;                        }                    }                }                catch (Exception)                {                    return null;                }            }            return _excelDb;        }    }}
代碼使用
        /// <summary>        /// 合并EXCEL數(shù)據(jù)        /// </summary>        /// <param name="_excelPath">excel路徑</param>        private void HandleMergeExcel(string _excelPath)        {            if (!string.IsNullOrEmpty(_excelPath))            {                OLEDBExcelToolV2 _excelHelper = new OLEDBExcelToolV2(_excelPath, true);                DataSet _excelSource = _excelHelper.ExecuteDataSet();                HandleExcelSource(_excelSource);            }        }

若在x64操作系統(tǒng),將第二個(gè)參數(shù)設(shè)置true,并且按照AccessDatabaseEngine_X64.exe即可正常讀取

代碼效果

無(wú)標(biāo)題


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 成人午夜在线免费 | 久久久久久中文字幕 | 免费观看一区二区三区 | www.三区| 欧美××××黑人××性爽 | 亚洲人成网站免费播放 | 精国产品一区二区三区 | 黄色大片网 | 久久密 | 久草在线手机视频 | 国产成人免费精品 | 久久久电影电视剧免费看 | 国产精品99久久久久久久 | 亚洲午夜免费电影 | 精品国产一区二区三区久久久蜜月 | 久久精品免费国产 | 成人一级毛片 | 国产毛片毛片 | 在线播放免费人成毛片乱码 | 热re91久久精品国产99热 | 国产精品成人一区二区三区电影毛片 | 舌头伸进添的我好爽高潮网站 | 国产1区2| 亚洲精品久久久久www | 精品欧美一区二区精品久久 | 国产精品久久久久久久午夜片 | 欧美精品久久久久久久久老牛影院 | 激情小说激情图片激情电影 | 天天色人人爱 | 久久久久国产成人免费精品免费 | 激情小说另类 | 中日韩免费视频 | 国产精品视频专区 | 欧美中文日韩 | 日韩在线毛片 | 久久久久九九九女人毛片 | 欧美一级在线免费 | 爽爽视频免费看 | 亚洲精久| 麻豆小视频在线观看 | 青草久久网 |