//controller
@RequestMapping("/expectEntryInfo")
public void expectEntryInfo(String ids,HttpServletRequest request, HttpServletResponse response) throws Exception {List<SensitivePostRecord> list = new ArrayList<SensitivePostRecord>();
list = sensitivePostRecordService.expectEntryInfo(ids);
// 绘制excel
String fileName = new String(("敏感风险岗位人员_" + new SimpleDateFormat("yyyy-MM-dd-HH").format(new Date())).getBytes(),
"UTF-8");// 中文文件名必须使用此句话
String[] headers = { "申报年月", "姓名", "中软工号", "身份证号", "性别", "入职日期", "敏感岗位类型", "特殊权限类型", "入职华为日期",
"华为离职日期","原华为产品线","原华为最小部门","原华为部门主管","原华为职级","原华为工号","原竞争对手单位名称","原单位部门信息","原单位入职日期","原单位离职日期","配偶是否仍在竞争对手单位","是否管理员、PM","办公地点","入职供应商",
// "入职业务群",
"入职业务线","入职事业线","入职交付部","入职岗位/职务","申报备案日期","报备部门","创建者工号","创建者姓名","创建者时间","修改者工号","修改者姓名","修改者时间","备注"}; // 表格的标题栏
String titles = "敏感风险岗位人员清单备案";
Workbook wb = null;
wb = sensitivePostRecordService.exportExcelEntryInfo(titles, headers, list);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "utf-8") + ".xls");
OutputStream ouputStream = null;
try {
ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
} catch (IOException e) {
LoggerUtil.error(this.getClass(), "IO 异常" , e);
}finally {
ouputStream.close();
wb.close();
}
}
//serviceImpl
@Override
public List<SensitivePostRecord> expectEntryInfo(String ids) {
List<SensitivePostRecord> list = new ArrayList<SensitivePostRecord>();
if("".equals(ids) || null == ids){
list = sensitivePostRecordMapper.expectAllEntryPersonnel();
}else{
List<String> idList = StringTools.changeStringToList(ids);
list = sensitivePostRecordMapper.expectEntryPersonnel(idList);
}
return list;
}
@Override
public Workbook exportExcelEntryInfo(String titles, String[] headers, List<SensitivePostRecord> list) {
String fileType="xls";
// 声明一个工作薄
Workbook workbook = excelTools.getWrokBook(fileType);
// 生成一个表格
/* // 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);*/
// 生成一个样式
CellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(IndexedColors.TAN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
// 生成一个字体
Font font = workbook.createFont();
font.setColor(IndexedColors.VIOLET.getIndex());
font.setFontHeightInPoints((short) 12);
font.setBold(true);// 加粗
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
CellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.WHITE.index);
style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style2.setBorderBottom(BorderStyle.THIN);
style2.setBorderLeft(BorderStyle.THIN);
style2.setBorderRight(BorderStyle.THIN);
style2.setBorderTop(BorderStyle.THIN);
style2.setAlignment(HorizontalAlignment.CENTER);
style2.setVerticalAlignment(VerticalAlignment.CENTER);
// 生成另一个字体
Font font2 = workbook.createFont();
font2.setBold(false);// 正常
// 把字体应用到当前的样式
// style2.setFont(font2);
String[] keys = new String[]{"yearmonth", "name", "workNum", "idNum", "sex", "entryTime", "sensitivePostType","specialCompetenciesType","entryTimeHuawei",
"quitTimeHuawei", "deptBuHuawei", "deptOpduHuawei","deptSupervisorHuawei", "levelHuawei","numHuawei","companyNameOld","deptNameOld","entryTimeOld","quitTimeOld","ifSpouse","ifPm","address","entryCompany",
"entryDeptBu","entryDeptDu","entryDeptOpdu","entryPost","recordDate","recordDept","creatorId","creator","createTime","updatorId","updator","updateTime","remark"};
Workbook wb = excelTools.write(workbook, keys, titles, headers, list, style2, style);
return wb;
}
<div>
<button id="" class=" btn btn-nose" οnclick="expectEntryInfo();"><i class="fa fa-sign-out fa-rotate-270"></i>导出</button>
<button id="" class=" btn btn-nose" οnclick="expectAllEntryInfo();"><i class="fa fa-sign-out fa-rotate-270"></i>全部导出</button>
</div>
//导出
function expectEntryInfo(){
var ids = $('#sensitivePostId').jqGrid("getGridParam","selarrrow");
if(ids ==""){
zeroModal.error('没有选择要导出的信息,请重新选择');
return false;
}
location.href = basePath + 'sensitivePost/expectEntryInfo?ids='+ids.toString()+'&random=' + Math.random();
}
function expectAllEntryInfo(){
var yearMonth = $("#sensiTive_year").val()+'-'+$("#sensiTive_month").val();
location.href = basePath + 'sensitivePost/expectEntryInfo?random=' + Math.random();
}