excel文件读取类

 /// <summary>
    /// EXCEL导入功能集合类
    /// 作者:Zuowenjun
    /// 日期:2016/1/15
    /// </summary>
    public sealed class Import
    {
        /// <summary>
        /// 由Excel导入DataTable
        /// </summary>
        /// <param name="excelFileStream">Excel文件流</param>
        /// <param name="sheetName">Excel工作表名称</param>
        /// <param name="headerRowIndex">Excel表头行索引</param>
        /// <param name="isCompatible">是否为兼容模式</param>
        /// <returns>DataTable</returns>
        public static DataTable ToDataTable(Stream excelFileStream, string sheetName, int headerRowIndex, bool isCompatible)
        {
            IWorkbook workbook = Common.CreateWorkbook(isCompatible, excelFileStream);
            ISheet sheet = null;
            int sheetIndex = -1;
            if (int.TryParse(sheetName, out sheetIndex))
            {
                sheet = workbook.GetSheetAt(sheetIndex);
            }
            else
            {
                sheet = workbook.GetSheet(sheetName);
            }


            DataTable table = Common.GetDataTableFromSheet(sheet, headerRowIndex);


            excelFileStream.Close();
            workbook = null;
            sheet = null;
            return table;
        }


        /// <summary>
        /// 由Excel导入DataTable
        /// </summary>
        /// <param name="excelFilePath">Excel文件路径,为物理路径,可传空值</param>
        /// <param name="sheetName">Excel工作表名称</param>
        /// <param name="headerRowIndex">Excel表头行索引</param>
        /// <returns>DataTable</returns>
        public static DataTable ToDataTable(string excelFilePath, string sheetName, int headerRowIndex)
        {
            if (string.IsNullOrEmpty(excelFilePath))
            {
                excelFilePath = Common.GetOpenFilePath();
            }


            if (string.IsNullOrEmpty(excelFilePath))
            {
                return null;
            }


            using (FileStream stream = System.IO.File.OpenRead(excelFilePath))
            {
                bool isCompatible = Common.GetIsCompatible(excelFilePath);
                return ToDataTable(stream, sheetName, headerRowIndex, isCompatible);
            }
        }


        #region 整治任务导入
        public static DataTable ToDataTableAgency(string excelFilePath, string sheetName, int headerRowIndex)
        {
            if (string.IsNullOrEmpty(excelFilePath))
            {
                excelFilePath = Common.GetOpenFilePath();
            }


            if (string.IsNullOrEmpty(excelFilePath))
            {
                return null;
            }


            using (FileStream stream = System.IO.File.OpenRead(excelFilePath))
            {
                bool isCompatible = Common.GetIsCompatible(excelFilePath);
                return ToDataTableAgency(stream, sheetName, headerRowIndex, isCompatible);
            }
        }
        public static DataTable ToDataTableAgency(Stream excelFileStream, string sheetName, int headerRowIndex, bool isCompatible)
        {
            IWorkbook workbook = Common.CreateWorkbook(isCompatible, excelFileStream);
            ISheet sheet = null;
            int sheetIndex = -1;
            DataTable table = null;
            int num = workbook.NumberOfSheets;
            if (num > 2)
            {
                for (int i = 2; i <= num - 2; i++)
                {
                    sheet = workbook.GetSheetAt(i);
                    if (i == 2)
                    {
                        table = Common.GetDataTableFromSheet(sheet, headerRowIndex);
                    }
                    else
                    {
                        if (table != null)
                        {
                            DataTable tv = Common.GetDataTableFromSheet(sheet, headerRowIndex);
                            table.Merge(tv);
                        }
                    }
                }
            } 
            excelFileStream.Close();
            workbook = null;
            sheet = null;
            return table;
        }
        #endregion  
        /// <summary>
        /// 由Excel导入DataSet,如果有多个工作表,则导入多个DataTable
        /// </summary>
        /// <param name="excelFileStream">Excel文件流</param>
        /// <param name="headerRowIndex">Excel表头行索引</param>
        /// <param name="isCompatible">是否为兼容模式</param>
        /// <returns>DataSet</returns>
        public static DataSet ToDataSet(Stream excelFileStream, int headerRowIndex, bool isCompatible)
        {
            DataSet ds = new DataSet();
            IWorkbook workbook = Common.CreateWorkbook(isCompatible, excelFileStream);
            for (int i = 0; i < workbook.NumberOfSheets; i++)
            {
                ISheet sheet = workbook.GetSheetAt(i);
                DataTable table = Common.GetDataTableFromSheet(sheet, headerRowIndex);
                ds.Tables.Add(table);
            }


            excelFileStream.Close();
            workbook = null;


            return ds;
        }


        /// <summary>
        /// 由Excel导入DataSet,如果有多个工作表,则导入多个DataTable
        /// </summary>
        /// <param name="excelFilePath">Excel文件路径,为物理路径。可传空值</param>
        /// <param name="headerRowIndex">Excel表头行索引</param>
        /// <returns>DataSet</returns>
        public static DataSet ToDataSet(string excelFilePath, int headerRowIndex)
        {
            if (string.IsNullOrEmpty(excelFilePath))
            {
                excelFilePath = Common.GetOpenFilePath();
            }


            if (string.IsNullOrEmpty(excelFilePath))
            {
                return null;
            }


            using (FileStream stream = System.IO.File.OpenRead(excelFilePath))
            {
                bool isCompatible = Common.GetIsCompatible(excelFilePath);
                return ToDataSet(stream, headerRowIndex, isCompatible);
            }
        }


        public static List<string> GetNumberOfSheets(string excelFilePath)
        {
            using (FileStream stream = System.IO.File.OpenRead(excelFilePath))
            {
                List<string> listName = new List<string>();
                IWorkbook workbook = Common.CreateWorkbook(false, stream);
                int count = workbook.NumberOfSheets;
                for (int i = 0; i < count; i++)
                {
                    listName.Add(workbook.GetSheetName(i));
            
                }
                return listName;
            }
        }


        /// <summary>
        /// 处理映射关系
        /// </summary>
        public static Dictionary<string, string> LoadMapping(string mappingXML)
        {
            Dictionary<string, string> mappingDic = new Dictionary<string, string>();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(mappingXML);
            XmlNodeList list = xmldoc.SelectNodes("//Field");
            for (int i = 0; i < list.Count; i++)
            {
                XmlNode node = list[i];
                string excelCoumn = node.Attributes["excelCoumn"].Value;
                string column = node.Attributes["column"].Value;
                mappingDic.Add(excelCoumn, column);
            }
            return mappingDic;
        }




    }
功能描述 clsWindow是VB6环境下使用的一个操作外部程序窗口的,比如得到窗口句柄,得到窗口里某个文本框的内容。非常方便,使用它可以让您脱身于一堆api函数,功能强大使用简单! 这个楼主很早就开始封装了,原本打算做成似DOM对象那样,通过一堆getElmentByXXX等方法实现对桌面程序下各个窗口以及里面各个控件对象的自由访问,但是具体要做的工作太多,目前只实现了一部分,期待大家一起加入更新维护。 目前该封装了绝大部分对windows窗口的常用操作,例如:获取窗口句柄,设置窗口为活动窗口,设置窗口内文本框内容,点击窗口内的某些按钮等。 这个现在还在一直不断地扩充,功能已经很强大很广泛,使用它可以轻而易举地设置窗口标题栏文字,移动窗体等等。以前要实现这些操作常常需要一大堆api函数,现在只需要一点点代码就可以了,完全让您脱身于api函数的海洋。当然您需要研究每个方法实现原理的话可以看一看源代码。 使用范例(请在v1.9以上测试): 1)关闭腾讯新闻窗口“腾讯网迷你版”。 Dim window As New clsWindow If window.GetWindowByTitle("腾讯网迷你版").hWnd > 0 Then window.CloseWindow '关闭窗口 End If 以上是不是很简洁呢? 20150715更新追加: 最新1.9版本更简洁,一句话解决: w.GetWindowByTitle("腾讯网迷你版").CloseWindow 小伙伴,是不是简洁爆了呢?:) 为了防止程序找不到窗口而一直等待可以改成: w.GetWindowByTitle("腾讯网迷你版",1).CloseWindow (意思为超时等待1秒。默认会耐心等60秒,除非你确定窗口一定有,然后就用上面的。) 2)获取某个打开的记事本里面的内容。假设记事本标题为“测试要求.txt - 记事本”,通过SPY等工具查看得知记事本的文本框名为:Edit,那么我们编写程序如下: Dim window As New clsWindow If window.GetWindowByTitle("测试要求.txt - 记事本").hWnd > 0 Then MsgBox window.GetElementTextByClassName("Edit") End If 这个看起来也很简单,方法自由还可以使用正则匹配,可以写成下面这样: Dim window As New clsWindow If window.GetWindowByTitleEx("工作任务\.txt.*?", , , True).hWnd > 0 Then MsgBox window.GetElementTextByClassName("Edi", , True) '第三个参数表示是否使用正则,默认为false End If 获取标题那边如果觉得要把标题写完整太麻烦,可以将GetWindowByTitle该车GetWindowByTitleEx然后后面只要写关键字就行啦。看招: Dim window As New clsWindow If window.GetWindowByTitleEx("工作任务").hWnd > 0 Then MsgBox window.GetElementTextByClassName("Edit") End If clsWindow最新版下载请关注博客: http://blog.csdn.net/sysdzw/article/details/9083313 '============================================================================================== '名 称:windows窗体控制v2.0 '描 述:一个操作windows窗口的,可对窗口进行很多常用的操作(名为clsWindow) '使用范例:Dim window As New clsWindow ' window.GetWindowByTitle "计算器" ' window.closeWindow '编 程:sysdzw 原创开发,如果有需要对模块扩充或更新的话请邮箱发我一份,共同维护 '发布日期:2013/06/01 '博 客:http://blog.163.com/sysdzw ' http://blog.csdn.net/sysdzw 'Email :sysdzw@163.com 'QQ :171977759 '版
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值