首先添加引用
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;using System.Reflection;
using MSExcel = Microsoft.Office.Interop.Excel;//在后面可以少敲很多字
添加在一个button_Click事件中的
private void button2_Click(object sender, EventArgs e)
{
//选择新建的文件存储的地方
string saveFile = "";
SaveFileDialog saveFD = new SaveFileDialog();
if (saveFD.ShowDialog() == DialogResult.OK)
{
saveFile = saveFD.FileName.ToString();
}
MSExcel.Application excelApp = new MSExcel.Application();//新建excel程序
Workbook excelDoc;//新建一个工作文件
Object nothing = Missing.Value;//为了后面少敲字
excelDoc = excelApp.Workbooks.Add(nothing);
//存储新建的exce文件
excelDoc.SaveAs(saveFile, nothing, nothing, nothing, nothing, nothing, MSExcel.XlSaveAsAccessMode.xlExclusive, nothing, nothing, nothing, nothing, nothing);
excelDoc.Close(nothing, nothing, nothing);
excelApp.Quit();
}