日期:2014-05-20 浏览次数:21388 次
public void WriteExcel(FileOutputStream fOut,ResultSet rs) throws SQLException, IOException{
XSSFWorkbook workbook = new XSSFWorkbook();
// FileOutputStream fOut = new FileOutputStream(".\\" + args[0]
// + "\\" + filename + ".xlsx");
XSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0, "sheet");
XSSFRow row = sheet.createRow((short) 0);
XSSFCell cell;
ResultSetMetaData md = rs.getMetaData();
int nColumn = md.getColumnCount();
for (int i = 1; i <= nColumn; i++) {
cell = row.createCell((short) (i - 1));
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new XSSFRichTextString(md
.getColumnLabel(i)));
}
int iRow = 1;
while (rs.next()) {
row = sheet.createRow((short) iRow);
;
for (int j = 1; j <= nColumn; j++) {
cell = row.createCell((short) (j - 1));
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new XSSFRichTextString(rs
.getObject(j) + ""));
}
iRow++;
}
workbook.write(fOut);
fOut.flush();
fOut.close();
}