1.java 批量导出pdf(将pdf压缩成zip文件导出)
@RequestMapping(value = "/batchExportPdf") public void batchExportPdf(HttpServletRequest request, ModelMap map, Long exportYear, String orgIds, String type,String tabType, HttpServletResponse response) throws IOException{ List<Map<String, Object>> fileList = new ArrayList<>(); String zipFileName=""; Long year = exportYear; String[] org_ids = orgIds.split(","); for (String orgId : org_ids) { BasOrg basOrg = basOrgService.findByOrgId(Long.valueOf(orgId)); File file = null; DocFileNew docFileNew = docFileNewService.findByFileTypeAndLeaseYearAndTabTypeAndOrgId(type,year.toString(),tabType,Long.valueOf(orgId)); if (docFileNew != null && !docFileNew.getIsUse().equals(SysContent.IS_USE_N)){// 判断文件是否存在 String filepath = docFileNew.getFilePath(); file = new File(filepath);// 获取模板文件 if (file.exists()) { // 取得文件名。 String filename = file.getName(); // 取得文件的后缀名。 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(filepath)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); if(filename.contains(".pdf")){ filename = year.toString() + "年度" + basOrg.getOrgName() + "批复函.pdf"; //重命名 }else if(filename.contains(".zip")){ filename = year.toString() + "年度" + basOrg.getOrgName() + "批复函.zip"; } Map<String, Object> fileMap = new HashMap<String, Object>(); fileMap.put("fileName", filename); fileMap.put("pdf", buffer); fileList.add(fileMap); } } } //zip压缩包名称 zipFileName = year.toString() + "年度批复函.zip"; request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-download"); zipFileName = URLEncoder.encode(zipFileName, "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + zipFileName); try (OutputStream os = response.getOutputStream()) { toPDFZip(fileList, os); } }
/** * 将批复函压缩成zip(导出pdf) * @param srcFiles * @param out */ public static void toPDFZip(List<Map<String, Object>> srcFiles, OutputStream out) { try (ZipOutputStream zos = new ZipOutputStream(out);) { // 最终压缩文件 for (Map<String, Object> mapFile : srcFiles) { String fileName = mapFile.get("fileName").toString(); byte[] buffer = (byte[]) mapFile.get("pdf"); ZipEntry entry = new ZipEntry(fileName); zos.putNextEntry(entry); zos.write(buffer); } zos.flush(); zos.close(); } catch (Exception e) { throw new RuntimeException("zip error", e); } }
2.java 批量导出excel(将excel压缩成zip文件导出)
/* * 批量下载excel文件 */ @RequestMapping(value = "/exportExcelBatch") public void exportExcelBatch(HttpServletRequest request, HttpServletResponse response,ModelMap map,String teamIds)throws Exception { String[] teamIdsArr= teamIds.split(","); Long[] convert = (Long[]) ConvertUtils.convert(teamIdsArr, Long.class); List<Long> teamIdsList = Arrays.asList(convert); // 生成所有的excel List<Map<String, Object>> fileList = new ArrayList<>(); for (Long teamId : teamIdsList) { List<Map> recMaps = new ArrayList<>(); ProjectTeamMain entity=projectteammainService.findProjectTeamMain(teamId); List<Map> lis =projectteamorgService.findByTeamIdAndIsUse(teamId,SysContent.IS_USE_Y,null); Integer allSum=0; Integer operateAll=0; Integer leaseAll=0; Integer wechatAll=0; Integer protectAll=0; if(lis.size()>0){ for(Map entityOrg:lis){ Map res=new HashMap(); //获取项目申报人和手机号 List<Map<String, Object>> list= projectteammainService.findReportPeople( Long.parseLong(entityOrg.get("TEAM_ID").toString()),Long.parseLong(entityOrg.get("ORG_ID").toString()),entityOrg.get("YEAR").toString()); if(list.size()>0){ res.put("PEOPLE",list.get(0).get("NAME")); res.put("PHONE", list.get(0).get("PHONE")); } res.put("ORG_NAME", entityOrg.get("ORG_NAME").toString()); if(entityOrg.get("OPERATESUM")!=null && !entityOrg.get("OPERATESUM").toString().equals("0")){ res.put("OPERATESUM", entityOrg.get("OPERATESUM").toString()); operateAll+=Integer.parseInt(entityOrg.get("OPERATESUM").toString()); } if(entityOrg.get("LEASESUM")!=null && !entityOrg.get("LEASESUM").toString().equals("0")){ res.put("LEASESUM", entityOrg.get("LEASESUM").toString()); leaseAll+=Integer.parseInt(entityOrg.get("LEASESUM").toString()); } if(entityOrg.get("WECHATSUM")!=null && !entityOrg.get("WECHATSUM").toString().equals("0")){ res.put("WECHATSUM", entityOrg.get("WECHATSUM").toString()); wechatAll+=Integer.parseInt(entityOrg.get("WECHATSUM").toString()); } if(entityOrg.get("PROTECTSUM")!=null && !entityOrg.get("PROTECTSUM").toString().equals("0")){ res.put("PROTECTSUM", entityOrg.get("PROTECTSUM").toString()); protectAll+=Integer.parseInt(entityOrg.get("PROTECTSUM").toString()); } res.put("REMARK", entityOrg.get("REMARK")); recMaps.add(res); } allSum=operateAll+leaseAll+wechatAll+protectAll; Map last=new HashMap(); last.put("ORG_NAME", "合计("+allSum+")"); if(operateAll!=0){ last.put("OPERATESUM",operateAll); } if(leaseAll!=0){ last.put("LEASESUM", leaseAll); } if(wechatAll!=0){ last.put("WECHATSUM", wechatAll); } if(protectAll!=0){ last.put("PROTECTSUM", protectAll); } recMaps.add(last); } SimpleDateFormat sdfF = new SimpleDateFormat( "yyyy-MM-dd" ); map.addAttribute("teamName", entity.getTeamName()); map.addAttribute("startTime", sdfF.format(entity.getStartTime())); map.addAttribute("projectType", lis.get(0).get("PROJECT_TYPE_TEXT_").toString()); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日 " ); String fileName = ""; //fileName +=sdf.format(entity.getStartTime()); //fileName += "专审项目分组明细表"; fileName +=entity.getTeamName(); fileName += ".xls"; map.addAttribute("recMaps", recMaps); map.put(JXLSExcelView.EXCEL_EXPORT_FILE_NAME, fileName); //导出文件名称 map.put(JXLSExcelView.EXCEL_TEMPLATE_FILE_NAME, "专审项目分组明细表.xls"); //excel模板名称 // 生成Excel文件 String templateName = "专审项目分组明细表.xls"; String srcFilePath = "/static/resources/excel/" + templateName; ServletContextResource resource = new ServletContextResource(request.getSession().getServletContext(),srcFilePath); XLSTransformer form = new XLSTransformer(); try (InputStream is = resource.getInputStream();) { Workbook workbook = form.transformXLS(is, map); Map<String, Object> fileMap = new HashMap<String, Object>(); fileMap.put("xls", workbook); fileMap.put("fileName", fileName); fileList.add(fileMap); } catch (Exception e) { logger.error("文件输入流异常", e); } } String filename = URLEncoder.encode("专审项目分组明细表.zip", "UTF-8"); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-download"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); try (OutputStream os = response.getOutputStream()) { toZip(fileList, os); } }
/** * 将excel压缩成zip文件导出 * * @param srcFiles * @param out */ public static void toZip(List<Map<String, Object>> srcFiles, OutputStream out) { try (ZipOutputStream zos = new ZipOutputStream(out);) { // 最终压缩文件 for (Map<String, Object> mapFile : srcFiles) { String fileName = mapFile.get("fileName").toString(); Workbook tplDoc = (Workbook) mapFile.get("xls"); ZipEntry entry = new ZipEntry(fileName); zos.putNextEntry(entry); tplDoc.write(zos); zos.closeEntry(); } } catch (Exception e) { throw new RuntimeException("zip error", e); } }
package com.ustc.core; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.view.AbstractView; public class JXLSExcelView extends AbstractView { public static final String EXCEL_EXPORT_FILE_NAME = "ExcelExportFileName";//导出的名字 public static final String EXCEL_TEMPLATE_FILE_NAME = "ExcelTemplateFileName";//模板名字 public static final String EXCEL_SHEET_NAMES = "ExcelSheetNames"; public static final String MUTIL_SHEET_DATA_KEY = "MutilSheetDataKey"; private static final String CONTENT_TYPE = "application/vnd.ms-excel"; private XLSTransformerExt transformer; public JXLSExcelView() { this.transformer = new XLSTransformerExt(); setContentType(JXLSExcelView.CONTENT_TYPE); } protected boolean generatesDownloadContent() { return true; } protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { String fileName = (String)model.get(JXLSExcelView.EXCEL_EXPORT_FILE_NAME); String templateName = (String)model.get(JXLSExcelView.EXCEL_TEMPLATE_FILE_NAME); List newSheetNames = (List)model.get(JXLSExcelView.EXCEL_SHEET_NAMES); response.setHeader("content-disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1")); String srcFilePath = "/static/resources/excel/" + templateName; System.out.println("srcFilePath="+srcFilePath); try(ServletOutputStream out = response.getOutputStream();){ if (newSheetNames == null) this.transformer.transformXLS(request.getSession().getServletContext(), srcFilePath, model, out); else { this.transformer.transformMultipleSheetsList(srcFilePath, (List)model.get(JXLSExcelView.MUTIL_SHEET_DATA_KEY), newSheetNames, out); } out.flush(); } } }
excel模板示例