首先,在对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);
//表1
excelsheet = 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);
//表1
excelsheet = 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");
}
}
}