poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Crocker, David" <David.Croc...@nrel.gov>
Subject RE: Inconsistancy in row numbers
Date Sat, 17 May 2014 16:45:51 GMT
No apologies necessary.  You gave a full, clear, and complete answer, and it must have taken
significant time and effort.

-----Original Message-----
From: Polk, Scott [mailto:scott.polk@pearson.com] 
Sent: Friday, May 16, 2014 3:13 PM
To: POI Users List
Subject: Re: Inconsistancy in row numbers

David - I'm not trying to step on your response, but I feel a bit of explanation is in order
that may help resolve the issue.


As stated in the Javadocs for getRow()
(https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html):

  public XSSFRow getRow(int rownum)

  Returns the logical row (0-based). If you ask for a row that is not defined you get a null.
This is to say row 4 represents the fifth row on a sheet.

Looking at the worksheet in Excel, the rows start at row 1 (1-based).
POI starts at row 0 (0-based).  So:

Excel row 1 = POI row 0
Excel row 2 = POI row 1
Excel row 3 = POI row 2
etc.

If your code is:

  Row row = mySheet.getRow(2);

it is going to return the 3rd row of data.  If you want the 2nd row, then you need to use
mySheet.getRow(1).  If you want 1st row, then you need to use mySheet.getRow(0).

I'm not sure how you are keeping track of which row you are on, but you could just subtract
1 from errorRowNumber, which should return the right row:

XSSFWorkbook workBook = new XSSFWorkbbok(filePath); Sheet mySheet = workBook.getSheetAt(0);
Row errorRow = mySheet.getRow(errorRowNumber - 1); //errorRowNumber = 2

Depending on how your code is written, this could work perfectly, or it could cause issues
somewhere downstream.  It all depends on how you are assigning errorRowNumber (as David Crocker
asked) and if/how it is being used later in your code.  This would also fail if errorRowNumber
= 0.




On Fri, May 16, 2014 at 2:43 PM, Crocker, David <David.Crocker@nrel.gov> wrote:
>
> Is it a zero-base array error?  When you get the row number, how do you do the comparison?
>
> -----Original Message-----
> From: Renjith R [mailto:ranju4u6@gmail.com]
> Sent: Thursday, May 15, 2014 5:56 AM
> To: user@poi.apache.org
> Subject: Inconsistancy in row numbers
>
> Hi,
>
>       I am reading an excel file using SAX+XSSF method. While reading I am saving the
row numbers of each row from 'attributes.getValue("r")' of 'Row'
> tag.
> After validating data of each row in the application, i want to write error at the last
column of each row.
> For writing the error ro excel I am using XSSFWorkbook alone.
>
> *Issue:*
>
> My sheet is having date at second row(first row is not used).On reading the excel sheet,
i will record the row number as 2.while validating i will check if the date is a past date
or not. If the date is a past date, i have to write 'INVAlID DATE' at the last column of second
row.
> For that i am using the following code.
>
> XSSFWorkbook workBook = new XSSFWorkbbok(filePath); Sheet mySheet = 
> workBook.getSheetAt(0); Row errorRow = mySheet.getRow(errorRowNumber); 
> //errorRowNumber = 2
>
> But row returned is not the second row, but the 3rd row which has some other data.
>
> My Assumption: XSSFWorkbook will not consider the first empty row. It will consider the
second row(which has data) as the first row. So on getting row with row number '2', it will
return 3rd row.
>
>
> Please let me know if my assumption is correct or not, if yes, how can i resolve it?
>
> --
> *RENJITH R*
> 9446011990
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org For additional 
> commands, e-mail: user-help@poi.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org For additional commands, e-mail: user-help@poi.apache.org

Mime
View raw message