2012-05-25 12 views
18

私はテキストファイルから整数を読み込み、それらをクエリの入力として与え、クエリ出力を得てxlsファイルに書き込みます。無効な行番号(65536)許容範囲外(0..65535)

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の数字を持って取得しています。

+0

にあります - 正確なラインが例外をスローするかを示してください。 (メッセージだけでなく例外スタックトレースを出力してください) –

+0

エラーはどこで発生しますか? –

+0

私はcode.problemをデバッグしませんでした。私がcontacts.txtファイルに少ない数を与えているときです。私はこのエラーが出ません。私がcontacts.txtファイルの36000番号で実行しているときに無効な行番号エラー – Cindrella

答えて

30

HSSFは、最大65536行しかサポートしないExcelバージョン(Excel 2003)を対象としています。

代わりに新しいXSSF APIを使用できます。このAPIは、より多くの行の制限がある後のバージョンのExcelをサポートしています。

2つのAPI間の変換に役立つconversion guideがあります。

+0

私は多くの時間を持っていません...私はXSSF APIを使用してファイルをExcelに書き込む例を与えるplzすることができます – Cindrella

+0

はい私は新しい行を作成しています連絡先ごとに – Cindrella

+8

@Mohini - あなたは他の人たちがあなたに無料で手伝ってくれる時間があると思いますか?自分で努力する準備ができていないと、あなたはとても無礼な態度を取っているようです。 –

0

Excel(おそらく古いバージョンのみ)は、65535行しか許可しません。

ここでは、実際には65535行であるExcel 2003の制限に対してlinkがあります。 2010年には1,048,576に増加しました。

2

テキストファイルに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++; 
     }  

、実際のインデックス++でWHILEであること、それは新しい行ごとのために毎回作成することはありません原因:次のコードは、あなたの問題であるかのように見えますレコードセットのエントリ?あなたが整数になるint型の変数を変更する必要が

0
int person_org_id, external_person_org_id; 

、制限はあなたが私たちに多くのコードを与えてくれたプリミティブと-32767以上にすることはできませんから32767

+0

データ型の長所はintではなくその範囲を持っています。 2ビットの符号付き2の補数整数で、最小値は-2^31、最大値は2^31-1です。 Java SE 8以降では、int型のデータ型を使用して、最小値0および最大値2^32-1を持つ符号なし32ビット整数を表すことができます。 Integerクラスを使用して、intデータ型を符号なし整数として使用する – AtliB