controller层
@RequestMapping("/excelModify")
@ResponseBody
public void excelModify(@RequestParam("YearMonth")String YearMonth, HttpServletResponse response) throws IOException {
//生成一个列表信息
Map<String, Object> cond = new HashMap<String, Object>();
cond.put("querytime",YearMonth);
List<Achievements> list = achievementsService.achievementselect(cond);
HSSFWorkbook wb = achievementsService.export(list,YearMonth);
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
String name = YearMonth.split("-")[0]+"年"+YearMonth.split("-")[1]+"月"+"信息技术部绩效积分统计";
response.setHeader("Content-disposition", "attachment;filename=" + new String( name.getBytes("gb2312"), "ISO8859-1" ) + ".xlsx");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
service层
public HSSFWorkbook export(List<Achievements> list,String YearMonth) {
//定义表头
HSSFWorkbook wb = new HSSFWorkbook();
//生成一个工作表
HSSFSheet sheet = wb.createSheet("信息技术部绩效积分统计");
sheet.setColumnWidth(0, 2000);
sheet.setColumnWidth(1, 3766);
sheet.setColumnWidth(2, 3000);
sheet.setColumnWidth(3, 3766);
sheet.setColumnWidth(4, 3000);
sheet.setColumnWidth(5, 3766);
sheet.setColumnWidth(6, 3766);
sheet.setColumnWidth(7, 2766);
sheet.setColumnWidth(8, 1600);
sheet.setColumnWidth(9, 1200);
sheet.setColumnWidth(10, 1200);
sheet.setColumnWidth(11, 2766);
sheet.setColumnWidth(12, 3066);
sheet.setColumnWidth(13, 3566);
sheet.setColumnWidth(14, 3066);
sheet.setColumnWidth(15, 2066);
sheet.setColumnWidth(16, 1766);
sheet.setColumnWidth(17, 3266);
CellRangeAddress cra = new CellRangeAddress(0,0 , 0, 17);
sheet.addMergedRegion(cra);
//生成第一行
HSSFRow row = sheet.createRow(0);
//生成单元格的样式style
HSSFCellStyle style = wb.createCellStyle();
HSSFFont redFont = wb.createFont();
redFont.setFontHeightInPoints((short)20);
redFont.setBold(true);
style.setFont(redFont);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
row.setRowStyle(style);
row.setHeightInPoints(30);
HSSFCellStyle style1 = wb.createCellStyle();
HSSFFont redFont1 = wb.createFont();
redFont1.setBold(true);
redFont1.setFontHeightInPoints((short)11);
style1.setFont(redFont1);
style1.setAlignment(HorizontalAlignment.CENTER);
style1.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFCellStyle style2 = wb.createCellStyle();
HSSFFont redFont2 = wb.createFont();
//字体大小
redFont2.setFontHeightInPoints((short)10);
style2.setFont(redFont2);
//自动换行
style2.setWrapText(true);
//水平
style2.setAlignment(HorizontalAlignment.CENTER);
//垂直
style2.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFCellStyle style3 = wb.createCellStyle();
HSSFFont redFont3 = wb.createFont();
redFont3.setFontHeightInPoints((short)10);
style3.setFont(redFont3);
style3.setWrapText(true);
style3.setAlignment(HorizontalAlignment.LEFT);
style3.setVerticalAlignment(VerticalAlignment.TOP);
Cell cell = row.createCell(0);
cell.setCellValue(YearMonth.split("-")[0]+"年"+YearMonth.split("-")[1]+"月"+"信息技术部绩效积分统计");
cell.setCellStyle(style);
String[] titles = {"姓名","开发进度考核","进度权重","开发质量考核","质量权重","质量合格率","日常维护考核","维护权重","综合","奖","罚","工资计分","应上班天数","实际上班天数",
"岗位工资","绩效奖","级别","月考核等级"};
row = sheet.createRow(row.getRowNum() + 1);
for(int i=0;i<titles.length;i++){
Cell cell1 = row.createCell(i);
cell1.setCellValue(titles[i]);
cell1.setCellStyle(style1);
}
row.setHeightInPoints(20);
String remarks = "";
String createname = "";
String createtime = "";
String examine= "";
String examinetime= "";
String approval= "";
String approvaltime= "";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
for (int i = 0; i < list.size(); i++) {
//得到当前行数的下一行(row.getRowNum():得到当前行数)
row = sheet.createRow(row.getRowNum() + 1);
Achievements achievements = list.get(i);
//赋值
row.setRowStyle(style2);
row.createCell(0).setCellValue(achievements.getUsername());
row.getCell(0).setCellStyle(style2);
remarks = achievements.getRemarks();
createname = achievements.getCreatename();
createtime = sdf1.format(achievements.getCreatetime());
approval = achievements.getApproval();
approvaltime = sdf1.format(achievements.getApprovaltime());
if(achievements.getProgress()==null){
row.createCell(1).setCellValue("");
row.getCell(1).setCellStyle(style2);
}else {
row.createCell(1).setCellValue(achievements.getProgress());
row.getCell(1).setCellStyle(style2);
}
if(achievements.getWeight()==null){
row.createCell(2).setCellValue("");
row.getCell(2).setCellStyle(style2);
}else {
row.createCell(2).setCellValue(achievements.getWeight());
row.getCell(2).setCellStyle(style2);
}
if(achievements.getQuality()==null){
row.createCell(3).setCellValue("");
row.getCell(3).setCellStyle(style2);
}else {
row.createCell(3).setCellValue(achievements.getQuality());
row.getCell(3).setCellStyle(style2);
}
if(achievements.getQualityweight()==null){
row.createCell(4).setCellValue("");
row.getCell(4).setCellStyle(style2);
}else {
row.createCell(4).setCellValue(achievements.getQualityweight());
row.getCell(4).setCellStyle(style2);
}
if(achievements.getPassrate()==null){
row.createCell(5).setCellValue("");
row.getCell(5).setCellStyle(style2);
}else {
row.createCell(5).setCellValue(achievements.getPassrate());
row.getCell(5).setCellStyle(style2);
}
if(achievements.getMaintain()==null){
row.createCell(6).setCellValue("");
row.getCell(6).setCellStyle(style2);
}else {
row.createCell(6).setCellValue(achievements.getMaintain());
row.getCell(6).setCellStyle(style2);
}
if(achievements.getMaintainweight()==null){
row.createCell(7).setCellValue("");
row.getCell(7).setCellStyle(style2);
}else {
row.createCell(7).setCellValue(achievements.getMaintainweight());
row.getCell(7).setCellStyle(style2);
}
if(achievements.getSynthesis()==null){
row.createCell(8).setCellValue("");
row.getCell(8).setCellStyle(style2);
}else {
row.createCell(8).setCellValue(achievements.getSynthesis());
row.getCell(8).setCellStyle(style2);
}
if(achievements.getPrize()==null){
row.createCell(9).setCellValue("");
row.getCell(9).setCellStyle(style2);
}else {
row.createCell(9).setCellValue(achievements.getPrize());
row.getCell(9).setCellStyle(style2);
}
if(achievements.getPenalty()==null){
row.createCell(10).setCellValue("");
row.getCell(10).setCellStyle(style2);
}else {
row.createCell(10).setCellValue(achievements.getPenalty());
row.getCell(10).setCellStyle(style2);
}
if(achievements.getIntegral()==null){
row.createCell(11).setCellValue("");
row.getCell(11).setCellStyle(style2);
}else {
row.createCell(11).setCellValue(achievements.getIntegral());
row.getCell(11).setCellStyle(style2);
}
row.createCell(12).setCellValue(achievements.getShoulddays());
row.getCell(12).setCellStyle(style2);
row.createCell(13).setCellValue(achievements.getActualdays());
row.getCell(13).setCellStyle(style2);
row.createCell(14).setCellValue(achievements.getBasepay());
row.getCell(14).setCellStyle(style2);
if(achievements.getMeritspay()==null){
row.createCell(15).setCellValue("");
row.getCell(15).setCellStyle(style2);
}else {
row.createCell(15).setCellValue(achievements.getMeritspay());
row.getCell(15).setCellStyle(style2);
}
row.createCell(16).setCellValue(achievements.getCompetencylevel());
row.getCell(16).setCellStyle(style2);
row.createCell(17).setCellValue(achievements.getMonthlylevel());
row.getCell(17).setCellStyle(style2);
row.setRowStyle(style2);
}
CellRangeAddress cra1 = new CellRangeAddress(row.getRowNum() + 2,row.getRowNum() + 6,1,17);
sheet.addMergedRegion(cra1);
row = sheet.createRow(row.getRowNum() + 2);
Cell cell1 = row.createCell(0);
cell1.setCellValue("备注:");
cell1.setCellStyle(style1);
cell1 = row.createCell(1);
cell1.setCellValue(remarks);
cell1.setCellStyle(style3);
cra1 = new CellRangeAddress(row.getRowNum() + 7,row.getRowNum() + 7,2,4);
sheet.addMergedRegion(cra1);
cra1 = new CellRangeAddress(row.getRowNum() + 7,row.getRowNum() + 7,7,10);
sheet.addMergedRegion(cra1);
cra1 = new CellRangeAddress(row.getRowNum() + 7,row.getRowNum() + 7,15,17);
sheet.addMergedRegion(cra1);
row = sheet.createRow(row.getRowNum() + 7);
cell1 = row.createCell(1);
cell1.setCellValue("制表人:");
cell1.setCellStyle(style1);
cell1 = row.createCell(2);
cell1.setCellValue(createname);
cell1.setCellStyle(style2);
cell1 = row.createCell(6);
cell1.setCellValue("审核:");
cell1.setCellStyle(style1);
cell1 = row.createCell(7);
cell1.setCellValue(approval);
cell1.setCellStyle(style2);
cell1 = row.createCell(14);
cell1.setCellValue("批准:");
cell1.setCellStyle(style1);
cell1 = row.createCell(15);
cell1.setCellValue(examine);
cell1.setCellStyle(style2);
cra1 = new CellRangeAddress(row.getRowNum() + 1,row.getRowNum() + 1,2,4);
sheet.addMergedRegion(cra1);
cra1 = new CellRangeAddress(row.getRowNum() + 1,row.getRowNum() + 1,7,10);
sheet.addMergedRegion(cra1);
cra1 = new CellRangeAddress(row.getRowNum() + 1,row.getRowNum() + 1,15,17);
sheet.addMergedRegion(cra1);
row = sheet.createRow(row.getRowNum() + 1);
cell1 = row.createCell(2);
cell1.setCellValue(createtime);
cell1.setCellStyle(style2);
cell1 = row.createCell(7);
cell1.setCellValue(approvaltime);
cell1.setCellStyle(style2);
cell1 = row.createCell(15);
cell1.setCellValue(examinetime);
cell1.setCellStyle(style2);
return wb;
}