提问者:小点点

CS0117 Form1不包含应用程序定义。(Windows studio 2019)


//我试图关闭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;

    }

共1个答案

匿名用户

尝试这样做:将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();
        }