当前位置: 移动技术网 > IT编程>开发语言>c# > C#使用oledb操作excel文件的方法

C#使用oledb操作excel文件的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#使用oledb操作excel文件的方法。分享给大家供大家参考。具体分析如下:

不管什么编程语言都会提供操作excel文件的方式,c#操作excel主要有以下几种方式:

1.excel

说明:利用office 的excel组件来操作excel文件
优点:能够完全操作excel文件,生成丰富文件内容
缺点:需要电脑安装excel,会启动excel进程这在web上很不方便

2.openxml

说明:一个操作字处理文档的组件包括excel
优点:能够操作操作excel2007版本文件
缺点:只能够操作excel2007文件

3.npoi

说明:一个开源的excel读写库
优点:不需要安装excel
缺点:只能够操作excel2003文档,对文档内容控制不完全

4.oledb

说明:使用microsoft jet 提供程序用于连接到 excel 工作簿,将excel文件作为数据源来读写
优点:简单快速,能够操作高版本excel
缺点:只能够进行有限的操作(读、写)

今天学习使用oledb操作excel文件

连接字符串:provider=microsoft.jet.oledb.4.0;data source=d:\test.xls;extended properties='excel 8.0;hdr=yes;imex=1;'
provider:表示提供程序名称
data source:这里填写excel文件的路径
extended properties:设置excel的特殊属性
extended properties 取值:
excel 8.0 针对excel2000及以上版本,excel5.0 针对excel97。
hdr=yes 表示第一行包含列名,在计算行数时就不包含第一行
imex 0:导入模式,1:导出模式:2混合模式

1.读取excel文件

if (openfiledialog1.showdialog() == dialogresult.ok)
{
 string sconnectionstring = "provider=microsoft.jet.oledb.4.0;" +
 "data source="+openfiledialog1.filename+";"+
 "extended properties='excel 8.0;hdr=yes;imex=2'";
 //实例化一个oledbconnection类(实现了idisposable,要using)
 using (oledbconnection ole_conn = new oledbconnection(sconnectionstring))
 {
  ole_conn.open();
  using (oledbcommand ole_cmd = ole_conn.createcommand())
  {
  //类似sql的查询语句这个[sheet1$对应excel文件中的一个工作表]
  ole_cmd.commandtext = "select * from [sheet1$]";
  oledbdataadapter adapter = new oledbdataadapter(ole_cmd);
  dataset ds = new dataset();
  adapter.fill(ds, "sheet1");
    for (int i = 0; i < ds.tables[0].rows.count; i++)
  {
   messagebox.show(ds.tables[0].rows[i]["商家名称"].tostring());
  }
  }
 }
}

2.获取工作簿中所有的工作表

if (openfiledialog1.showdialog() == dialogresult.ok)
{
 string sconnectionstring = "provider=microsoft.jet.oledb.4.0;" +
 "data source="+openfiledialog1.filename+";"+
 "extended properties='excel 8.0;hdr=yes;imex=2'";
 //实例化一个oledbconnection类(实现了idisposable,要using)
 using (oledbconnection ole_conn = new oledbconnection(sconnectionstring))
 {
  ole_conn.open();
  using (oledbcommand ole_cmd = ole_conn.createcommand())
  {
  datatable tb = ole_conn.getoledbschematable(oledbschemaguid.tables, null);
  foreach (datarow row in tb.rows)
  {
   messagebox.show(row["table_name"].tostring());
  }
  }
 }
}

3.写入数据到excel表

if (openfiledialog1.showdialog() == dialogresult.ok)
{
 string sconnectionstring = "provider=microsoft.jet.oledb.4.0;" +
 "data source="+openfiledialog1.filename+";"+
 "extended properties=excel 8.0;";
 //实例化一个oledbconnection类(实现了idisposable,要using)
 using (oledbconnection ole_conn = new oledbconnection(sconnectionstring))
 {
  ole_conn.open();
  using (oledbcommand ole_cmd = ole_conn.createcommand())
  {
  ole_cmd.commandtext = "insert into [sheet1$](商户id,商家名称)values('dj001','点击科技')";
  ole_cmd.executenonquery();
  messagebox.show("数据插入成功......");
  }
 }
}

4.创建excel文件并写入数据

string sconnectionstring = "provider=microsoft.jet.oledb.4.0;" +
  "data source=d:\\excel1.xls;" +
  "extended properties=excel 8.0;";
 //实例化一个oledbconnection类(实现了idisposable,要using)
 using (oledbconnection ole_conn = new oledbconnection(sconnectionstring))
 {
  ole_conn.open();
  using (oledbcommand ole_cmd = ole_conn.createcommand())
  {
  ole_cmd.commandtext = "create table customerinfo ([customerid] varchar,[customer] varchar)";
  ole_cmd.executenonquery();
  ole_cmd.commandtext = "insert into customerinfo(customerid,customer)values('dj001','点击科技')";
  ole_cmd.executenonquery();
  messagebox.show("生成excel文件成功并写入一条数据......");
  }
}

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网