poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Chadov <omene...@gmail.com>
Subject Get IndexOutOfBoundsException while creating Excel with data grouping
Date Fri, 02 Aug 2013 12:43:03 GMT
Hello!
I'm trying to create Excel with data grouping.
Could you please clarify why I get IndexOutOfBoundsException?

There is a simple test attached to create Excel with data grouping.
When it groups rows in reverse order (starting from the last row) it works
ok
When I change it to natural order (starting from the first row) - it throws
IndexOutOfBoundsException.

Stacktrace:Exception in thread "main" java.lang.IndexOutOfBoundsException
    at org.apache.xmlbeans.impl.store.Xobj.remove_attribute(Xobj.java:2287)
    at
org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl.unsetCollapsed(Unknown
Source)
    at
org.apache.poi.xssf.usermodel.XSSFSheet.expandRow(XSSFSheet.java:1870)
    at
org.apache.poi.xssf.usermodel.XSSFSheet.setRowGroupCollapsed(XSSFSheet.java:1780)
    at CreateExcel.fillData(CreateExcel.java:49)
    at CreateExcel.create(CreateExcel.java:22)

I'm using jdk1.6.0_25, 32 bit





See test below. There are several lines marked with 'natural order' or
'reverse order', you can switch grouping order by commenting/uncommenting
them

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;

public class CreateExcel {
    private static final int ROWS_NUMBER = 200;
    private static final int GROUP_SIZE = 5;

    private final String o_filename;
    private int o_groupsNumber = 0;

    public CreateExcel(String p_filename) {
        o_filename = p_filename;
    }

    public void create() {
        long startTime = System.currentTimeMillis();
        Workbook wb = new XSSFWorkbook();
        fillData(wb);
        writeToFile(wb);

        System.out.println("Number of groups: " + o_groupsNumber);
        System.out.println("Execution time: " +
(System.currentTimeMillis()-startTime) + " ms");
    }


    private void fillData(Workbook p_wb) {
        Sheet sheet = p_wb.createSheet("sheet123");
        sheet.setRowSumsBelow(false);

        for (int i = 0; i < ROWS_NUMBER; i++) {
            Row row = sheet.createRow(i);
            Cell cell = row.createCell(0);
            cell.setCellValue(i+1);
        }

        int i = 1;
        while (i < ROWS_NUMBER) {
            int end = i+(GROUP_SIZE-2);
            int start = i;                    // natural order
//            int start = end - 1;                // reverse order
            while (start < end) {             // natural order
//                while (start >= i) {            // reverse order
                sheet.groupRow(start, end);
                o_groupsNumber++;
                sheet.setRowGroupCollapsed(start, isCollapsed());
                start++;                      // natural order
//                start--;                        // reverse order
            }
            i += GROUP_SIZE;
        }
    }

    private boolean isCollapsed() {
        return Math.random() > 0.05d;
    }

    private void writeToFile(Workbook p_wb) {
        FileOutputStream fileOut = null;
        try {
            fileOut = new FileOutputStream(o_filename);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            p_wb.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (fileOut != null) {
                fileOut.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        CreateExcel createExcel = new CreateExcel("K://workbook.xlsx");
        createExcel.create();
    }
}



-- 
Best regards,
Michael Chadov

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message