1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
| @Override public void collectExcelExport(List title,String isAllAgy, BipSchePlan bip, Integer fiscal, HttpServletResponse response, HttpServletRequest request) { try { // 将excel导出的文件位置 String filePath = "C:"+File.separator+"Users"+File.separator+"Public" + File.separator+"Downloads" + File.separator + "tyzj"; //创建临时文件夹 createFile(filePath); //压缩导出 excelExport(title,filePath,isAllAgy,bip,fiscal,response,request); try { /* delAllFile(filePath); // 删除完里面所有内容 filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); // 删除空文件夹 System.out.println("缓存文件删除完毕");*/ delFolder(filePath); System.out.println("缓存文件删除完毕"); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { }
} /** * 导出 */ private void excelExport(List title,String filePath,String isAllAgy, BipSchePlan bip, Integer fiscal, HttpServletResponse response, HttpServletRequest request) throws IOException { //年度 String finalFundTypeName = ""; if("1".equals(bip.getFundType())){ finalFundTypeName="当年"; } if("0".equals(bip.getFundType())){ finalFundTypeName="上年"; } //生成excel List<String> fileNames =createExcel(title,isAllAgy,fiscal,bip); //导出压缩文件的全路径 List<BipSchePlan> exportAgyName = new ArrayList<>(); if(!StringUtils.isEmpty(bip.getFiscal()) && !StringUtils.isEmpty(bip.getAgyCode())){ exportAgyName=budSchePlanDao.selExportAgy(bip); } //zip处理 String saveFileZipName = ""; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); saveFileZipName += format.format(Calendar.getInstance().getTime()) +exportAgyName.get(0).getAgyName()+finalFundTypeName+"预算执行计划表"+ ".zip"; String zipFilePath = filePath+File.separator+saveFileZipName; //导出zip File zip = new File(zipFilePath); //将excel文件生成压缩文件 File srcfile[] = new File[fileNames.size()]; for (int j = 0, n1 = fileNames.size(); j < n1; j++) { srcfile[j] = new File(fileNames.get(j)); } ZipFiles(srcfile, zip); System.out.println("压缩文件生成目录:"+zipFilePath); System.out.println("压缩文件生成完毕"); String url = ""; url = URLEncoder.encode(zip.getName(), "UTF8"); response.setContentType("application/zip"); response.setHeader("Location", url); response.setHeader("Content-Disposition", "attachment; filename=" + url); OutputStream outputStream = response.getOutputStream(); InputStream inputStream = new FileInputStream(zipFilePath); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } outputStream.flush(); outputStream.close(); inputStream.close(); System.out.println("导出完毕"); } /** * 生成excel */ private List<String> createExcel(List title,String isAllAgy,Integer fiscal,BipSchePlan bip){ // 用于存放生成的excel文件名称 List<String> fileNames = new ArrayList<String>(); if(StringUtils.isEmpty(bip.getFundType()) || bip.getFundType()==""){ bip.setFundType("1"); List<String> lastExcel = createExcelByFiscal(title,isAllAgy,fiscal,bip); bip.setFundType("0"); List<String> excel = createExcelByFiscal(title,isAllAgy,fiscal,bip); fileNames.addAll(lastExcel); fileNames.addAll(excel); return fileNames; } else{ List<String> excel = createExcelByFiscal(title,isAllAgy,fiscal,bip); fileNames.addAll(excel); return fileNames; } } /** * 分年度createExcel */ private List<String> createExcelByFiscal(List title,String isAllAgy,Integer fiscal,BipSchePlan bip){ //输入流 InputStream in = null; //输出流 FileOutputStream o = null; // 用于存放生成的excel文件名称 List<String> fileNames = new ArrayList<String>(); // 导出Excel文件路径 String fullFilePath = ""; //年度 String fundTypeName = ""; if("1".equals(bip.getFundType())){ fundTypeName="当年"; } if("0".equals(bip.getFundType())){ fundTypeName="上年"; } //如果是单位的就导出单位的数据 List<BipSchePlan> bips =budSchePlanDao.selectExcel(bip); if(bips.size()>0){ bips.stream().forEach(b->{ if("人员经费".equals(b.getProjectName()) || "公用经费".equals(b.getProjectName())){ b.setProjectName(""); } if("99901".equals(b.getProjectCode()) || "99902".equals(b.getProjectCode())){ b.setProjectCode(""); } }); JSONArray json = JSONArray.fromObject(bips); List<Map<String, Object>> list = json; //单位的 List<Map<String, Object>> collectDanWei = list.stream().filter(ee -> StringUtils.isEmpty(ee.get("departmentCode"))).collect(Collectors.toList()); //协会 Map<String, List<Map<String, Object>>> listValue = list.stream().filter(ee -> !StringUtils.isEmpty(ee.get("departmentCode"))) .collect(Collectors.groupingBy(e -> e.get("departmentCode").toString())); //总体 listValue.put("agy", collectDanWei); //年度判断 String finalFundTypeName = fundTypeName; listValue.forEach((agy, col) -> { FileOutputStream os = null; try { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("导出excel"); // 第一行 HSSFRow row = sheet.createRow(0); HSSFRow row1 = sheet.createRow(1); HSSFRow row2 = sheet.createRow(2); CellStyle style = CatalogExcelUtil.getHeadStyle(wb); HSSFFont font = wb.createFont(); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 font.setFontName("新宋体"); font.setColor(HSSFColor.RED.index); font.setBoldweight((short) 10); //1.生成字体对象 HSSFCellStyle cellFontStyle = wb.createCellStyle(); cellFontStyle.setFont(font);
HSSFCellStyle setFont2 = wb.createCellStyle(); HSSFFont font2 = wb.createFont(); font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 font2.setFontHeightInPoints((short) 18);
HSSFFont font3 = wb.createFont(); font3.setFontName("仿宋_GB2312"); font3.setFontHeightInPoints((short) 18);
HSSFCellStyle cellStyleRight = wb.createCellStyle(); cellStyleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//right cellStyleRight.setFont(font2);
HSSFCellStyle cellStyleRight_wy = wb.createCellStyle(); cellStyleRight_wy.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//right
HSSFCellStyle cellStyleLeft = wb.createCellStyle(); cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);//left cellStyleLeft.setFont(font2);
HSSFCellStyle cellStyleLeft_fj = wb.createCellStyle(); cellStyleLeft_fj.setAlignment(HSSFCellStyle.ALIGN_LEFT);//left cellStyleLeft_fj.setFont(font3);
HSSFCellStyle cellStyleLeft_hj = wb.createCellStyle(); cellStyleLeft_hj.setAlignment(HSSFCellStyle.ALIGN_LEFT);//left
HSSFCellStyle cellStyleLeft_hj_align = wb.createCellStyle(); cellStyleLeft_hj_align.setAlignment(HSSFCellStyle.ALIGN_LEFT);//left cellStyleLeft_hj_align.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 cellStyleLeft_hj_align.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 cellStyleLeft_hj_align.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 cellStyleLeft_hj_align.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 cellStyle.setFont(font2);
HSSFCellStyle setBorder = wb.createCellStyle(); setBorder.setWrapText(true);//设置自动换行 setBorder.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, title.size() - 1)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, title.size() - 1)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 5)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 30, 31)); HSSFCell cell = row.createCell(0); cell.setCellValue("附件"); cell.setCellStyle(cellStyleLeft_fj); HSSFCell cell3 = row1.createCell(0); cell3.setCellValue("国家体育总局及直属事业单位(协会)" + fiscal + finalFundTypeName + "预算执行计划表(含彩票公益金)"); cell3.setCellStyle(cellStyle); String cell4Name = ""; if (StringUtils.isEmpty(col.get(0).get("departmentCode"))) { cell4Name = col.get(0).get("agyName").toString(); } else { cell4Name = col.get(0).get("departmentName").toString(); } HSSFCell cell4 = row2.createCell(0); cell4.setCellValue("单位:" + cell4Name); cell4.setCellStyle(cellStyleLeft); HSSFCell cell4_last = row2.createCell(30); cell4_last.setCellValue("单位:万元"); cell4_last.setCellStyle(cellStyleRight_wy); sheet.setColumnWidth(0,10*256); sheet.setColumnWidth(1,30*256); sheet.setColumnWidth(2,22*256); sheet.setColumnWidth(3,20*256); sheet.setColumnWidth(4,8*256); sheet.setColumnWidth(5,20*256); sheet.setColumnWidth(6,20*256); HSSFRow rows = sheet.createRow(3); HSSFCellStyle canEditStyle = wb.createCellStyle(); canEditStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 canEditStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 canEditStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 canEditStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 canEditStyle.setLocked(true); //设置列的锁定状态为锁定 HSSFCellStyle sumCellStyle = wb.createCellStyle(); sumCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 sumCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 sumCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 sumCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 //创建行4 HSSFRow sumAmtRow = sheet.createRow(4); sumAmtRow.setRowStyle(cellStyleLeft_hj); HSSFCell sumAmtCell5 = sumAmtRow.createCell(6); sumAmtCell5.setCellValue("合计"); sumAmtCell5.setCellStyle(cellStyleLeft_hj_align); //新建集合用来map转bean放入List计算合计 List<BipSchePlan> lsToBean = new ArrayList<>(); col.forEach(k->{ BipSchePlan b=new BipSchePlan(); transMap2Bean2(k,b); lsToBean.add(b); } ); //合计方法 BipSchePlan sumBip = sumMonthSchedule(lsToBean); //设定 sumPlanAmt(sumBip,sumAmtRow,sumCellStyle); HSSFCellStyle noCanEdit = wb.createCellStyle(); noCanEdit.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框 noCanEdit.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框 noCanEdit.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框 noCanEdit.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框 noCanEdit.setLocked(false); //设置列的锁定状态为锁定 int num = 5; for (int i = 0; i < col.size(); i++) { HSSFRow row4 = sheet.createRow(num++); for (int a = 0; a < title.size(); a++) { HSSFCell rowsCell = rows.createCell(a); rowsCell.setCellStyle(setBorder); rowsCell.setCellValue(((LinkedHashMap) title.get(a)).get("desc").toString()); LinkedHashMap object = (LinkedHashMap) title.get(a); String object2 = object.get("value").toString(); HSSFCell createCell = row4.createCell(a); if (a < 8) { createCell.setCellStyle(canEditStyle); } if (a >= 8 && a < 32) { createCell.setCellStyle(noCanEdit); } //合计添加百分号 if(a==9 || a==11 || a==13 || a==15 || a==17 || a==19|| a==21 || a==23 || a==25 || a==27 || a==29 || a==31){ createCell.setCellValue((col.get(i)).get(object2) == null ? "0%" : (col.get(i)).get(object2).toString().concat("%")); }else{ createCell.setCellValue((col.get(i)).get(object2) == null ? "" : (col.get(i)).get(object2).toString()); }
} } //设置边框 for (int i = 0; i <= 5; i++) { sumAmtRow.createCell(i).setCellStyle(sumCellStyle); } // 必须在单元格设值以后进行 // 设置为根据内容自动调整列宽 /* for (int k = 0; k < 5; k++) { sheet.autoSizeColumn(k); }*/ sheet.protectSheet("123"); String saveFileName = ""; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); saveFileName += format.format(Calendar.getInstance().getTime()) + "预算执行批复表.xls"; // 导出excel的全路径 String excName = ""; if (StringUtils.isEmpty(col.get(0).get("departmentCode"))) { excName = col.get(0).get("agyName").toString(); } else { excName = col.get(0).get("departmentName").toString(); } String fullFilePaths =""; if("all".equals(isAllAgy)){ fullFilePaths="C:"+File.separator+"Users"+File.separator+"Public" + File.separator+"Downloads" +File.separator +"预算执行计划表(所有单位)"+File.separator+ bip.getAgyName() + File.separator + excName +finalFundTypeName+ saveFileName; }else { fullFilePaths="C:"+File.separator+"Users"+File.separator+"Public" + File.separator+"Downloads" +File.separator +"tyzj"+File.separator+ excName +finalFundTypeName+ saveFileName; } fileNames.add(fullFilePaths); os = new FileOutputStream(fullFilePaths); // 写文件 wb.write(os); //清空流缓冲区数据 os.flush(); //关闭流 os.close(); } catch (Exception e) { e.printStackTrace(); } }); return fileNames; } else { return fileNames; } } /** * 创建文件夹 */ private void createFile(String path){ File fileDir = new File(path); System.out.println(path); //创建文件夹 if (!fileDir.exists() && !fileDir.isDirectory()) { fileDir.mkdirs(); } } //压缩文件 public void ZipFiles(File[] srcfile, File zipfile) { byte[] buf = new byte[1024]; try { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipfile)); for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); out.putNextEntry(new ZipEntry(srcfile[i].getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); } catch (IOException e) { e.printStackTrace(); } }
/*** * 删除指定文件夹下所有文件 * * @param path 文件夹完整绝对路径 * @return */ public static boolean delAllFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件 delFolder(path + "/" + tempList[i]);// 再删除空文件夹 flag = true; } } return flag; }
/*** * 删除文件夹 * * @param folderPath 文件夹完整绝对路径 */ public static void delFolder(String folderPath) { try { delAllFile(folderPath); // 删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); // 删除空文件夹 } catch (Exception e) { e.printStackTrace(); } }
|