Fill Serie xls with java POI

Asked

Viewed 66 times

2

I have a code inside a for that takes a certain column and insert the value, and assigns the amounts of lines defined in for, for example Rows =1 ; Rows<=10, put the setCellValue("10/02/2012") so it fills all fields with this same date, how do I fill in in series, where you get in trouble.

for(int rowIndex = 1;rowIndex<=10;rowIndex++)
{

    Row rowsx = sheetAlunos.createRow(rowIndex);
    HSSFRow row = sheetAlunos.getRow(rowIndex);

    Cell data = row.createCell(0);
    data.setCellValue("10/02/2012");

}
  • 1

    You can put what you’ve done?

  • added code, in case each lop of for it has to auto increment that date set initially, equal in execel with the fill serie option.

  • Beauty, you want to add date in series? ex: 10/02/2012, 11/02/2012.. ?

  • Yeah, that’s right

1 answer

1


Come on, if I understand correctly you want to pick a date and increment 1 day each time you pass the loop, ex:

10/02/2012
11/02/2012
12/02/2012

For that you can use the Classe Calendar to add days in a date, ex:

calendar.add(Calendar.DAY_OF_MONTH, 1);

In your case you need that number of days to be incremented according to your tie. You can create a method to do the date conversion and assign the days:

public String stringToCalendar(String date, int i) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));
        calendar.add(Calendar.DAY_OF_MONTH, i);
        return sdf.format(calendar.getTime());
}

This method takes two parameters, the first is the date you want to pass which in the case is "10/02/2012" and the next will be the rowIndex to increase it on date.

Your Lasso would look that way:

for (int rowIndex = 0; rowIndex <= 10; rowIndex++) {
        //Chama o método que converte uma String pra Calendar e incrementa o rowIndex na data
        String date = stringToCalendar("10/02/2012", rowIndex);
        Row rowsx = sheet1.createRow(rowIndex);
        HSSFRow rowx = sheet1.getRow(rowIndex);
        Cell data = rowx.createCell(0);
        //Passa a string que foi retornada para a celula
        data.setCellValue(date);
}

Exit:

inserir a descrição da imagem aqui

  • 1

    Exactly what I wanted, I knew the classCalendar but I had not thought to last it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.