1、加入相应的COM组件
在Winform程序中对Word进行操作需要引入相关的COM组件。如果安装的office2007则引入12.0版本的COM组件,如下图所示:
在对应的CS文件中使用其名字空间,
using System.Reflection;
using MSWord=Microsoft.Office.Interop. Excel
using Microsoft.Office.Core;
using System.IO;
2、打开及关闭Excel文件
string strDir = Directory.GetCurrentDirectory();
string fileName = strDir + @"\test.xls";
MSExcel.Application excelApp= new MSExcel.Application();
excelApp.Visible = true;//是打开可见