我从一个文本文件中读取整数,将它们作为查询的输入,并获得查询输出并将其写入xls文件。
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());
}
我收到错误Invalid row number (65536) outside allowable range (0..65535)
我的contacts.txt
文件大约有36000个数字。
发布于 2012-05-25 19:31:22
HSSF针对的是最多支持65536行的Excel版本(Excel2003)。
您可以尝试使用较新的Excel,它支持更高版本的XSSF,后者具有更宽松的行限制。
有一个conversion guide可以帮助你在两个API之间进行转换。
发布于 2012-05-25 19:34:18
如果文本文件中只有36000个项目,那么一定有什么地方出了问题。
创建一个小样本,假设有100个条目,并对其进行测试。
如果可以,请仔细查看生成的Excel文件。看起来下面这段代码就是您的问题:
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++;
}
我只是猜测,index++在WHILE中的事实不会导致它每次都为记录集中的每个条目创建一个新行吗?
https://stackoverflow.com/questions/10753562
复制相似问题