Write entered numbers in an excel file with each button click in C# -



Write entered numbers in an excel file with each button click in C# -

i want create win app includes 1 textbox , 1 button 'add'. when user enters number , clicks button first time, programme create excel file , write numbers in order in file each click. here buttonadd:

private void buttonadd_click(object sender, eventargs e) { double num1; excel.application xlapp; excel.workbook xlworkbook; excel.worksheet xlworksheet; xlapp = new excel.application(); xlworkbook = xlapp.workbooks.add(); xlworksheet = (excel.worksheet)xlworkbook.activesheet; num1 = convert.todouble(textbox1.text); xlworksheet.cells[i, 1] = num1; i++; xlworkbook.saveas(path); xlworkbook.close(true); }

i know code not work because in every button click, programme tries create new excel file same path. new object oriented programming don't know how can create excel file outside of 'buttonadd_click' , phone call within 'buttonadd_click'.

is there has thought it?

you need create excel sheet outside buttonadd_click function. within function access sheet.

public partial class mainwindow : window { // declare private fellow member in mainwindow excel.application xlapp; excel.workbook xlworkbook; excel.worksheet xlworksheet; private void buttonadd_click(object sender, eventargs e) { // assign our private fellow member once, @ first button click if (xlworksheet == null) { xlapp = new excel.application(); xlworkbook = xlapp.workbooks.add(); xlworksheet = (excel.worksheet)xlworkbook.activesheet; } // after sec button click, have reference // work sheet, can write it. xlworksheet.cells[1, 1] = 10; // example. // don't close excel application here. }

you can close excel application when main application closes , not after every time write sheet, current code does. can utilize window.closing event, example.

c# excel

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -