刚开始接触c#操作excel可能会不知从哪着手,但或许又急需写一个能实现excel自动化的程序,我把必要的步骤和一些最简单的常用操作写在下面,希望能帮到一些网友。
适用于:windows office 2003,visual c# 2008。其他版本可能需要略作改变。
首先
添加引用,方法:项目->添加引用->选择COM项->Microsoft Excel 11.0 Object Library
可能装了office 2003 也没有Microsoft Excel 11.0 Object Library,请重新装一遍。
然后
在using后面添加两行
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
如果不行请换上如下两行试试
using Excel;
using System.Reflection;
上面的没问题后便可以进行对excel的操作了,下面是常用的操作及步骤,有些是必须的
//创建excel 对象
Excel.Application exampleExcel = new Excel.Application();
//设置可视化
exampleExcel.Visible = true;
Excel.Workbook aBook;
Excel.Worksheet aSheet1;
Excel.Workbook bBook;
Excel.Worksheet bSheet1;
//打开工作簿,第一个参数为文件路径,其他参数可暂时不设置
aBook = exampleExcel.Workbooks.Open(aBookName,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//新建一个工作簿,第一个参数为文件路径,可暂时不设置
bBook = exampleExcel.Workbooks.Add(Missing.Value);
//选择表
aSheet1 = (Excel.Worksheet)aBook.Worksheets[1];
bSheet1 = (Excel.Worksheet)bBook.Worksheets[1];
//将工作簿a表1中单元格[x,y]中值复制到b表1中单元格[i,j],如下面将工作簿a表1中单元格A1的值复制赋给了工作簿b表1中单元格C2
//bSheet1.Cells[i,j] = aSheets1[x,y];
bSheet1.Cells[2, 3] = aSheet1.Cells[1, 1];
//关闭工作簿,第一个参数为是否保存修改,第二个为保存为的文件名,第三个可暂不设置
aBook.Close(false, Missing.Value, Missing.Value);
bBook.Close(true, bBookName, Missing.Value);
//退出
exampleExcel.Quit();
完整示例:
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 Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string aBookName, bBookName;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "excel files (*.xls)|*.xls";
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
aBookName = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "xls files (*.xls)|*.xls";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.OverwritePrompt = false;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
bBookName = saveFileDialog1.FileName;
}
//创建excel 对象
Excel.Application exampleExcel = new Excel.Application();
//可视化
exampleExcel.Visible = true;
Excel.Workbook aBook;
Excel.Worksheet aSheet1;
Excel.Workbook bBook;
Excel.Worksheet bSheet1;
//打开工作簿
aBook = exampleExcel.Workbooks.Open(aBookName,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//新建一个工作簿
bBook = exampleExcel.Workbooks.Add(Missing.Value);
//选择表
aSheet1 = (Excel.Worksheet)aBook.Worksheets[1];
bSheet1 = (Excel.Worksheet)bBook.Worksheets[1];
//将工作簿a表1中单元格[x,y]中值复制到b表1中单元格[i,j]
//bSheet1.Cells[i,j] = aSheets1[x,y];
bSheet1.Cells[2, 3] = aSheet1.Cells[1, 1];
//关闭工作簿
aBook.Close(false, Missing.Value, Missing.Value);
bBook.Close(true, bBookName, Missing.Value);
exampleExcel.Quit();
MessageBox.Show("复制完成\n", "提示");
}
}
}