c# - Why the last row never gets read? -


hello have 1 question why last row never gets read? dosen´t matter if 1 row in excel file or 100 rows. last row never shows in list. , have no clue why....

here excel file: excel file

and method:

public list<string> getlistdata(bool skipfirstrow, int numberofcolumns, string filepath) {     int startpoint = 1;     int cell = 1;     int row = 1;      list<string> stringlist = new list<string>();      //open excel (application)     var excelapplication = openexcelapplication();     //open excel file     excel.workbook excelworkbook = excelapplication.workbooks.open(filepath);     //get worksheets file     excel.sheets excelsheets = excelworkbook.worksheets;     //select first worksheet     excel.worksheet worksheet = (excel.worksheet)excelsheets.get_item(1);      if (skipfirstrow == true)     {         startpoint = 2;     }      excel.range range = worksheet.get_range("a" + convert.tostring(startpoint), missing.value);      while ((range.cells[startpoint, cell] excel.range).value2 != null)     {         (int = 1; <= numberofcolumns + 1; i++)         {             string svalue = (range.cells[row, cell] excel.range).value2.tostring();             stringlist.add(svalue);             cell++;         }         startpoint++;         cell = 1;         row++;     }      closeexcelapplication(excelapplication);      var result =           stringlist         .select((item, index) => new { item = item, index = index })         .groupby(x => x.index / numberofcolumns)         .select(g => string.join(";", g.select(x => x.item)))         .tolist();      return result;  } 

i tried debugger , google. tried last used row stuff didnt worked.

excel.range last = worksheet.cells.specialcells(excel.xlcelltype.xlcelltypelastcell, type.missing); excel.range range = worksheet.get_range("a1", last);  int lastusedrow = last.row; int lastusedcolumn = last.column; 

any or advise great time , help.

your algorithm buggy.

let's see happens when skipfirstrow true , excel sheet has 3 rows 1, 2 , 3. @ start of while loop, have following situation:

startpoint = 2 row = 1 

during first iteration, while loop reads contents of row 1. after iteration, have following situation:

startpoint = 3 row = 2 

during second iteration, while loop reads contents of row 2. after iteration, have following situation:

startpoint = 4 row = 3 

since range.cells[startpoint, cell] empty, code stops here. rows 1 , 2 have been read, row 3 has been ignored.

as can see, reason problem check row in startpoint , read row in row, , when 2 differ, have problem. suggested solution: drop startpoint variable , use row instead.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -