I am able to write a groovy script for writing data to excel file. I want to reverse the loop in groovy script. Here is the groovy script-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
for (int i = 0; i < dataContext.getDataCount(); i++) {
InputStream is = dataContext.getStream(i);
Properties props = dataContext.getProperties(i);
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Data Sheet");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
int rownum = 0;
while ((line = reader.readLine()) != null) {
Row row = sheet.createRow(rownum);
rownum++;
int cellnum = 0;
String[] fields = line.split(",");
for (int j = 0; j < fields.length; j++) {
row.createCell(cellnum).setCellValue(fields[j]);
cellnum++;
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
is = new ByteArrayInputStream(baos.toByteArray());
dataContext.storeStream(is, props);
}
Actual-
Expected-
1
Replace your while
loop with:
is.readLines().reverseEach { line ->
...
}
Unlike the readLine()
it will read the whole file into memory, but I don’t think there is a way to figure out the number of lines without it. And the only two options are to have the number of lines and fill in the sheet from the last position or to read the entire file and process lines in reverse.