JAVA ZIP打包和POI导出 JAVAPOIZIP
程序员文章站
2024-03-24 16:41:22
...
/** * 导出Excel功能 * @param sql 获取数据要执行的SQL语句 * @param params SQL语句的参数 * @param column Excel表头,如果有扩展属性的话,可定义为:户号,户名,营业区域$ALL_BS_INFO * @param excelRowsLimit 没个Excel单元格的条数 * @param httpServletRequest * @return 改Excel的下载地址 * @throws Exception */ public String expExcel(String sql, List params, String[] column, int excelRowsLimit, HttpServletRequest httpServletRequest) throws Exception { DBTool dbTool = null; String downloadUrl = ""; Record[] rds = null; Record record = null; ZipOutputStream out = null; try { dbTool = new DBTool(); rds = dbTool.executeQuery(sql, params); // System.out.println("L: "+rds.length); int genExcelRowsLimit = excelRowsLimit <= 0 ? ServiceLocator.getInstance().getWebAppConfig().getGenExcelRowsLimit(): excelRowsLimit; String path = ServiceLocator.getInstance().getWebAppConfig().getExcelTempDirectory(); String zipPath = httpServletRequest.getSession().getId()+ System.currentTimeMillis() + ".zip"; downloadUrl = "http://" + httpServletRequest.getServerName() + ":"+ httpServletRequest.getServerPort() + httpServletRequest.getContextPath()+ "/GetTempFileServlet?fileName=" + zipPath; if (path == null) { path = httpServletRequest.getSession().getServletContext().getRealPath("download"); downloadUrl = "http://" + httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort()+ httpServletRequest.getContextPath() + "/download" + "/" + zipPath; } // System.out.println(path+System.getProperty("file.separator")+zipPath); File file = new File(path+System.getProperty("file.separator")+zipPath); if(!file.exists()){ file.createNewFile(); } out = new ZipOutputStream(new FileOutputStream(file)); DropBeanMap dbm = DropBeanMap.getInstance(); int fileCount = rds.length % genExcelRowsLimit == 0 ? rds.length/genExcelRowsLimit : rds.length / genExcelRowsLimit + 1; // System.out.println("fileCount "+fileCount); for (int i = 0; i < fileCount; i++) { int temp = i * genExcelRowsLimit; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet hs = wb.createSheet(); HSSFCellStyle style = wb.createCellStyle(); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short) 11); style.setFont(font); HSSFCellStyle style1 = wb.createCellStyle(); style1.setBorderBottom(HSSFCellStyle.BORDER_THIN); style1.setBorderLeft(HSSFCellStyle.BORDER_THIN); style1.setBorderRight(HSSFCellStyle.BORDER_THIN); style1.setBorderTop(HSSFCellStyle.BORDER_THIN); style1.setFillForegroundColor(HSSFColor.ORANGE.index); style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font1 = wb.createFont(); font1.setFontHeightInPoints((short) 15); font1.setBoldweight((short) 700); style1.setFont(font); HSSFCellStyle style2 = wb.createCellStyle(); style2.setWrapText(true); // 设置自动换行 HSSFRow hr = hs.createRow(0); for (int j = 0; j < column.length; j++) { // 设置表头 String value = ""; if (column[j].indexOf("$") < 0) { value = column[j]; } else { value = column[j].substring(0, column[j].indexOf("$")); } int len = value.length(); hs.setColumnWidth((short) j, (short) (len * 1600)); HSSFCell hc = hr.createCell((short) j); hc.setEncoding(HSSFCell.ENCODING_UTF_16); hc.setCellStyle(style1); hc.setCellValue(value); } int eachCount = genExcelRowsLimit; if(i==(fileCount-1) && rds.length % genExcelRowsLimit != 0 ){ eachCount = rds.length % genExcelRowsLimit-1; } // System.out.println("i: "+i+" eachCount: "+eachCount); for (int j = 0; j < eachCount; j++) { hr = hs.createRow(j + 1); // System.out.println("temp + j: "+(temp + j)); record = rds[temp + j]; for (int r = 0; r < column.length; r++) { String dropName = ""; String value = record.getString(r + 1) == null ? "": record.getString(r + 1).toString(); value = value.replaceAll(";alt", "'"); if (column[r].indexOf("$") >= 0) { dropName = column[r].substring(column[r].indexOf("$") + 1, column[r].length()); value = dbm.getDropLabel(dropName, value); } hr.setHeight((short) 500); HSSFCell hc = hr.createCell((short) r); hc.setCellStyle(style); hc.setEncoding(HSSFCell.ENCODING_UTF_16); hc.setCellValue(value); hc.setCellStyle(style2); } } out.putNextEntry(new ZipEntry(i+".xls")); wb.write(out); } out.close(); } catch (Exception e) { e.printStackTrace(); throw new highsoft.framework.exceptions.BaseRunException( "导出excel出错!", e); } finally { if (dbTool != null) { dbTool.closeConnection(); dbTool = null; } if(null != out){ try{ out.flush(); out.close(); out = null; } catch(IOException e){}; } } return downloadUrl; } /** * 遍历目录并添加文件. * @param jos - JAR 输出流 * @param file - 目录文件名 * @param pathName - ZIP中的目录名 * @throws IOException * @throws FileNotFoundException */ private static void recurseFiles(ZipOutputStream jos, File file, String pathName) throws IOException, FileNotFoundException { byte buf[] = new byte[1024]; if (file.isDirectory()) { pathName = pathName + file.getName() + "/"; jos.putNextEntry(new ZipEntry(pathName)); String fileNames[] = file.list(); if (fileNames != null) { for (int i = 0; i < fileNames.length; i++) recurseFiles(jos, new File(file, fileNames[i]), pathName); } } else { ZipEntry jarEntry = new ZipEntry(pathName + file.getName()); // System.out.println(pathName + " " + file.getName()); FileInputStream fin = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fin); jos.putNextEntry(jarEntry); int len; while ((len = in.read(buf)) >= 0) jos.write(buf, 0, len); in.close(); jos.closeEntry(); } } /** * 创建 ZIP/JAR 文件. * * @param directory - * 要添加的目录 * @param zipFile - * 保存的 ZIP 文件名 * @param zipFolderName - * ZIP 中的路径名 * @param level - * 压缩级别(0~9) * @throws IOException * @throws FileNotFoundException */ public static void makeDirectoryToZip(File directory, File zipFile, String zipFolderName, int level) throws IOException, FileNotFoundException { level = (level < 0 || level > 9)? 7 : level; if (zipFolderName == null) { zipFolderName = ""; } ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(zipFile)); jos.setLevel(level); String fileNames[] = directory.list(); if (fileNames != null) { for (int i = 0; i < fileNames.length; i++) recurseFiles(jos, new File(directory, fileNames[i]),zipFolderName); } jos.close(); }
上一篇: MyBatis | 一级缓存与二级缓存
下一篇: SpringBoot分页的插件