0
votes

I am trying to write the output of a complex query into Excel. For this I am using JDBC and created a function of return type 'ResultSet' through which I'll be getting the output of the Query.

To write the ResultSet Rows into excel, I should do like below:

        HSSFRow row = firstSheet.createRow(index);
    row.createCell(0).setCellValue(rs.getInt(1));
    row.createCell(1).setCellValue(rs.getInt(2));
    row.createCell(2).setCellValue(rs.getString(2));
    row.createCell(3).setCellValue(rs.getString(3));

Now The issue is, the resultset of the complex query includes variable number of rows and columns depending upon the date we run the query which need to be exported to excel. Is there any feasible approach through programming I can handle this situation and export the query result into an excel.

Thanks in advance.

1

1 Answers

2
votes

You should use ResultSet.getMetaData() method and, for example, iterate for ResultSetMetaData.getColumnCount() times.