一些对excel的操作, 适用于要求不是很高的excel文件操作, 赋值, 计算等等…
1.添加引用
新建工程, 引用–添加引用–程序集–扩展–添加 Microsoft.Office.Interop.Excel
2.主要代码
using Microsoft.Office.Interop.Excel;
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application app1 = new Microsoft.Office.Interop.Excel.Application();
app1.Visible = true;
// get book, sheet, range object
Workbook book1 = app1.Workbooks.Add(Type.Missing);
Worksheet sheet1 = (Worksheet)book1.Sheets[1];
Range rng1 = sheet1.get_Range("A1", Type.Missing);
// add a sheet
book1.Sheets.Add(After: book1.Sheets[book1.Sheets.Count]);
// Set value in the cells
// equals -> sheet1.get_Range("A1").Value = "aaa";
rng1.Value2 = "Hello World!";
// delete a sheet
app1.DisplayAlerts = false;
sheet1.Delete();
app1.DisplayAlerts = true;
}
}
}