Excel Interoperability with C# - how to fill columns to the right

211 views Asked by At

I am making an Interoperability application with excel in C #. The aim is to put information in the textboxes and load this information to an excel sheet. I can fill the first column in the excel sheet... but what i want is to press a button that i have named "clean" and automatically clean the contents of the textboxes and then the new information that will be entered goes to the next Excel sheet column and so on.

Here is an example of the code I have:

 private void button9_Click(object sender, EventArgs e)
        {
            var excelApplication = new MyExcel.Application();
            excelApplication.Visible = false;
            MyExcel.Workbook excelWorkBook = excelApplication.Workbooks.Open(textBox189.Text);
            MyExcel.Worksheet excelWorkSheet = excelWorkBook.ActiveSheet;



            excelWorkSheet.Cells[25, 2] = textBox6.Text;
            excelWorkSheet.Cells[27, 2] = textBox11.Text;
            excelWorkSheet.Cells[29, 2] = textBox10.Text;
            excelWorkSheet.Cells[31, 2] = textBox9.Text;
            excelWorkSheet.Cells[35, 2] = textBox8.Text;
            excelWorkSheet.Cells[39, 2] = textBox7.Text;




            excelWorkBook.Save();
        }

Someone help me?

Thanks.

1

There are 1 answers

2
Johannes91 On

I'm not sure if I understand your problem. But why don't you create a variable that holds the column you want to write to. Every time you click clean this variable is incremented. Besides that you are opening the workbook again and again when you click on Button 9. I would do that only once.