poi导出excel表

public void downloadAssementResult(EmpQuery query,HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response){
// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
response.reset();
// 获得国际化语言
RequestContext requestContext = new RequestContext(request);
String CourseCompany = requestContext.getMessage("assessment-Result");
response.setContentType("APPLICATION/vnd.ms-excel;charset=UTF-8");
// 注意,如果去掉下面一行代码中的attachment; 那么也会使IE自动打开文件。
response.setHeader(
"Content-Disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(
DateUtil.getExportDate() + ".xls", "UTF-8"));
OutputStream os = response.getOutputStream();// new
query.setEmployeeCode(CurrentUserUtil.getCurrentUserName());
// List<EmployeeAssessmentModel> list = employeeService.fetchAssessPage(query);
List<Map> list=employeeService.fetchAssessPage(query);
// 产生Excel表头
HSSFSheet sheet = workbook.createSheet(DateUtil.getExportDate()
+ CourseCompany);
sheet.setDefaultColumnWidth((short) 17);
// 创建属于上面Sheet的Row,参数0可以是0~65535之间的任何一个,
HSSFRow header = sheet.createRow(0); // 第0行
HSSFCellStyle style = workbook.createCellStyle();
// 设置样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

String employeeCode = requestContext.getMessage("employeeCode");
String employeeName = requestContext.getMessage("employeeName");
String assessTime = requestContext.getMessage("assessTime");
String state = requestContext.getMessage("state");
String employed=requestContext.getMessage("employed");
String unemployed=requestContext.getMessage("unemployed");
String Unaudited=requestContext.getMessage("Unaudited");
// 表头名称数组
String[] headerArr = new String[] { employeeCode, employeeName,
assessTime, state };
// 产生标题列
HSSFCell cell;
for (int i = 0; i < headerArr.length; i++) {
cell = header.createCell((short) i);
cell.setCellStyle(style);
cell.setCellValue(headerArr[i]);
}
// 迭代数据
if (list != null && list.size() > 0) {
int rowNum = 1;
for (Map history : list) {
HSSFRow row = sheet.createRow(rowNum++);
row.createCell((short) 0).setCellValue(
(String)history.get("employee_code"));

row.createCell((short) 1).setCellValue(
(String)history.get("name"));
row.createCell((short) 2).setCellValue(
(history.get("stage_time") != null ? history.get("stage_time").toString(): ""));

if (history.get("state")!=null){
String ss=history.get("state").toString();
}
if (history.get("state")!=null&&history.get("state").toString().equals("1")) {
row.createCell((short) 3).setCellValue(employed);
}else if(history.get("state")!=null&&history.get("state").toString().equals("2")) {
row.createCell((short) 3).setCellValue(unemployed);
}else{
row.createCell((short) 3).setCellValue(Unaudited);
}

}
}

workbook.write(os);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

posted @ 2018-01-04 15:57  半入江风半入云  阅读(224)  评论(0编辑  收藏  举报