I am currently trying to create an excel through my android application. I have a response array that is fetched from the server. But when the data is inserted into the excel only the last columns are filled for some weird reason. I tried a few solutions I switched from for iterations to for-each to traverse through the array because the for loop was not working the way I intended it to do.
I logged the data received which was supposed to be inserted into the cell at the position and row and the logged data was correct but the output was not.
The below code is for setting values into the cells and logging the position and row. Also attached the image of the output: [![This is the image of the issue am facing][1]][1]
private void setData() {
try {
int i = 0;
for (int pos : postions) {
List<Orderpojo> atttt = Arrays.asList(dataList.get(i).getEmployee());
int pos1 = pos 1;
int pos2 = pos 2;
int intimecounter = row_index;
Log.e(Admin_Att_Report.class.getSimpleName(), "setData: Position" pos);
for (Orderpojo Record : atttt) {
Row newrow = sheet.createRow(intimecounter);
cell = newrow.createCell(pos);
cell.setCellValue(Record.getIn_Time());
Log.e(Admin_Att_Report.class.getSimpleName(), "setData: Data Inserted at Row " intimecounter "\n"
"VALUE User Name " dataList.get(i).getMgmt_Name()
"\n"
"VALUE " pos " : InTime " Record.getIn_Time()
"\n"
"VALUE " pos1 " : OutTime " Record.getOut_Time()
"\n"
"VALUE " pos2 " : Work DOne " Record.getWork_Done()
"\n");
cell = newrow.createCell(pos1);
cell.setCellValue(Record.getOut_Time());
cell = newrow.createCell(pos2);
cell.setCellValue(Record.getWork_Done());
Log.e(Admin_Att_Report.class.getSimpleName(), "setData: Data Inserted at Row " intimecounter " at Columns :" pos " & " pos1 " & " pos2);
intimecounter = intimecounter 1;
}
i ;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
The Data is supposed to be filled in three positions: 'In-Time', 'Out-Time' & 'Work Done'. Hence the incremental variables such as pos1 and pos2 are used. The starting position is three and incremented by 3 for every new user. ie 3,9,12,.... Hence filling the positions for the data.
The logged data looks as follows and is correct and verified a dozen times.
Admin_Att_Report: setData: Data Inserted at Row 6
VALUE User Name Rohit Borkar
VALUE 39 : InTime 2022-02-05 09:43:31
VALUE 40 : OutTime 2022-02-05 18:06:39
VALUE 41 : Work DOne working on amanora park school drawings with vinay.
I hope the logged data is understandable. [1]: https://i.stack.imgur.com/hrgEa.png
CodePudding user response:
Using apache poi
Row.createRow
always creates a new empty row. If that row already existed in the sheet, then all cells in that row will be removed.
So if you want to fetch and update already existing rows, you must first try to get those rows with Sheet.getRow
. Only if Sheet.getRow
returns null
because that row doesn't exists yet, create it.
In your example:
...
Row newrow = sheet.getRow(intimecounter);
if (newrow == null) newrow = sheet.createRow(intimecounter);
...