学习(一)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);


            //表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");
        }
    }
}


  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值