AlejandroGS
AlejandroGS

Reputation: 7

How to send data from WPF to Excel in groups of rows?

I have a program in C# and WPF that sends data from text boxes to Excel (I use Interop). The format used follows a succession of rows, but in the same column.

For example, from row 15 to 21, the data must be filled out. Then you must follow a sum of 8 (for example the next should be row 23 to 29) in each number until you reach a row 239.

Example of Rows in Excel

I tried with this code, but it didn't work:

int _lastRow = xlWorkSheet.Range["E" +
    xlWorkSheet.Columns.Count].End[Excel.XlDirection.xlDown].Column + 1;

I tried with a for loop but it did not work. I also tried with an example of Fibonacci, trying to adapt it to the program.

Upvotes: 0

Views: 168

Answers (1)

JosephC
JosephC

Reputation: 929

Not sure the differences in C# but this is how I would do it in VB

Initialize  
CurRow = 15

Submit Event  
 xlWorkSheet.Cells(CurRow, 5) = tbLine1.text  
 xlWorkSheet.Cells(CurRow + 1, 5) = tbLine2.text  
 xlWorkSheet.Cells(CurRow + 2, 5) = tbLine3.text  
 ...  
 xlWorkSheet.Cells(CurRow + 6, 5) = tbLine7.text  
 CurRow += 8  

Every time you press your button to submit it will use your CurRow Counter, and add your 7 lines, and then increment your counter by 8 to leave a space for the next group of lines. You can put the Submit code in a loop if required. Can do a check if you exceed your row 239 limit

Upvotes: 1

Related Questions