


下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、教你用C#賣寫、刪除、更新excel表格記錄如以下圖所示,編一個程序,鼠標(biāo)單擊窗體視圖區(qū)右邊時,獲取一對坐標(biāo)X,Y,點擊保存將點保存到excel表記錄中。此外,還實現(xiàn)了刪除、更新功能以及翻開excel表功能。插入和更新比擬簡單,和操作一般的數(shù)據(jù)庫一樣,但是刪除稍微有點復(fù)雜,不能用 delete from Sheet1$ where ID=x 的方式刪除,自己可以去試,主要是 excel數(shù)據(jù)之間的關(guān)系不像關(guān)系數(shù) 據(jù)庫那么簡單,oledb不提供這種方法。所以只能用專門操作excel表的Microsoft.Office.lnterop.Excel 名字空間下,先添加引用來實現(xiàn)刪除某條記錄的功能。源代
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 Leationpublic partial class FrmMain :
3、 Form/定義變量private OleDbConnection connection = nullprivate private private private privateOleDbCommancdmd = null ;OleDbDataAdapterDataSet dataSet = string filePath = string connStr = properties='Excel 8.0;HDR=yes;IMEX=2dataAdapter = null ;null ; "G:points.xls" ;"provider=microsoft
4、.jet.oledb.4.0;data source=G:points.xls;extendedII!privatestringselectStr = "select * from Sheet1$privatestringcmdStr = null ;privatestringOID = null ; / 對象 IDprivatestringx = null ;privatestringy = null ;privateExcel.Application excelApp = null ;privateExcel.Workbook book = null ;privateExcel.
5、Worksheet sheet = null ;privateExcel.Range range = null ;/ 構(gòu)造函數(shù)publicFrmMain()InitializeComponent();/ 鼠標(biāo)移動事件private void splitContainer1_Panel2_MouseMove(objectsender, MouseEventArgs e)this .lblxy.Text = "x=" + e.X.ToString() +y=+ e.Y.ToString();/ 鼠標(biāo)按下事件private void splitContainer1_Panel2_
6、MouseDown(objectsender, MouseEventArgs e)if (e.Button = MouseButtons .Left)this .tbX.Text = e.X.ToString();this .tbY.Text = e.Y.ToString();/ 刷新 dataGridView1 private void RefreshTable()new OleDbConnection (connStr);connection = connection.Open(); dataAdapter = dataSet = dataAdapter.Fill(dataSet);new
7、 OleDbDataAdapter (selectStr, connection); new DataSet ();this .dataGridView1.DataSource = dataSet.Tables0; connection.Close();/ 程序加載事件,初始化 dataGridView1private void FrmMain_Load( object sender, EventArgs e) this .RefreshTable();/ 獲取一個可以用的 OID private string GetOID() int rowNum = this .dataGridView1
8、.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_Click( object sender, EventArgs e)OID = this .GetOID();
9、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 Sheet1$(ID,X,Y) values("+ OID + "," + x
10、+ "," + y + ")" ;cmd =new OleDbComman(dcmdStr, connection);int row=cmd.ExecuteNonQuery();if (row > 0)lblTip.Text =" 保存成功,插入行數(shù): " + row.ToString();elselblTip.Text =" 保存失敗 "connection.Close();this .RefreshTable();/ 刪除記錄private void btnDelSelRow_Click( object
11、sender, EventArgs e)int selRowIndex = this .dataGridView1.CurrentRow.Index + 2;/excel 表中的行索引與 dataGridView 不一樣,這里注意if (selRowIndex<1)MessageBox.Show( " 沒有選中行 ");lblTip.Text ="刪除失敗 "return ;excelApp = newMicrosoft.Office.Interop.Excel. Application ();excelApp.Visible = false ;
12、/ 假設(shè)為 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 .Value, Missing .Value, Missing .Value);/ 獲
13、取 sheet1sheet = (Excel.Worksheet )book.Worksheets1;/ 獲取編輯范圍range = (Excel. Range)sheet.RowsselRowIndex, Missing .Value;/ 刪除整行range.EntireRow.Delete(Excel. XlDeleteShiftDirection .xlShiftUp);/ 保存編輯 book.Save();看看 ;/ 關(guān)閉 book book.Close( Missing .Value, Missing .Value, Missing .Value);/ 退出 excel applic
14、ation,可以將前面的 excelApp.Visible = false 改為 excelApp.Visible = trueexcelApp.Workbooks.Close();excelApp.Quit();/ 刷新 dataGridView1this .RefreshTable();/ 選中刪除行的上一行if (selRowIndex - 3) > 0)this .dataGridView1.RowsselRowIndex - 3.Selected =true ;this .lblTip.Text="刪除成功 "/ 更新記錄private void btnUp
15、date_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 =this .tbY.Text;if (x ="&qu
16、ot; | 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 =new OleDbComman(dcm
17、dStr, connection);int row = cmd.ExecuteNonQuery();if (row >= 1)lblTip.Text ="更新成功,更新行數(shù): " + row.ToString();elselblTip.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.T
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 城市水環(huán)管理的綜合性研究計劃
- 制定產(chǎn)品研發(fā)路徑圖計劃
- 營造積極班級氛圍的方法計劃
- 新生適應(yīng)性教育工作計劃
- 2025年修改離婚協(xié)議模板
- 2025年智能燃?xì)獗砗献鲄f(xié)議書
- 2025年廠房租賃合同模板集錦十篇
- 三年級下冊數(shù)學(xué)教案-4.1 旋轉(zhuǎn)和平移現(xiàn)象 ︳西師大版
- 三年級上冊數(shù)學(xué)教案-4.5乘與除 練習(xí)三-北師大版
- 2025年合肥貨運資格證考試中心
- 2025年內(nèi)蒙古交通職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫含答案
- 河南2025年河南職業(yè)技術(shù)學(xué)院招聘30人筆試歷年參考題庫附帶答案詳解
- 急診危重癥患者轉(zhuǎn)運專家共識解讀課件
- 《混凝土預(yù)制構(gòu)件出廠驗收標(biāo)準(zhǔn)》
- 2025年數(shù)字安徽有限責(zé)任公司招聘筆試參考題庫含答案解析
- 2025年江蘇省無錫市江南大學(xué)專職輔導(dǎo)員招聘45人歷年高頻重點提升(共500題)附帶答案詳解
- 《拆除工程施工安全》課件
- 2024版智能物流倉儲管理系統(tǒng)采購與運維服務(wù)合同3篇
- 金融風(fēng)險細(xì)則解讀
- IATF16949:2024標(biāo)準(zhǔn)質(zhì)量手冊
- 2024年包頭鐵道職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫
評論
0/150
提交評論