学习(一)C#利用窗体打开Excel文件进行正常访问和写入
生活随笔
收集整理的這篇文章主要介紹了
学习(一)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文件进行正常访问和写入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue - 网站首屏加载等待动画(极简解
- 下一篇: 【技巧】如何搜索公众号内文章