日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

学习(一)C#利用窗体打开Excel文件进行正常访问和写入

發布時間:2024/1/8 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学习(一)C#利用窗体打开Excel文件进行正常访问和写入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,在對Excel文件進行操作時,首先應該注意引用(word為一起加的,這里為非必要)

using MSWord = Microsoft.Office.Interop.Word; using MSExcel = Microsoft.Office.Interop.Excel;


(1)、能打開指定的Excel文件

private void button1_Click(object sender, EventArgs e){Form2 myform = new Form2();myform.ShowDialog();MessageBox.Show("hello world");OpenExcel(@"D:\工作簿2.xls");} (2 、定義一個方法打開文件(主要是初始化)

static void OpenExcel(string filename)//定義方法OpenExcel{MSExcel.Application excelApp = null;excelApp = new MSExcel.Application();excelApp.Visible = true;excelApp.DisplayAlerts = false;//保存Excel的時候,不彈出是否保存的窗口直接進行保存excelApp.AlertBeforeOverwriting = false;MSExcel.Workbook excelbook = null;MSExcel.Worksheet excelsheet = null;excelbook = excelApp.Workbooks.Open(filename, Type.Missing,false, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing);//表1excelsheet = excelbook.Sheets["hello1"] as MSExcel.Worksheet;string str1 = excelsheet.Cells[1, 1].Text.ToString();string str2 = excelsheet.Cells[1, 2].Text.ToString();MSExcel.Range rang1 = excelsheet.get_Range("E3:F5");string str3 = rang1.Cells[1, 1].Text.ToString();string str4 = rang1.Cells[1, 2].Text.ToString();excelsheet.Cells[5, 1] = "yoyou";excelsheet.Cells[8, 4] = "yoyo12u";excelbook.Save();excelbook.Close();excelApp.Quit();//逐級關閉,不可省略} 其中:

MSExcel.Application excelApp = null;excelApp = new MSExcel.Application();//打開Excel程序,使之運行excelApp.Visible = true;excelApp.DisplayAlerts = false;//保存Excel的時候,不彈出是否保存的窗口直接進行保存excelApp.AlertBeforeOverwriting = false; 為初始化操作。

尤其注意最后逐級關閉打開的系統,即為最后三行。




完整如下:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MSWord = Microsoft.Office.Interop.Word; using MSExcel = Microsoft.Office.Interop.Excel;namespace MyTest1 {public partial class Form1 : Form{public Form1(){InitializeComponent();}static void OpenExcel(string filename){MSExcel.Application excelApp = null;excelApp = new MSExcel.Application();excelApp.Visible = true;excelApp.DisplayAlerts = false;//保存Excel的時候,不彈出是否保存的窗口直接進行保存excelApp.AlertBeforeOverwriting = false;MSExcel.Workbook excelbook = null;MSExcel.Worksheet excelsheet = null;excelbook = excelApp.Workbooks.Open(filename, Type.Missing,false, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing);//表1excelsheet = excelbook.Sheets["hello1"] as MSExcel.Worksheet;string str1 = excelsheet.Cells[1, 1].Text.ToString();string str2 = excelsheet.Cells[1, 2].Text.ToString();MSExcel.Range rang1 = excelsheet.get_Range("E3:F5");string str3 = rang1.Cells[1, 1].Text.ToString();string str4 = rang1.Cells[1, 2].Text.ToString();excelsheet.Cells[5, 1] = "yoyou";excelsheet.Cells[8, 4] = "yoyo12u";excelbook.Save();excelbook.Close();excelApp.Quit();}private void button1_Click(object sender, EventArgs e){Form2 myform = new Form2();myform.ShowDialog();MessageBox.Show("hello world");OpenExcel(@"D:\工作簿2.xls");}} }


總結

以上是生活随笔為你收集整理的学习(一)C#利用窗体打开Excel文件进行正常访问和写入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。