这里是一个小demo!
int pageNum = 1;
int pageSize = 2;
int reportStatus = 0;
String infoNum = "";
//第一步,创建一个webbook,对应一个excel文件
HSSFWorkbook wb = new HSSFWorkbook();
//第二步,在webbook中添加一个sheet,对应excel文件中的sheet
HSSFSheet sheet = wb.createSheet("举报列表");
//第三步,在sheet中添加表头第0行,
HSSFRow row = sheet.createRow(0);
//第四步,创建单元格,并设置表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(); 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("案例id");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("标题");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("联系方式");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("举报内容");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("举报时间");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("案例状态");
cell.setCellStyle(style);
Map<String,Object> paramMap = new HashMap<String,Object>();
//第五步,写入实体数据,
List<FundraisingReportAndOperation> reportByPageList = manageFundraisingReportBiz.getReportByPage(pageNum, pageSize, infoNum, reportStatus);
FundraisingReportAndOperation r = null;
for (int i = 0; i < reportByPageList.size(); i++) {
row = sheet.createRow(i+1);
r=reportByPageList.get(i);
//创建单元格并设置值
row.createCell(0).setCellValue(r.getInfoNum());
row.createCell(1).setCellValue(r.getTitle());
row.createCell(2).setCellValue(r.getMobile());
row.createCell(3).setCellValue(r.getContent());
row.createCell(4).setCellValue(r.getCreateTime());
row.createCell(5).setCellValue(r.getReportStatusName());
}
//第六步将文件存到指定位置
Map<String, Object> retMap = new HashMap<String, Object>();
String resultStr = null;
File file=new File("/Users/apple/Downloads/举报信息.xls");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fout = null;
try {
fout = new FileOutputStream(file);
wb.write(fout);
fout.close();
System.out.println("导出成功");
} catch (IOException e1) {
e1.printStackTrace();
}