Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POI MySQL to Excel

Status
Not open for further replies.

Ciarrai

Technical User
Jun 27, 2003
17
US
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){}
 
Got it working taking out the first for loop...school boy error...Thanks everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top