Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid row number (65536) outside allowable range (0..65535)

Tags:

java

file-io

xls

I am reading integers from a text file, giving them as input to a query and getting the query output and writing to an xls file.

ResultSet rs;
Connection con = null;
PreparedStatement ps = null;
int person_org_id, external_person_org_id;
File f = null;
Scanner scan = null;

try {
    System.out.println("----------checkpoint-----------");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("----------checkpoint 1-----------");
    con = DriverManager.getConnection("jdbc:oracle:thin:@ksdjf.kjdlk.jkd.com:2222:edb", "aaaaa", "aaaa");
    System.out.println("----------checkpoint 2 ----------");
    if (con == null) {
        System.out.println("unable to connect to database");
    }
    System.out.println("----------checkpoint 3::connected to database---------");
    StringBuffer sql = new StringBuffer();
    sql.append("select abd from edb.abd where customer_id=510 and person_org_id =? ");
    ps = con.prepareStatement(sql.toString());

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Excel Sheet");
    HSSFRow rowhead = sheet.createRow(0);
    rowhead.createCell(0).setCellValue("ABC");
    rowhead.createCell(1).setCellValue("DEF");

    f = new File("/tmp/contacts.txt");
    scan = new Scanner(f);
    int index=1;

    while (scan.hasNextInt()) {

        person_org_id = scan.nextInt();

        ps.setInt(1,person_org_id);
        rs= ps.executeQuery();

        while (rs.next()) {                 

            external_person_org_id = rs.getInt(1);

            HSSFRow row = sheet.createRow(index);
            row.createCell(0).setCellValue(person_org_id);
            row.createCell(1).setCellValue(external_person_org_id);
            index++;
        }           

    }   
    FileOutputStream fileOut = new FileOutputStream(new File("/tmp/External_contact_id.xls"));
    wb.write(fileOut);
    fileOut.close();
    System.out.println("--------checkpoint 4:: writing data to xls completed------------");
}
catch (Exception e) {
    System.out.println(e.getMessage());
}

I am getting error Invalid row number (65536) outside allowable range (0..65535)

My contacts.txt file has around 36000 numbers.

like image 443
Cindrella Avatar asked May 25 '12 11:05

Cindrella


2 Answers

HSSF targets a version of Excel (Excel 2003) which only supports a maximum of 65536 rows.

You could try using the newer XSSF API instead, which supports later versions of Excel which have a more generous row limit.

There is a conversion guide which will help you convert between the two APIs.

like image 165
Simon Nickerson Avatar answered Sep 23 '22 23:09

Simon Nickerson


If you only have 36000 items in the text file, something else must be wrong.

Create a small sample, of let's say, 100 entries, and test with that.

Take a careful look at the resulting Excel file, if you can. It looks as if the following piece of code is your problem:

while(rs.next()){                   

        external_person_org_id = rs.getInt(1);

        HSSFRow row = sheet.createRow(index);
            row.createCell(0).setCellValue(person_org_id);
            row.createCell(1).setCellValue(external_person_org_id);
            index++;
        }       

I'm just guessing, but won't the fact that the index++ is in the WHILE cause it to create a new row each time for EVERY entry in a record set?

like image 41
Ewald Avatar answered Sep 26 '22 23:09

Ewald