版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、教你用c#读写、删除、更新excel表格记录如下图所示,编一个程序,鼠标单击窗体视图区(右边)时,获取一对坐标(x,y),点击保存将点保存到excel表记录中。此外,还实现了删除、更新功能以及打开excel表功能。插入和更新比较简单,和操作一般的数据库一样,但是删除稍微有点复杂,不能用delete from sheet1$ where id=x的方式删除,自己可以去试,主要是excel数据之间的关系不像关系数据库那么简单,oledb不提供这种方法。所以只能用专门操作excel表的(microsoft.office.interop.excel名字空间下,先添加引用)来实现删除某条记录的功能。源代
2、码:using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.text;using system.windows.forms;using system.data.oledb;using system.reflection;using excel = microsoft.office.interop.excel;namespace leation public partial class frmmain
3、: form /定义变量 private oledbconnection connection = null; private oledbcommand cmd = null; private oledbdataadapter dataadapter = null; private dataset dataset = null; private string filepath = "g:points.xls" private string connstr = "provider=microsoft.jet.oledb.4.0;data source=g:point
4、s.xls;extended properties='excel 8.0;hdr=yes;imex=2'" private string selectstr = "select * from sheet1$" private string cmdstr = null; private string oid = null; /对象id private string x = null; private string y = null; private excel.application excelapp = null; private excel.wo
5、rkbook book = null; private excel.worksheet sheet = null; private excel.range range = null; /构造函数 public frmmain() initializecomponent(); /鼠标移动事件 private void splitcontainer1_panel2_mousemove(object sender, mouseeventargs e) this.lblxy.text = "x=" + e.x.tostring() + " y=" + e.y.t
6、ostring(); /鼠标按下事件 private void splitcontainer1_panel2_mousedown(object sender, mouseeventargs e) if (e.button = mousebuttons.left) this.tbx.text = e.x.tostring(); this.tby.text = e.y.tostring(); /刷新datagridview1 private void refreshtable() connection = new oledbconnection(connstr); connection.open(
7、); dataadapter = new oledbdataadapter(selectstr, connection); dataset = new dataset(); dataadapter.fill(dataset); this.datagridview1.datasource = dataset.tables0; connection.close(); /程序加载事件,初始化datagridview1 private void frmmain_load(object sender, eventargs e) this.refreshtable(); /获取一个可以用的oid priv
8、ate string getoid() int rownum = this.datagridview1.rows.count - 1; int maxoid = 0; int temp = 0; for (int i = 0; i < rownum; i+) temp = int.parse(this.datagridview10, i.value.tostring(); if (maxoid < temp) maxoid = temp; return (maxoid+1).tostring(); /插入一条记录,即保存一个点信息 private void btnsavepnt_c
9、lick(object sender, eventargs e) oid = this.getoid(); x = this.tbx.text; y = this.tby.text; if (x = "" | y = "") messagebox.show("x,y不能为空"); lbltip.text = "保存失败" return; connection = new oledbconnection(connstr); connection.open(); cmdstr = "insert into s
10、heet1$(id,x,y) values(" + oid + "," + x + "," + y + ")" cmd = new oledbcommand(cmdstr, connection); int row=cmd.executenonquery(); if (row > 0) lbltip.text = "保存成功,插入行数:" + row.tostring(); else lbltip.text = "保存失败" connection.close(); this.re
11、freshtable(); /删除记录 private void btndelselrow_click(object sender, eventargs e) int selrowindex = this.datagridview1.currentrow.index + 2; /excel表中的行索引与datagridview不一样,这里注意 if (selrowindex<1) messagebox.show("没有选中行"); lbltip.text = "删除失败" return; excelapp = new microsoft.offic
12、e.interop.excel.application(); excelapp.visible = false; /若为true,删除瞬间可以看见 office excel界面 /打开excel文件 book = excelapp.workbooks.open(filepath, missing.value,false, missing.value, missing.value, missing.value, true, missing.value, missing.value, missing.value, missing.value, missing.value, missing.valu
13、e, missing.value, missing.value); /获取sheet1 sheet = (excel.worksheet)book.worksheets1; /获取编辑范围 range = (excel.range)sheet.rowsselrowindex, missing.value; /删除整行 range.entirerow.delete(excel.xldeleteshiftdirection.xlshiftup); /保存编辑 book.save(); /关闭book book.close(missing.value, missing.value, missing.
14、value); /退出excel application,可以将前面的excelapp.visible = false改为excelapp.visible = true看看; excelapp.workbooks.close(); excelapp.quit(); /刷新datagridview1 this.refreshtable(); /选中删除行的上一行 if (selrowindex - 3) > 0) this.datagridview1.rowsselrowindex - 3.selected = true; this.lbltip.text="删除成功"
15、 /更新记录 private void btnupdate_click(object sender, eventargs e) int selrowindex= this.datagridview1.currentrow.index; if (selrowindex< 0) messagebox.show("没有选中行!"); lbltip.text = "更新失败" return; oid = this.datagridview10, selrowindex.value.tostring(); x = this.tbx.text; y = thi
16、s.tby.text; if (x = "" | y = "") messagebox.show("x,y不能为空"); lbltip.text = "更新失败" return; connection = new oledbconnection(connstr); connection.open(); cmdstr = "update sheet1$ set x="+x+",y="+y+" where id='"+oid+"'&q
17、uot; cmd = new oledbcommand(cmdstr, connection); int row = cmd.executenonquery(); if (row >= 1) lbltip.text = "更新成功,更新行数:" + row.tostring(); else lbltip.text = "更新失败" connection.close(); this.refreshtable(); /选中更新的行 this.datagridview1.rowsselrowindex.selected = true; private void btnopenfile_click(object sender, eventargs e) openfiledialog ofd = new openfiledialog(); ofd.filter = "excel文件(*.xls)|*.xls" ofd.title = "代开excel表" if (ofd
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 44400-2024电动振动台双台并激振动试验系统
- 《2024年 锂离子电池的明星材料磷酸铁锂-基本性能、优化改性及未来展望》范文
- 《 基于物料运动模型的圆锥破碎机工作性能研究》
- 出版传媒业数字化内容生产与传播平台建设规划书
- 农产品电子商务解决方案
- 人教版一年级数学下册导学案【练习课(1-2课时) (3)】
- 变电站系统运行维护操作手册
- 期末高频考题组合检测卷(试卷)2024-2025学年六年级数学上册人教版
- 仪器分析智慧树知到答案2024年华北科技学院
- 朵娅2019年度合作建议-20190118-
- 2024年中学教师资格证《综合素质》真题及答案(完整版)
- JT-T-329-2010公路桥梁预应力钢绞线用锚具、夹具和连接器
- 细胞生物学考研复习笔记
- 庆祝中华人民共和国成立75周年系列活动策划方案(3种类型30余项活动设计)
- 如何提升高中生的学业成绩和学习动力
- 联芯科技ERP项目-TOBE-未来业务蓝图-FICO-V2.0
- 八上语文第六单元三说
- 2024年综合病例站点式竞赛理论试题
- DB13-T1773-2013水生植物建植及养护管理技术规范
- 教师结构化面试题目及解答汇总
- 第1课《自主选择课余生活》第二课时(课件)
评论
0/150
提交评论