使用POI技术导出Java对象到Excel中
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("数据表");
HSSFRow headRow = sheet.createRow(0);
String[] title = new String[] { "数据ID", "数据内容"};
for (int i = 0; i < title.length; i++) {
headRow.createCell(i).setCellValue(title[i]);
}
for (DataBean dataBean : dataBeanList) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(dataBean.getId());
dataRow.createCell(1).setCellValue(dataBean.getData());
}
sheet.autoSizeColumn(1, true);
//对象置空
dataBeanList=null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(fileNameString));
workbook.write(fos);
fos.flush();
} catch (Exception e) {
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {}
}
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {}
}
}