//我试图关闭excel工作簿,但在“应用程序”上出现CS0117错误
private void btnExcelClose_Click(object sender, EventArgs e)
{
//Set the file name
object fileName = "FoodPantry.xlsx";
//Get the file
Excel.Workbook workbook = Form1.Application.Workbooks.get_Item(fileName);
//Close the workbook
workbook.Close(false);
}
private void btExcelOpen_Click(object sender, EventArgs e)
{
// Get fully qualified path for xlsx file
string excelWB = (@"C:\Users\User-123\Documents\Food Pantry\Working Backups\FoodPantry.xlsx");
var exApp = new Microsoft.Office.Interop.Excel.Application();
var exWbk = exApp.Workbooks.Open(excelWB);
exApp.Visible = true;
}
尝试这样做:将exApp声明为Form的全局变量。
Microsoft.Office.Interop.Excel.Application exApp;
private void btExcelOpen_Click(object sender, EventArgs e)
{
// Get fully qualified path for xlsx file
string excelWB = (@"D:\sts_device.xlsx");
exApp = new Microsoft.Office.Interop.Excel.Application();
var exWbk = exApp.Workbooks.Open(excelWB);
exApp.Visible = true;
}
private void btnExcelClose_Click(object sender, EventArgs e)
{
exApp.Workbooks.Close();
}