Rest参数入口添加此参数 HttpServletResponse response
Workbook workbook = new XSSFWorkbook(); //region 兼容2003版本 /*SecurityUser securityUser = getSecurityUserInfo(); //此行代码报错 SysImporterItemEx sysImporterItemEx = sysImportItemImpl.selectByPrimaryKey(securityUser.getUserId(), itemId); if(sysImporterItemEx!=null&&sysImporterItemEx.getFileType().equals("xls")) { workbook=new HSSFWorkbook(); }*/ //endregion //region 获取mapping表,获取到列的排序 Iterator<Map.Entry<String, List<HashMap>>> hashMapTempIterator = hashMapTemp.entrySet().iterator(); while (hashMapTempIterator.hasNext()) { Map.Entry<String, List<HashMap>> entry = hashMapTempIterator.next(); String sheetName = entry.getKey(); // 生成一个表格 Sheet sheet; sheet = workbook.createSheet(sheetName); List<SysItemMappingDto> itemMappingDtos = new ArrayList<SysItemMappingDto>(); for (SysImporterItemDetailDo sysImporterItemDetailDo : detailDos) { if (sysImporterItemDetailDo.getSheetName().equals(sheetName)) { itemMappingDtos = itemMappingImpl.selectByDetailId(sysImporterItemDetailDo.getId()); } } // 数据行 int index = -1; //第一行添加英文列名或标题 Row row = sheet.createRow(++index); int cellIndex = -1; if (itemMappingDtos.size() <= 0) { Cell cell = row.createCell(0); cell.setCellValue("没有找到mapping表数据信息"); } for (SysItemMappingDto sysItemMappingDto : itemMappingDtos) { Cell cell = row.createCell(++cellIndex); cell.setCellValue(sysItemMappingDto.getSourceKey()); } row = sheet.createRow(++index); cellIndex = -1; //第二行中文列名或标题 for (SysItemMappingDto sysItemMappingDto : itemMappingDtos) { Cell cell = row.createCell(++cellIndex); cell.setCellValue(sysItemMappingDto.getSourceName()); } //第三行之后都是数据行 List<HashMap> list = entry.getValue(); for (HashMap hashmapTemp : list) { //增加行 row = sheet.createRow(++index); cellIndex = -1; for (SysItemMappingDto sysItemMappingDto : itemMappingDtos) { //添加列值 Cell cell = row.createCell(++cellIndex); String value = (String) hashmapTemp.get(sysItemMappingDto.getSourceKey()); cell.setCellValue(value); } } } //endregion //region 设置返回文件流 if (response != null) { //response.setContentType("application/vnd.ms-excel;charset=utf-8"); //response.setHeader("Content-Disposition", "attachment;filename="" + new String((itemId + ".xls").getBytes("gb2312"), "ISO8859-1")); OutputStream out = response.getOutputStream(); workbook.write(out); out.close(); response.setContentType("application/msdownload"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(itemId, "UTF-8")); }