版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 37002-2026网络安全技术电子邮件系统安全技术规范
- 广东韶关市2026年上半年全国英语等级考试(PETS)一级B全真模拟试题及答案解析
- 2026年造价员考试市政工程计量与计价实务综合练习题及答案
- 2026年上半年全国英语等级考试(PETS)一级B全真模拟试题及答案解析(苏州)
- 汽车驾驶员(技师)模拟考试题库含答案
- 2026二级健康管理师《理论知识》试题A卷附答案
- 铸造企业驾驶员运行操作安全操作规程
- 安全生产月知识竞赛题库及答案
- 新版冶金(有色)生产安全作业(煤气作业)特种作业人员考试题库(含答案)
- 巡视条例试题库及答案
- 2025-2030跑酷培训行业市场发展分析及前景趋势与投融资发展机会研究报告
- 光伏发电站施工作业指导手册与工程实践案例分析
- 企业内部控制制度检查表模板
- 设备振动基础知识培训课件
- 风电场运维风险防控策略2025
- 2025年新版《医疗器械经营质量管理规范》培训试题(附答案)
- 四升五数学40天(暑假作业人教版)
- TCFPA0032021模块化消防救援方舱
- 2025年国投招聘笔试参考题库附带答案详解
- 呼吸科常见吸入剂临床应用指南
- QGDW10384-2023输电线路钢管塔加工技术规程
评论
0/150
提交评论