@RequestMapping(value = "/exportExcel")
@ResponseBody
public Object exportExcel(DeviceInVo vo,HttpServletResponse response) {
logger.info("执行导出excel文件: DeviceInController.exportExcel()");
try {
DeviceInStorageExample example = new DeviceInStorageExample();
DeviceInStorageExample.Criteria criteria = example.createCriteria();
StUser user = (StUser)ContextHolderUtils.getSession().getAttribute(Constant.USER_SESSION);
String chanId = user.getChanId();
if(user!=null && !"01".equals(chanId)){
List<String> sameDeptUserList = this.stDeptServiceImpl.selectSameDeptUser(chanId);
criteria.andOperatorIn(sameDeptUserList);
}
if(StringUtils.isNotBlank(vo.getBeginDate()) && StringUtils.isNotBlank(vo.getEndDate())){
if(vo.getBeginDate().equals(vo.getEndDate())){
String endDate = DateUtil.getDateOfDay(vo.getBeginDate(), 1, "yyyy-MM-dd");
vo.setEndDate(endDate);
}
}
if(StringUtils.isNotBlank(vo.getBeginDate())){
criteria.andInDateGreaterThan(DateUtil.convertDate(vo.getBeginDate()));
}
if(StringUtils.isNotBlank(vo.getEndDate())){
criteria.andInDateLessThan(DateUtil.convertDate(vo.getEndDate()));
}
if(!StringUtil.isEmpty(vo.getSort())){
example.setOrderByClause(vo.getOrderByClause());
}
if(!StringUtils.isEmpty(vo.getDeviceSn())){
criteria.andDeviceSnEqualTo(vo.getDeviceSn());
}
if(!StringUtil.isEmpty(vo.getStatus())){
criteria.andStatusEqualTo(vo.getStatus());
}
//criteria.andStatusEqualTo("1");
List<Object> list = deviceInService.exportExcel(example);
if(list==null ||list.size()==0){
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("无数据可导出,请改变查询条件后再试");
return null;
}
if(list.size()>50000){
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("最多一次只能导出50000条数据,您查询到的实际条数为"+list.size()+"。请缩小查询条件后再试");
return null;
}
//将需要导出的字段的显示值进行转换,需要导出什么字段需在headermap.properties中配置
list = stDictionaryService.transField(transition, list);//部分字段数据字典转换
PoiUtil.exportFile("deviceInStorage", list, excelTitle.split(","),response);//使用模板
} catch (Exception e) {
logger.error("导出excel文件出现异常:", e);
}
return null;
}
private String transition= "DEVICE_TYPE|DEVICE_SOURCE|OUT_STATUS|MER_STATUS";
private String excelTitle= "设备SN|DEVICE_SN,设备型号|DEVICE_TYPE,设备状态|OUT_STATUS,设备来源|DEVICE_SOURCE,入库时间|IN_DATE,出库时间|OUT_DATE,所属商户|MER_NAME,所属分行|DEPT_NAME,商户状态|MER_STATUS,更新时间|AUDITCREATEDATE";
@Override
public List<Object> transField(String transition, List<Object> list){
if (!StringUtils.isBlank(transition) && list != null && list.size() > 0) {
// transition的格式:status|dictionary|order_status
String[] strs = StringUtils.split(transition, ",");
for (String str : strs) {
String[] args = StringUtils.split(str, "|");
if(!StringUtils.isBlank(args[0])&& !StringUtils.isBlank(args[2])) {
//查询字典表
List<StDictionary> dictionarys = this.selectDicItems(args[2]);
String dicCode = null;
for(Object object : list) {
Map<String,String> map = (Map<String,String>)object;
//获取对象中的属性的值
dicCode = map.get(args[0]) != null ? String.valueOf(map.get(args[0])) : "";
for(StDictionary dictionary : dictionarys) {
//根据对象中属性值和数据字典中的字典代码的对应关系,得到字典值,并放到map中
if(dicCode.equals(dictionary.getDicCode())) {
map.put(args[0], dictionary.getDicValue());
}
}
}
}
}
}
return list;
}
public static void exportFile(String fileName, List<Object> reportList, String tableHeader[],
HttpServletResponse response) throws Exception {
OutputStream os = null;
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-disposition", "attachment" + ";filename=" + FileUtil.getFileNameByBrowser(fileName)
+ ".xls");
os = response.getOutputStream();
HSSFWorkbook wb = new HSSFWorkbook();
try {
createExcelSheet(wb, tableHeader, reportList);
wb.write(os);
os.flush();
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
os.close();
}
}
@SuppressWarnings("unchecked")
public static void createExcelSheet(HSSFWorkbook wb, String[] tableHeader, List<Object> reportList)
throws Exception {
HSSFSheet sheet = wb.createSheet(EXCEL_DEFAULT_SHEET_NAME);
HSSFRow headerRow = sheet.createRow((short) 0);
int cellNumber = tableHeader.length;
HSSFCellStyle style = getExcelStyle(wb);
if (reportList.size() > MAX_RECORD) {
HSSFCell headerCell = headerRow.createCell(0);
headerCell.setCellStyle(style);
headerCell.setCellValue(STR_WARN);
sheet.setColumnWidth(0, 6800);
} else {
/**
* 存放类型MAP
*/
List<String> Cell_style = new ArrayList<String>();
Map<String, String> fileTitleMap = getFileTitleMap(tableHeader, Cell_style);
String fileTitleArray[] = new String[cellNumber];
Iterator<String> it = fileTitleMap.keySet().iterator();
int index = 0;
while (it.hasNext()) {
fileTitleArray[index++] = it.next();
}
for (int i = 0; i < cellNumber; i++) {
HSSFCell headerCell = headerRow.createCell(i);
headerCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
headerCell.setCellValue(String.valueOf(fileTitleMap.get(tableHeader[i].split("\\|")[1])));
headerCell.setCellStyle(style);
headerCell.setCellComment(getComment(sheet, "请设置所有单元格为为文本类型。设置单元格格式-->文本-->确定"));
sheet.setColumnWidth(i, 6800);
}
// HSSFCellStyle contentStyle = getExcelContentStyle(wb);
for (int j = 0; j < reportList.size(); j++) {
HSSFRow dataRow = sheet.createRow(j + 1);
Map<String, String> map = (Map<String, String>) reportList.get(j);
HSSFCell contentCell = null;
for (int t = 0; t < cellNumber; t++) {
contentCell = dataRow.createCell(t);
// 实现类型
if ("number".equals(Cell_style.get(t))) {
Object obj = map.get(tableHeader[t].split("\\|")[1]);
if (obj instanceof BigDecimal) {
contentCell.setCellValue(((BigDecimal) obj).doubleValue());
} else {
contentCell.setCellValue(Double.valueOf((String) obj));
}
} else if ("boolean".equals(Cell_style.get(t))) {
contentCell.setCellValue(Boolean.valueOf(map.get(tableHeader[t].split("\\|")[1])));
} else if ("date".equals(Cell_style.get(t))) {
contentCell.setCellValue(Date.valueOf(map.get(tableHeader[t].split("\\|")[1])));
} else {
contentCell.setCellValue(String.valueOf(getString(map.get(tableHeader[t].split("\\|")[1]))));
}
// added by fanzhen 2011-8-2设置单元格类型为文本。方便用户输入
// MODIFY BY JFAN 2012-6-20 根据LIST的值修改单元格类型
// HSSFCellStyle contentStyle =
// getExcelContentStyle(wb,t,Cell_style);
// contentCell.setCellStyle(contentStyle);
// contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
}
}
}
}