enemyofnone
enemyofnone

Reputation: 85

Result set to excel ignoring 1st row

I am trying to execute queries in java and export to excel

Code :

HSSFSheet localHSSFSheet = localHSSFWorkbook.createSheet(ExcelSheetName);
localHSSFSheet.autoSizeColumn((int) 0, true);
HSSFRow localHSSFRow = localHSSFSheet.createRow(0);  
ResultSetMetaData localResultSetMetaData = localResultSet.getMetaData();
System.out.println(localResultSet.next());

System.out.println(i);
for (int k = 0; k < localResultSetMetaData.getColumnCount(); k++){
 // localHSSFRow.setRowStyle(style)
     localHSSFRow.createCell((int)k).setCellValue(
         localResultSetMetaData.getColumnLabel(k + 1));
}
ResultSet localResultSet1 = localStatement1.executeQuery(sql);
ResultSetMetaData localResultSetMetaData1 = localResultSet1.getMetaData();
System.out.println(localResultSet1.next());

for (int d = 0; d < localResultSetMetaData1.getColumnCount(); d++)
    localHSSFRow.createCell((int)d).setCellValue(
         localResultSetMetaData1.getColumnLabel(d+1));
for (int d = 1; localResultSet1.next(); d++){
    localHSSFRow = localHSSFSheet.createRow((int)d);
    for (int j = 0; j <= localResultSetMetaData.getColumnCount(); j++)
         localHSSFRow.createCell((int)j).setCellValue(
               localResultSet1.getString(j+1));
}

But the above code is simply ignoring the first record (first row) and printing the rest in excel.

What i am doing wrong?

Upvotes: 0

Views: 168

Answers (1)

ljgw
ljgw

Reputation: 2771

I suspect that your debugging statements (specifically System.out.println(localResultSet1.next());) are to blame: they advance your localResultSet1 from the starting position to the second position (i.e. the second row), and you start writing to excel from there..

Upvotes: 4

Related Questions