Hi,
I'm trying to read from a table into an Excel spreadsheet. My loop is wrong but is there a more efficient way of doing this? Thanks.
try{
queryString = ("SELECT * from table ORDER BY time ASC");
// create a new workbook
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
HSSFRow r = null;
// declare a cell object reference
HSSFCell c = null;
short rownum;
// set the sheet name in Unicode
wb.setSheetName(0, "sheet");
rs = stmt.executeQuery(queryString);
int i=1;
while (rs.next())
{
for (rownum = (short) 0; rownum < 85; rownum++)
{
// create a row
r = s.createRow(rownum);
//rs.next();
for (short cellnum = (short) 0; cellnum < 6; cellnum ++)
{
//String cellValue;
c = r.createCell((short) (cellnum));
}
c.setCellValue( rs.getString(i) );
System.out.println(rs.getString(i));
i++;
}
}//end while
FileOutputStream fileOut = new FileOutputStream("C:\\documents and settings\\Desktop\\workbook.xls");
wb.write(fileOut);
fileOut.close();
}//end try
catch (SQLException e){}
I'm trying to read from a table into an Excel spreadsheet. My loop is wrong but is there a more efficient way of doing this? Thanks.
try{
queryString = ("SELECT * from table ORDER BY time ASC");
// create a new workbook
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
HSSFRow r = null;
// declare a cell object reference
HSSFCell c = null;
short rownum;
// set the sheet name in Unicode
wb.setSheetName(0, "sheet");
rs = stmt.executeQuery(queryString);
int i=1;
while (rs.next())
{
for (rownum = (short) 0; rownum < 85; rownum++)
{
// create a row
r = s.createRow(rownum);
//rs.next();
for (short cellnum = (short) 0; cellnum < 6; cellnum ++)
{
//String cellValue;
c = r.createCell((short) (cellnum));
}
c.setCellValue( rs.getString(i) );
System.out.println(rs.getString(i));
i++;
}
}//end while
FileOutputStream fileOut = new FileOutputStream("C:\\documents and settings\\Desktop\\workbook.xls");
wb.write(fileOut);
fileOut.close();
}//end try
catch (SQLException e){}