Fill Series xls with POI java

2

I have a code inside a for that it takes a certain column and inserts the value, and assigns the quantities of rows defined in for, for example rows = 1; rows

asked by anonymous 13.06.2016 / 14:20

1 answer

1

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

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

For this you can use Classe Calendar for add days to a date, eg:

calendar.add(Calendar.DAY_OF_MONTH, 1);

In your case you need this number of days to be incremented according to your loop. 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 that in the case is "10/02/2012" and the following is the rowIndex to increment it on the date.

Your Tie would look like this:

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);
}

Output:

    
13.06.2016 / 19:31