C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式

前言

作为项目管理大队中的一员,在公司里面接触最多的就是Excel文件了,所以一开始就想从Excel入手,学习简单的二次开发,开始自己的编程之路!

程序界面

功能说明

打开文件按钮,可以由使用者指定要操作的Excel文件,并在后面的textBox中显示出文件路径。

设置单元格按钮,可以根据程序设置Excel文件的内容。

退出程序按钮,关闭窗体。

程序源代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Reflection;
10 using Microsoft.Office.Interop.Excel;
11 namespace ExcelReadAndWrite
12 {
13     public partial class Form1 : Form
14     {
15         public String filename = string.Empty;
16         public Form1()
17         {
18             InitializeComponent();
19         }
20         /// <summary>
21         /// 打开Excel文件,并且设置制定单元格的值
22         /// </summary>
23         /// <param name="sender"></param>
24         /// <param name="e"></param>
25         private void button1_Click(object sender, EventArgs e)
26         {
27             if (textBox_FileName.Text != "")
28             {
29                 //这是读取Excel文件的一种方式
30                 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
31                 Workbook wbook = app.Workbooks.Open(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
32                     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
33 
34                 Worksheet worksheet = (Worksheet)wbook.Worksheets[1];
35                 worksheet.Cells[1, 1] = "ID";
36                 worksheet.Cells[1, 2] = "Value";
37                 for (int i = 2; i < 100;i++ )
38                 {
39                     worksheet.Cells[i, 1] = i.ToString();
40                     worksheet.Cells[i, 2] = i * i;
41                 }
42                 MessageBox.Show("已经设置成功!");
43 
44                 //保存并退出Excel文件
45                 wbook.Save();
46                 worksheet = null;
47                 wbook = null;
48                 app.Quit();
49                 app = null;
50             }
51             else
52             {
53                 MessageBox.Show("还未指定文件!");
54             }
55         }
56         /// <summary>
57         /// 关闭窗体
58         /// </summary>
59         /// <param name="sender"></param>
60         /// <param name="e"></param>
61         private void button_Quit_Click(object sender, EventArgs e)
62         {
63             this.Close();
64         }
65         /// <summary>
66         /// 获取指定文件的文件名
67         /// </summary>
68         /// <param name="sender"></param>
69         /// <param name="e"></param>
70         private void button_OpenFile_Click(object sender, EventArgs e)
71         {
72             OpenFileDialog ofd = new OpenFileDialog();
73             ofd.InitialDirectory = "E:\";
74             ofd.Filter="Excel文件|*.xlsx";
75             ofd.RestoreDirectory = true;
76             ofd.FilterIndex = 1;
77             if (ofd.ShowDialog() == DialogResult.OK)
78             {
79                 filename = ofd.FileName;
80                 textBox_FileName.Text = filename;
81             }
82         }
83     }
84 }
原文地址:https://www.cnblogs.com/xingzhui/p/6119658.html