您的位置:首页 > 其它

POI的简单使用

2016-04-14 00:00 239 查看
摘要: 用POI实现从数据库中导出后缀为xls的excel表格

public class ServiceUtils {
public static void exportExcel(ServletOutputStream outputStream, List<User> userList) {
HSSFWorkbook workbook = null;
try {
//创建workbook
workbook = new HSSFWorkbook();
//创建sheet
HSSFSheet sheet = workbook.createSheet("用户列表");
//创建第1行
HSSFRow headRow = sheet.createRow(0);
//创建第1行单元格
HSSFCell headCell = headRow.createCell(0);
headCell.setCellValue("用户列表");
//合并第1行单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
//设置第1行单元格字体和格式
HSSFFont headFont = workbook.createFont();
headFont.setFontHeightInPoints((short) 16);
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
headFont.setFontName("宋体");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
cellStyle.setFont(headFont);
headCell.setCellStyle(cellStyle);

String[] titles = {"用户名", "账号", "所属部门", "性别", "手机号码", "电子邮箱", "生日"};
//创建第2行
HSSFRow secondRow = sheet.createRow(1);
//创建第2行的字体和格式
HSSFFont secondRowFont = workbook.createFont();
secondRowFont.setFontName("宋体");
secondRowFont.setFontHeightInPoints((short) 13);//13号字体
secondRowFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
HSSFCellStyle secondRowCellStyle = workbook.createCellStyle();
secondRowCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
secondRowCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
secondRowCellStyle.setFont(secondRowFont);
//为第2行每个单元格赋值
for(int i = 0; i < titles.length; i ++) {
//创建第2行单元格
HSSFCell secondRowCell = secondRow.createCell(i);
secondRowCell.setCellValue(titles[i]);
//设置第2行单元格字体和格式
secondRowCell.setCellStyle(secondRowCellStyle);
sheet.autoSizeColumn(i);//自动匹配单元格长度
}

//创建第3行及以后的字体和格式
HSSFFont iFont = workbook.createFont();
iFont.setFontName("宋体");
iFont.setFontHeightInPoints((short) 11);//11号字体
HSSFCellStyle iCellStyle = workbook.createCellStyle();
iCellStyle.setFont(iFont);
//动态生成第3行及以后的内容
for(int i = 0; i < userList.size(); i ++) {
//创建第i行
HSSFRow iRow = sheet.createRow(i + 2);
User user = userList.get(i);
String[] values = {
user.getUserName(), user.getAccount(),
user.getDept(), user.getGender(), user.getPhone(),
user.getEmail(), new SimpleDateFormat("yyyy-MM-dd").format(user.getBirthday())
};
//创建第i行第j单元格
for(int j = 0; j < values.length; j ++) {
HSSFCell iCell = iRow.createCell(j);
iCell.setCellValue(values[j]);
iCell.setCellStyle(iCellStyle);
sheet.autoSizeColumn(j);
}
}
workbook.write(outputStream);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if(workbook != null) {
try {
workbook.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

}
}

这上面的代码写在工具类中,上层调用的时候只需要设置好response的头和将response.getOutputStream传进来,那么就可以实现浏览器导出excel表格。

浏览器头的设置、

HttpServletResponse response= ServletActionContext.getResponse();
//设置MIME类型
response.setContentType("application/x-excel");
//设置处理方式,注意,此时设置的03版本的excel
response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("用户列表.xls", "UTF-8"));
//在service层处理文件
userService.exportExcel(response.getOutputStream());

还要注意判断从数据库导出的list集合是否为空。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: