




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、教你用C#读写、删除、更新excel表格记录如下图所示,编一个程序,鼠标单击窗体视图区(右边)时,获取一对坐标(X,Y),点击保存将点保存到excel表记录中。此外,还实现了删除、更新功能以及打开excel表功能。插入和更新比较简单,和操作一般的数据库一样,但是删除稍微有点复杂,不能用delete from Sheet1$ where ID=x的方式删除,自己可以去试,主要是excel数据之间的关系不像关系数据库那么简单,oledb不提供这种方法。所以只能用专门操作excel表的源代码:using System;usingusing System.ComponentModel;using Sy
2、stem.Data;using System.Drawing;using System.Text;usingusingusing System.Reflection;usingnamespace Leation public partial class FrmMain : Form /定义变量 private OleDbConnection connection = null; private OleDbCommand cmd = null; private OleDbDataAdapter dataAdapter = null; private DataSet dataSet = null;
3、 private string filePath = "G:points.xls" private string connStr = ; 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; priv
4、ate Excel.Workbook 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="" y=" /鼠标按下事件 private
5、 void splitContainer1_Panel2_MouseDown(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left) this this /刷新dataGridView1 private void RefreshTable() connection = new OleDbConnection(connStr); connection.Open(); dataAdapter = new OleDbDataAdapter(selectStr, connection); dataSet = new Data
6、Set(); dataAdapter.Fill(dataSet); this.dataGridView1.DataSource = dataSet.Tables0; connection.Close(); /程序加载事件,初始化dataGridView1 private void FrmMain_Load(object sender, EventArgs e) this.RefreshTable(); /获取一个可以用的OID private string GetOID() int rowNum = this int maxOID = 0; int temp = 0; for (int i =
7、 0; i < rowNum; i+) temp = int.Parse(this.dataGridView10, i.Value.ToString(); if (maxOID < temp) maxOID = temp; return (maxOID+1).ToString(); /插入一条记录,即保存一个点信息 private void btnSavePnt_Click(object sender, EventArgs e) OID = this.GetOID(); x = this.tbX.Text; y = this.tbY.Text; if (x = "&quo
8、t; | y = "") MessageBox.Show("x,y不能为空"); lblTip.Text = "保存失败" return; connection = new OleDbConnection(connStr); connection.Open(); cmdStr = "insert into Sheet1$(ID,X,Y) values(" + OID + "," + x + "," + y + ")" cmd = new OleDbComm
9、and(cmdStr, connection); int row=cmd.ExecuteNonQuery(); if (row > 0) lblTip.Text = "保存成功,插入行数:" + row.ToString(); else lblTip.Text = "保存失败" connection.Close(); this.RefreshTable(); /删除记录 private void btnDelSelRow_Click(object sender, EventArgs e) int selRowIndex = this/excel表中
10、的行索引与dataGridView不一样,这里注意 if (selRowIndex<1) MessageBox.Show("没有选中行"); lblTip.Text = "删除失败" return; excelApp = newApplication(); excelApp.Visible = false; /若为true,删除瞬间可以看见 office excel界面 /打开excel文件Missing.Value,false, Missing.Value, Missing.Value, Missing.Value, true, Missing.
11、Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); /获取sheet1 sheet = (Excel.Worksheet)book.Worksheets1; /获取编辑范围 range = (Excel.Range)sheet.RowsselRowIndex, Missing.Value; /删除整行 XlDeleteShiftDirection.xlShiftUp); /保存编辑 book.Save(); /关闭book
12、 book.Close(Missing.Value, Missing.Value, Missing.Value); /退出excel application,可以将前面的excelApp.Visible = false改为excelApp.Visible = true看看; excelApp.Quit(); /刷新dataGridView1 this.RefreshTable(); /选中删除行的上一行 if (selRowIndex - 3) > 0) this.dataGridView1.RowsselRowIndex - 3.Selected = true; this.lblTip
13、.Text="删除成功" /更新记录 private void btnUpdate_Click(object sender, EventArgs e) int selRowIndex= this if (selRowIndex< 0) MessageBox.Show("没有选中行!"); lblTip.Text = "更新失败" return; OID = this.dataGridView10, selRowIndex.Value.ToString(); x = this.tbX.Text; y = this.tbY.Text
14、; 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+"'" cmd =
15、 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 = "exce
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年南昌货运从业资格证考试题及答案大全
- 八个着力战略部署与3341项目工程-酒泉
- 物流行业货物运输及安全送达证明(7篇)
- ××超市促销活动规定
- 联合营销合作协议及说明
- 2025年消防安全知识实操考试题库:实操应用篇重点难点
- 2025年劳动关系协调员(初级)考试试卷:劳动保障政策解读与应用策略技巧
- 2025年铸造机械项目规划申请报告
- 体育场馆设施维护管理服务协议
- 历史文化遗产保护研究试题库
- 企业工会采购制度管理规定
- 浙江温州高速公路瓯北片区招聘高速公路巡查人员考试真题2022
- 江苏苏州工业园区苏相合作区管理委员会机关工作人员招聘13人告5204笔试题库含答案解析
- 2018年三年级数学下册期末试卷A3(附答题卡、答案)
- 三年级下学期音乐复习题
- 工伤预防概念1
- GA 1808-2022军工单位反恐怖防范要求
- 山水林田湖试点铜川市耀州区沮河下游生态保护修复项目环评报告
- 2023-2024学年云南省大理市小学数学四年级下册期末通关测试题
- GB/T 14536.1-2022电自动控制器第1部分:通用要求
- 一升二数学思维训练8 15
评论
0/150
提交评论