Okay, I have had a look at the example and think I can see where the
confusion arises. What you need to do is either keep track of the rows using
a Collection - an ArrayList for example - or interrogate the Sheet object
for the row so that you can add onto it. Of the two options, the latter is
by far the better one IMO as the Sheet object is already storing the Row
references for you internally in a list structure.
Obviously, I do not know what triggers the requirement to add a new cell
onto a row (to jump from populating cell A6 on your example to populating
cell B2), only you can be fully aware of your algorithm, but you need to
basically do something like the following (and I am assuming that you are
targetting the HSSF stream here;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
// Create the first row.
HSSFRow row = sheet.createRow(0);
// Create the first cell on the row
HSSFCell cell = row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(10);
// Now, repeat to create the first column full of cells
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(20);
row = sheet.createRow(2);
cell = row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(30);
row = sheet.createRow(3);
cell = row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(40);
// Assuming that you have filled the first column and want to create the
next cell
// as cell B1 - the second cell on the first row - the trick is to recover
the reference
// for the first row from the sheet like this;
row = sheet.getRow(0);
// and then you can add a cell to the row
cell = row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(400);
and so on.....
One extra wrinkle is to test the value returned by the call to the getRow()
method. If that method call returns a null value, then you will need to
create the row, something a little like this;
row = sheet.getRow(5);
if(row == null) {
row = sheet.createRow(5);
}
The other option would be to get your data set and then populate each row
completely but this may not be possible; only you can know the constraints
imposed by the data set.
Hope this helps.
Yours
Mark B
njr30 wrote:
>
> Hi Mark,
>
> Thanks for the reply.
>
> I am reading the data from db and writing the data on to spreadsheet using
> POI.
>
> I have attached a sample spreadsheet with this post.
>
> I have got 7 cells where the data has to be written. I first fill up cell
> 1 then cell2 and so on.
>
> Say for eg one cell one data was laid out for over 5 rows. When I start
> writing to cell 2 it starts writing from row 6 but i want it to start
> again from row 1 same with the rest.
>
> In the attached spreadsheet i have 2 worksheets wrong and right. wrong is
> what the pgm is doing at the moment and the right has the way data has to
> be laid out. Can you kindly
> http://www.nabble.com/file/p26012375/sample.xls sample.xls assist.
>
> Thanx a lot in advance.
>
>
> Thanks&Regards
> Jag
>
>
> MSB wrote:
>>
>> Sorry, but I am not competely clear what you are asking.
>>
>> Are you saying that you have a worksheet and use POI to open it, access a
>> number of cells on the sheet that could each be on a different row,
>> change the value in each of those cells and then re-position each onto
>> row 3 of the worksheet?
>>
>> Yours
>>
>> Mark B
>>
>>
>> njr30 wrote:
>>>
>>> Hi,
>>>
>>> I am writing some data to Excel using POI where I have to reset the row
>>> number to 3 as I complete inserting values in different cells.
>>>
>>> How do I do that ?
>>>
>>> Thanks,
>>> njr30
>>>
>>
>>
>
>
--
View this message in context:
http://www.nabble.com/Setting-the-row-number-to-3-while-inserting-values-in-different-cells-%21%21-tp26003733p26013360.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]