您的位置:首页 > 其它

使用poi导出xls

2015-11-18 15:18 288 查看
代码:

@RequestMapping("exportXls")
public void exportXls(
@ModelAttribute("operationReportVo") OperationReportVo operationReportVo,
HttpServletRequest req, HttpServletResponse resp, HttpSession session) throws FileNotFoundException, IOException, ParseException {
String roles = getRoles(operationReportVo.getRoles());
Integer locationId = this.getCurrentUser(req).getLocationId();
if (operationReportVo.getLocationIds() != null &&
operationReportVo.getLocationIds().length > 0) {
locationId = operationReportVo.getLocationIds()[0];
}
Date endTime = sdf.parse(sdfDay.format(operationReportVo.getEndTime()) + " 23:59:59");
List<UserBehaviorVo> userBehaviorVos = userBehaviorService
.getUserBehaviorVo(null, operationReportVo.getStartTime(),
endTime, locationId, roles);

String path = session.getServletContext().getRealPath("/") + "/WEB-INF/ppt_template/usetimes.xlsx";
XSSFWorkbook workBook = new XSSFWorkbook(new FileInputStream(path));
XSSFSheet sheet = workBook.getSheetAt(0);
XSSFCellStyle style = sheet.getWorkbook().createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);

for (int it = 0; it < userBehaviorVos.size(); it++) {
XSSFRow row = sheet.createRow(it + 1);
// 每一行创建12列
for (int cellIndex = 0; cellIndex < 16; cellIndex++) {
XSSFCell cell = row.createCell(cellIndex);
cell.setCellStyle(style);
}
int rowNum = it + 1;
sheet.getRow(rowNum).getCell(0).setCellValue(userBehaviorVos.get(it).getMobileNo());
sheet.getRow(rowNum).getCell(1).setCellValue(userBehaviorVos.get(it).getRealName());
sheet.getRow(rowNum).getCell(2).setCellValue(userBehaviorVos.get(it).getSex());
sheet.getRow(rowNum).getCell(3).setCellValue(userBehaviorVos.get(it).getRole());
sheet.getRow(rowNum).getCell(4).setCellValue(userBehaviorVos.get(it).getArea());
Integer loginTimes = userBehaviorVos.get(it).getLoginTimes();
String isLogin = loginTimes > 0 ? "是" : "否";
sheet.getRow(rowNum).getCell(5).setCellValue(isLogin);
sheet.getRow(rowNum).getCell(6).setCellValue(loginTimes);
sheet.getRow(rowNum).getCell(7).setCellValue(userBehaviorVos.get(it).getWorkLogTimes());
sheet.getRow(rowNum).getCell(8).setCellValue(userBehaviorVos.get(it).getMessageTimes());
sheet.getRow(rowNum).getCell(9).setCellValue(userBehaviorVos.get(it).getNoticeTimes());
sheet.getRow(rowNum).getCell(10).setCellValue(userBehaviorVos.get(it).getAssignmentTimes());

sheet.getRow(rowNum).getCell(11).setCellValue(userBehaviorVos.get(it).getAcceptAssignmentTimes());
sheet.getRow(rowNum).getCell(12).setCellValue(userBehaviorVos.get(it).getCommitAssignmentTimes());
sheet.getRow(rowNum).getCell(13).setCellValue(userBehaviorVos.get(it).getServerFarmers());
sheet.getRow(rowNum).getCell(14).setCellValue(userBehaviorVos.get(it).getScores());

sheet.getRow(rowNum).getCell(15).setCellValue(userBehaviorVos.get(it).getTotalTimes());
}
// 2设置文件名
String fileName = URLEncoder.encode("用户使用情况统计", "UTF-8");
resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ");
resp.setHeader("Content-Disposition", "attachment;" + " filename=" + fileName + ".xlsx");
workBook.write(resp.getOutputStream());
}

其中PPT模板在/WEB-INF/ppt_template/usetimes.xlsx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: