C#用Oledb查询Excel某个sheet中的数据——按多个条件查询
来源:互联网 发布:linux chgrp 单词 编辑:程序博客网 时间:2024/04/19 15:24
最近在研究如何操作Excel文件,目前主要研究oledb方法和利用Office 的Excel组件来操作Excel文件
今天发表利用oledb的方式,根据多个条件进行数据的查询。
明天争取做个例子,利用Office 的Excel组件来操作Excel文件
1.Excel文件第一行设置为列名:
/// <summary> /// 获取OLEDB连接对象。 /// </summary> /// <param name="excelPath">Excel文件的路径</param>/// <returns>OLEDB连接对象</returns> public static OleDbConnection GetOledbConnection(string excelPath){ // 这里的HDR我设置为了Yes,也就是说我Excel中的第一行会当做列名,而不是数据 string conString=string.Format("Provider=Microsoft.Ace.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'",excelPath); return new OleDbConnection( conString=string.Format );}/// <summary> /// 按条件查询sheet中的信息 /// </summary> /// <param name="excelPath">Excel文件的路径</param>/// <param name="sheetName">sheet名称</param>/// <returns>DataTable</returns> public DataTable GetSheetInfoByCondition(string excelPath,string sheetName){ OleDbConnection con=null; try { con = GetOledbConnection(excelPath); con.Open(); OleDbCommand cmd = con.CreateCommand(); // 这里查询的条件我写死了,当然你可以动态的传值进来进行查询 cmd.CommandText = string.Format("select * from [{0}$] where 姓名='张三' and 年龄='25'",sheetName); OleDbDataAdapter adapter = new OleDbDataAdapter ( cmd ); DataSet ds = new DataSet (); adapter.Fill(ds,sheetName); return ds.Tables[0]; } catch(Exception ex) { throw ex; } finally { if(con!=null) { con.Close(); } } return null;}
2.Excel文件第一列不是列名:
/// <summary> /// 获取OLEDB连接对象。 /// </summary> /// <param name="excelPath">Excel文件的路径</param>/// <returns>OLEDB连接对象</returns> public static OleDbConnection GetOledbConnection(string excelPath){ // 这里的HDR我设置为了No,也就是说我Excel中没有列名,每一列的输入的值都是数据 string conString=string.Format("Provider=Microsoft.Ace.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=No;IMEX=1;'",excelPath); return new OleDbConnection( conString=string.Format );}/// <summary> /// 按条件查询sheet中的信息 /// </summary> /// <param name="excelPath">Excel文件的路径</param>/// <param name="sheetName">sheet名称</param>/// <returns>DataTable</returns> public DataTable GetSheetInfoByCondition(string excelPath,string sheetName){ OleDbConnection con=null; try { con = GetOledbConnection(excelPath); con.Open(); OleDbCommand cmd = con.CreateCommand(); // 这里查询的条件我写死了,当然你可以动态的传值进来进行查询 // 如果你在连接字符串里没有设置列名,oledb会自动生成列名,列名分别为F1,F2,F3,F4......... cmd.CommandText = string.Format("select * from [{0}$] where F1='张三' and F2='25'",sheetName); OleDbDataAdapter adapter = new OleDbDataAdapter ( cmd ); DataSet ds = new DataSet (); adapter.Fill(ds,sheetName); return ds.Tables[0]; } catch(Exception ex) { throw ex; } finally { if(con!=null) { con.Close(); } } return null;}以上两种oledb连接字符串,我使用的都是Excel2007版本,对于不同版本的连接字符串,请参照下面连接里的详细说明:
http://blog.csdn.net/sdd379733766/article/details/9838453
- C#用Oledb查询Excel某个sheet中的数据——按多个条件查询
- EXCEL-VBA:通过SQL查询数据Sheet中的数据
- C#用Odbc、Oledb查询Excel和CSV
- EXCEL OLEDB 查询
- 使用C#查询excel文件中的数据
- 用OleDb读取Excel中的数据
- 空间数据查询——根据属性条件查询对象
- 空间数据查询——根据几何条件查询对象
- oledb 读取excel 不知名sheet
- c#用OleDb读写excel
- 6.数据查询(条件
- springmvc 有查询条件的数据 下载到excel
- C# OLEDB方式读取Excel文件数据
- 用OleDb读取Excel数据
- Excel VBA 多条件查询
- 用OLEDB的GetOleDbSchemaTable方法获取EXCEL的Sheet列表
- C# 如何将 不同的数据(多个查询结果集 dataset)导出到一个excel的不同sheet中
- OleDb和ADO.NET读取文本文件时进行条件查询
- unity3D之克隆游戏对象
- 使用Ajax发送Struts2请求返回JSON数据格式
- SICP学习 - 序
- 数据结构multiset hdu-2275-Kiki & Little Kiki 1
- hdu1527-威佐夫博弈(Wythoff Game)
- C#用Oledb查询Excel某个sheet中的数据——按多个条件查询
- Linux开机启动服务设置
- IT学生解惑真经
- redhat5安装jdk6、eclipse和tomcat6
- 按位运算符(>> << ~ & ^ | )及实例
- 自学java总结----equals
- jquery fullCalendar 显示时出现12a的解决方案
- Python 字典
- Effective C++读书笔记(三) 资源管理