private void button5_Click(object sender, EventArgs e)
{
string currentPath = Directory.GetCurrentDirectory();
WriteExcel(textBox3.Text + textBox4.Text+".xlsx");
MessageBox.Show("文件创建成功!");
}
public void WriteExcel(string filename)
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
if (excelApp == null)
{
MessageBox.Show("抱歉在你的电脑上没有安装Excel");
return;
}
//判断指定路径中是否存在要创建的Excel
Microsoft.Office.Interop.Excel.Workbook workBook;
if (File.Exists(filename))
{
workBook = excelApp.Workbooks.Open(filename, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
}
else
{
workBook = excelApp.Workbooks.Add(true);
}
Microsoft.Office.Interop.Excel.Worksheet workSheet = workBook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
excelApp.Visible = false;
excelApp.DisplayAlerts = false;
workBook.SaveAs(filename);
workBook.Close(false, Missing.Value, Missing.Value);
excelApp.Quit();
workSheet = null;
workBook = null;
excelApp = null;
GC.Collect();
}