Question Hi, I have a The name 'excelApp' does not exist in the current context. problem

TADBABA

New member
Joined
Dec 9, 2020
Messages
3
Programming Experience
Beginner
I am new to C# I met this problem:
1607522700418.png

It says: The name 'excelApp' does not exist in the current context.
I am trying not to let the program initial and create a new Excel file while running.
Any help?
Appreciate it.
C# code:
public void excel_add(int t1, int t2)
        {
             row++;
            if (excel_init == 0)
            {
                var excelApp = new Excel.Application();
           
           

                excelApp.Visible = true;
                excelApp.Workbooks.Add();
                Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
                workSheet.Cells[1, "A"] = "Time";
                workSheet.Cells[1, "B"] = "Temperature";
                workSheet.Cells[1, "C"] = "Sampling frequency";
                workSheet.Cells[1, "D"] = "Display period";
                workSheet.Cells[1, "E"] = "Average number";
            }

                workSheet.Cells[row, "A"] = t1.ToString();
                workSheet.Cells[row, "B"] = t2.ToString();
                workSheet.Cells[row, "C"] = Sampling_frequency;
                workSheet.Cells[row, "D"] = Display_period;
                workSheet.Cells[row, "E"] = Average_number;
                workSheet.Columns[1].AutoFit();
                workSheet.Rows[1].AutoFit();

                excel_flag = 1;
                excel_init = 1;

         
           
                 
                 
                    //workSheet.Cells[row, "A"] = "32A";
         }
 
Last edited:
Welcome to the website.

Please post your code in code tags. And please do no post screenshots of code since there is no way for us to copy the contents.

Put your closing bracket at the end of your file. }
 
Welcome to the website.

Please post your code in code tags. And please do no post screenshots of code since there is no way for us to copy the contents.

Put your closing bracket at the end of your file. }
Thanks, I have posted the code
 
Why have you closed off the circuit logic of your if statement?

Your work sheet and excel object is created inside the if statement. But you are closing off the circuit logic on line 18. Why?

If line 5 does not execute because it's condition should not be met, you will get an error on line 20.
 
You will get an error because you are closing the circuit logic of your if statement on line 18.

In order to use that code, the closing bracket needs to come after line 27.

Please be precise about what you are trying to do.
 
Back
Top Bottom