学习(一)C#利用窗体打开Excel文件进行正常访问和写入
来源:互联网 发布:淘宝卖家好评不返现 编辑:程序博客网 时间:2024/05/19 17:58
首先,在对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"); } }}
阅读全文
0 0
- 学习(一)C#利用窗体打开Excel文件进行正常访问和写入
- C#窗体打开EXCEL
- c# 向Excel文件写入数据(Workbook 和Worksheet )
- 利用PHPExcel实现Excel文件的写入和读取
- 使用C#和Excel进行报表开发(一)--开始
- C#打开EXCEL文件
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- C#创建、打开、读取、写入、保存Excel,Excel打印输出
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#读取EXCEL 文件同时向文件中写入数据和Excel的Range对象
- winform 不能正常打开窗体,总是提示文件下载
- C#不能打开excel文件
- 利用JAVA操作EXCEL文件(一)
- c# 写入数据到excel文件
- c# Excel 读取,StreamWriter写入文件
- Swift_Block
- cglib和asm的版本冲突问题_SparkStreaming
- springboot设置上传文件大小的方式(两种)
- HTTP详解(十三):对称加密算法正传
- (二)整数规划
- 学习(一)C#利用窗体打开Excel文件进行正常访问和写入
- 关于烂代码的那些事(中)
- 实用hostname查看主机名的步骤
- 合理的布局,绚丽的样式,谈谈Winform程序的界面设计
- 关于IE下DATE()方法出现Nan的情况。
- MIME type
- 死锁必要条件
- css3动画的多次调用
- const关键字简单理解