Commit 5cb45e377cf09b2ada1f5aee28ebe78bbc0c2579

Authored by 娄高锋
1 parent b8003da3

行车路单批量打包下载改成下载单张excel其中持有所有的sheet表单

src/main/java/com/bsth/controller/DownloadController.java
@@ -19,9 +19,6 @@ import org.springframework.http.MediaType; @@ -19,9 +19,6 @@ import org.springframework.http.MediaType;
19 import org.springframework.http.ResponseEntity; 19 import org.springframework.http.ResponseEntity;
20 import org.springframework.stereotype.Component; 20 import org.springframework.stereotype.Component;
21 import org.springframework.web.bind.annotation.RequestMapping; 21 import org.springframework.web.bind.annotation.RequestMapping;
22 -  
23 -import com.google.protobuf.UnknownFieldSet.Field;  
24 -import com.mysql.fabric.Response;  
25 22
26 /** 23 /**
27 * <一句话功能简述> 24 * <一句话功能简述>
@@ -55,11 +52,12 @@ public class DownloadController @@ -55,11 +52,12 @@ public class DownloadController
55 headers, HttpStatus.CREATED); 52 headers, HttpStatus.CREATED);
56 } 53 }
57 54
58 - @RequestMapping("download") 55 + @RequestMapping("download")
59 public void download(HttpServletResponse response,String fileName) 56 public void download(HttpServletResponse response,String fileName)
60 throws IOException { 57 throws IOException {
61 // String fileNames=URLDecoder.decode(fileName,"UTF-8"); 58 // String fileNames=URLDecoder.decode(fileName,"UTF-8");
62 - fileName =fileName + ".xls"; 59 + if(!fileName.contains(".xls"))
  60 + fileName =fileName + ".xls";
63 String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName; 61 String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName;
64 File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的 62 File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
65 // String filename = file.getName();// 获取日志文件名称 63 // String filename = file.getName();// 获取日志文件名称
@@ -82,6 +80,7 @@ public class DownloadController @@ -82,6 +80,7 @@ public class DownloadController
82 public void downloadList(HttpServletResponse response,String fileName) 80 public void downloadList(HttpServletResponse response,String fileName)
83 throws IOException { 81 throws IOException {
84 // String fileNames=URLDecoder.decode(fileName,"UTF-8"); 82 // String fileNames=URLDecoder.decode(fileName,"UTF-8");
  83 + File files = new File(this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName);//
85 fileName = fileName + ".zip"; 84 fileName = fileName + ".zip";
86 String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName; 85 String moudelPath = this.getClass().getResource("/").getPath()+ "static/pages/forms/export/"+fileName;
87 File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的 86 File file = new File(moudelPath);// path是根据日志路径和文件名拼接出来的
@@ -99,7 +98,8 @@ public class DownloadController @@ -99,7 +98,8 @@ public class DownloadController
99 os.flush(); 98 os.flush();
100 os.close(); 99 os.close();
101 file.delete(); 100 file.delete();
102 - } 101 + deleteFiles(files);
  102 + }
103 103
104 104
105 @RequestMapping("downloadModel") 105 @RequestMapping("downloadModel")
@@ -125,4 +125,23 @@ public class DownloadController @@ -125,4 +125,23 @@ public class DownloadController
125 } 125 }
126 126
127 127
  128 + /**
  129 + * 删除文件夹
  130 + * @param files
  131 + * @return
  132 + */
  133 + public Boolean deleteFiles(File files){
  134 + if (files.isDirectory()) {
  135 + String[] children = files.list();
  136 + //递归删除目录中的子目录下
  137 + for (int i=0; i<children.length; i++) {
  138 + boolean success = deleteFiles(new File(files, children[i]));
  139 + if (!success) {
  140 + return false;
  141 + }
  142 + }
  143 + }
  144 + //目录此时为空,可以删除
  145 + return files.delete();
  146 + }
128 } 147 }
src/main/java/com/bsth/service/realcontrol/impl/ScheduleRealInfoServiceImpl.java
@@ -4338,7 +4338,8 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -4338,7 +4338,8 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
4338 return new ArrayList<Map<String, Object>>(); 4338 return new ArrayList<Map<String, Object>>();
4339 } 4339 }
4340 4340
4341 - public void exportWaybill_pl(List<ScheduleRealInfo> listpl,String date,String jName, String clZbh,String lpName) { 4341 + public void exportWaybill_pl(List<ScheduleRealInfo> listpl,
  4342 + String date, String jName, String clZbh, String lpName) {
4342 ReportUtils ee = new ReportUtils(); 4343 ReportUtils ee = new ReportUtils();
4343 ReportRelatedUtils rru = new ReportRelatedUtils(); 4344 ReportRelatedUtils rru = new ReportRelatedUtils();
4344 List<Iterator<?>> list = new ArrayList<Iterator<?>>(); 4345 List<Iterator<?>> list = new ArrayList<Iterator<?>>();
@@ -4361,7 +4362,7 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -4361,7 +4362,7 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
4361 // int cjbc = scheduleRealInfoRepository.findCjbc(jName, clZbh, lpName); 4362 // int cjbc = scheduleRealInfoRepository.findCjbc(jName, clZbh, lpName);
4362 // int ljbc = scheduleRealInfoRepository.findLjbc(jName, clZbh, lpName); 4363 // int ljbc = scheduleRealInfoRepository.findLjbc(jName, clZbh, lpName);
4363 //计算里程和班次数,并放入Map里 4364 //计算里程和班次数,并放入Map里
4364 - Map<String, Object> map = this.MapById(scheduleRealInfos.get(0).getId()); 4365 + Map<String, Object> map = this.MapById(scheduleRealInfos.get(0).getId());
4365 4366
4366 map.put("jhlc",Arith.add(culateMieageService.culateJhgl(scheduleRealInfos),culateMieageService.culateJhJccgl(scheduleRealInfos))); 4367 map.put("jhlc",Arith.add(culateMieageService.culateJhgl(scheduleRealInfos),culateMieageService.culateJhJccgl(scheduleRealInfos)));
4367 map.put("remMileage",culateMieageService.culateLbgl(scheduleRealInfos)); 4368 map.put("remMileage",culateMieageService.culateLbgl(scheduleRealInfos));
@@ -4508,7 +4509,6 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -4508,7 +4509,6 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
4508 4509
4509 4510
4510 String path = this.getClass().getResource("/").getPath() + "static/pages/forms/"; 4511 String path = this.getClass().getResource("/").getPath() + "static/pages/forms/";
4511 -  
4512 list.add(listMap.iterator()); 4512 list.add(listMap.iterator());
4513 String xls=""; 4513 String xls="";
4514 if(map.get("type").toString().equals("0")){ 4514 if(map.get("type").toString().equals("0")){
@@ -4516,15 +4516,16 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -4516,15 +4516,16 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
4516 }else{ 4516 }else{
4517 xls="waybill_minhang_dl.xls"; 4517 xls="waybill_minhang_dl.xls";
4518 } 4518 }
  4519 + map.put("sheetName", jName + "-" + clZbh + "-" + lpName);
4519 ee.excelReplace(list, new Object[]{map}, path + "mould/"+xls, 4520 ee.excelReplace(list, new Object[]{map}, path + "mould/"+xls,
4520 path + "export/" + date + "-" + jName + "-" + clZbh + "-" + lpName + "-行车路单.xls"); 4521 path + "export/" + date + "-" + jName + "-" + clZbh + "-" + lpName + "-行车路单.xls");
4521 -// return scheduleRealInfos;  
4522 } 4522 }
4523 4523
4524 @Override 4524 @Override
4525 public Map<String, Object> exportWaybillMore(Map<String, Object> map) { 4525 public Map<String, Object> exportWaybillMore(Map<String, Object> map) {
4526 String date = map.get("date").toString(); 4526 String date = map.get("date").toString();
4527 String line = map.get("line").toString(); 4527 String line = map.get("line").toString();
  4528 + ReportUtils ee = new ReportUtils();
4528 List<List> lists = JSON.parseArray(map.get("strs").toString(), List.class); 4529 List<List> lists = JSON.parseArray(map.get("strs").toString(), List.class);
4529 String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/"; 4530 String path = this.getClass().getResource("/").getPath() + "static/pages/forms/export/";
4530 SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"), 4531 SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM-dd"),
@@ -4534,16 +4535,19 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -4534,16 +4535,19 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
4534 try { 4535 try {
4535 while (true) { 4536 while (true) {
4536 String fileUrl = path + "行车路单" + sdfSimple.format(sdfMonth.parse(date)); 4537 String fileUrl = path + "行车路单" + sdfSimple.format(sdfMonth.parse(date));
4537 - file = new File(fileUrl + (num == 0 ? "/" : "(" + num + ")/"));  
4538 - if (file.exists()) { 4538 +// file = new File(fileUrl + (num == 0 ? "/" : "(" + num + ")/")); //新建文件夹
  4539 + file = new File(fileUrl + (num == 0 ? ".xls" : "(" + num + ").xls")); //新建excel文件
  4540 + if (file.exists()) { //判断是否已存在重名
4539 num++; 4541 num++;
4540 } else { 4542 } else {
4541 break; 4543 break;
4542 } 4544 }
4543 } 4545 }
4544 - file.mkdirs(); 4546 +// file.mkdirs(); //创建
4545 List<ScheduleRealInfo> lists_line=scheduleRealInfoRepository.scheduleByDateAndLineTjrb(line, date); 4547 List<ScheduleRealInfo> lists_line=scheduleRealInfoRepository.scheduleByDateAndLineTjrb(line, date);
4546 - for (List<String> list : lists) { 4548 +
  4549 + List<File> files = new ArrayList<File>();
  4550 + for (List<String> list : lists){
4547 List<ScheduleRealInfo> newList=new ArrayList<ScheduleRealInfo>(); 4551 List<ScheduleRealInfo> newList=new ArrayList<ScheduleRealInfo>();
4548 String jName = list.get(0); 4552 String jName = list.get(0);
4549 String clZbh = list.get(1); 4553 String clZbh = list.get(1);
@@ -4558,26 +4562,37 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf @@ -4558,26 +4562,37 @@ public class ScheduleRealInfoServiceImpl extends BaseServiceImpl&lt;ScheduleRealInf
4558 this.exportWaybill_pl(newList, date, jName, clZbh, lpName); 4562 this.exportWaybill_pl(newList, date, jName, clZbh, lpName);
4559 File temp = new File(path + date + "-" + jName + "-" + clZbh + "-" + lpName + "-行车路单.xls"); 4563 File temp = new File(path + date + "-" + jName + "-" + clZbh + "-" + lpName + "-行车路单.xls");
4560 String fileName = file.getName(); 4564 String fileName = file.getName();
4561 - temp.renameTo(new File(path + fileName + "/" + temp.getName()));  
4562 -  
4563 - File[] listFiles = file.listFiles();  
4564 - ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(path + file.getName() + ".zip")));  
4565 -// zos.setEncoding("gbk");  
4566 -// zos.putNextEntry(new ZipEntry(fileName + "/"));  
4567 - for (int i = 0; i < listFiles.length; i++) {  
4568 - zos.putNextEntry(new ZipEntry(fileName + "/" + listFiles[i].getName()));  
4569 - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(listFiles[i]));  
4570 - BufferedOutputStream bos = new BufferedOutputStream(zos);  
4571 - int bytesRead = 0;  
4572 - for (byte[] buffer = new byte[BUF_SIZE]; ((bytesRead = bis.read(buffer, 0, BUF_SIZE)) != -1); ) {  
4573 -// zos.write(buffer, 0, bytesRead);  
4574 -// zos.flush();  
4575 - bos.write(buffer, 0, bytesRead);  
4576 - bos.flush();  
4577 - }  
4578 - }  
4579 - zos.close();  
4580 - } 4565 + files.add(temp);
  4566 + }
  4567 + for(int i = 1; i < files.size(); i++){
  4568 + File file1 = files.get(0);
  4569 + File file2 = files.get(i);
  4570 + ee.copySheetByFile(file2, file1, 0, 145);
  4571 + }
  4572 + File newFile = files.get(0);
  4573 + newFile.renameTo(file);
  4574 +
  4575 +// temp.renameTo(new File(path + fileName + "/" + temp.getName()));
  4576 +// File[] listFiles = file.listFiles();
  4577 +// ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(path + file.getName() + ".zip")));
  4578 +//// zos.setEncoding("gbk");
  4579 +//// zos.putNextEntry(new ZipEntry(fileName + "/"));
  4580 +// for (int i = 0; i < listFiles.length; i++) {
  4581 +// zos.putNextEntry(new ZipEntry(fileName + "/" + listFiles[i].getName()));
  4582 +// BufferedInputStream bis = new BufferedInputStream(new FileInputStream(listFiles[i]));
  4583 +// BufferedOutputStream bos = new BufferedOutputStream(zos);
  4584 +// int bytesRead = 0;
  4585 +// for (byte[] buffer = new byte[BUF_SIZE]; ((bytesRead = bis.read(buffer, 0, BUF_SIZE)) != -1); ) {
  4586 +//// zos.write(buffer, 0, bytesRead);
  4587 +//// zos.flush();
  4588 +// bos.write(buffer, 0, bytesRead);
  4589 +// bos.flush();
  4590 +// }
  4591 +//// bos.close();
  4592 +// bis.close();
  4593 +// }
  4594 +// zos.close();
  4595 +// }
4581 4596
4582 } catch (Exception e) { 4597 } catch (Exception e) {
4583 // TODO: handle exception 4598 // TODO: handle exception
src/main/java/com/bsth/util/ReportUtils.java
@@ -18,6 +18,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet; @@ -18,6 +18,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
18 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 18 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
19 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 19 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
20 import org.apache.poi.ss.usermodel.Cell; 20 import org.apache.poi.ss.usermodel.Cell;
  21 +import org.apache.poi.ss.util.CellRangeAddress;
21 22
22 import com.bsth.entity.Line; 23 import com.bsth.entity.Line;
23 import com.bsth.entity.realcontrol.ScheduleRealInfo; 24 import com.bsth.entity.realcontrol.ScheduleRealInfo;
@@ -114,6 +115,145 @@ public class ReportUtils { @@ -114,6 +115,145 @@ public class ReportUtils {
114 } 115 }
115 } 116 }
116 117
  118 + /**
  119 + * 将file1中的一页sheet复制到file2中
  120 + *
  121 + * @param file1
  122 + * 原sheet所在的excel文件
  123 + * @param file2
  124 + * 目标excel文件
  125 + * @param page
  126 + * 原excel中要被复制的sheet的位置(从0开始)
  127 + * @param rate
  128 + * 调整复制后的缩放倍率(列如:145,则为缩放145%)
  129 + */
  130 + public void copySheetByFile(File file1, File file2, int page, int rate) {
  131 + try {
  132 + // 把源文件放入流中
  133 + POIFSFileSystem fs1 = new POIFSFileSystem(new FileInputStream(file1));
  134 + HSSFWorkbook wb1 = new HSSFWorkbook(fs1);
  135 + HSSFSheet sheet = wb1.getSheetAt(page);
  136 + POIFSFileSystem fs2 = new POIFSFileSystem(new FileInputStream(file2));
  137 + HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
  138 + HSSFSheet createSheet = wb2.createSheet(sheet.getSheetName());
  139 + HSSFCellStyle createCellStyle = wb2.createCellStyle();
  140 + HSSFRow row;
  141 +
  142 + createSheet.setZoom(rate, 100);
  143 + for(int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++){
  144 + createSheet.setColumnWidth(i, sheet.getColumnWidth(i));
  145 + }
  146 +
  147 + List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
  148 + for(int l = 0; l < mergedRegions.size(); l++){
  149 + //复制源表中的合并单元格
  150 + createSheet.addMergedRegion(mergedRegions.get(l));
  151 + }
  152 + int firstRow = sheet.getFirstRowNum();
  153 + int lastRow = sheet.getLastRowNum();
  154 + for(int k = firstRow; k <= lastRow; k++){
  155 + // 创建新建excel Sheet的行
  156 + HSSFRow rowCreat = createSheet.createRow(k);
  157 + // 取得源有excel Sheet的行
  158 + row = sheet.getRow(k);
  159 +// rowCreat.setHeight(row.getHeight()); //设置行高
  160 + // 单元格式样
  161 + int firstCell = row.getFirstCellNum();
  162 + int lastCell = row.getLastCellNum();
  163 + for (int j = firstCell; j < lastCell; j++) {
  164 + // 自动适应列宽 貌似不起作用
  165 +// createSheet.autoSizeColumn(j);
  166 +// System.out.println(row.getCell(j));
  167 + rowCreat.createCell(j);
  168 + String strVal = "";
  169 + if (row.getCell(j)==null) {
  170 +
  171 + } else {
  172 + strVal = row.getCell(j).getStringCellValue();
  173 + rowCreat.getCell(j).setCellValue(strVal);
  174 + copyCellStyle(wb1, row.getCell(j).getCellStyle(), createCellStyle);
  175 + createCellStyle.setBorderTop((short)1);
  176 + createCellStyle.setBorderLeft((short)1);
  177 + createCellStyle.setBorderRight((short)1);
  178 + createCellStyle.setBorderBottom((short)1);
  179 + rowCreat.getCell(j).setCellStyle(createCellStyle);
  180 + }
  181 + }
  182 + }
  183 +
  184 +// int firstRowNum = createSheet.getFirstRowNum();
  185 +// int lastRowNum = createSheet.getLastRowNum();
  186 +// int test = 0;
  187 +// for(int k = firstRowNum; k <= lastRowNum; k++){
  188 +// HSSFRow createRow = createSheet.getRow(k);
  189 +// int firstCellNum = createRow.getFirstCellNum();
  190 +// int lastCellNum = createRow.getLastCellNum();
  191 +// for(int i = firstCellNum; i < lastCellNum; i++){
  192 +// HSSFCell cell = createRow.getCell(i);
  193 +// cell.getCellStyle().setBorderTop(HSSFCellStyle.BORDER_THIN);
  194 +// cell.getCellStyle().setBorderLeft(HSSFCellStyle.BORDER_THIN);
  195 +// cell.getCellStyle().setBorderRight(HSSFCellStyle.BORDER_THIN);
  196 +// cell.getCellStyle().setBorderBottom(HSSFCellStyle.BORDER_THIN);
  197 +// test ++;
  198 +// }
  199 +// }
  200 +// System.out.println("test = " + test);
  201 +
  202 + FileOutputStream fileOut = new FileOutputStream(file2);
  203 + wb2.write(fileOut);
  204 + fileOut.close();
  205 + wb2.close();
  206 + wb1.close();
  207 + fs2.close();
  208 + fs1.close();
  209 + file1.delete();
  210 +// // 创建目标文件夹
  211 +// createFolder(targetPath);
  212 + // 输出文件
  213 + } catch (Exception e) {
  214 + e.printStackTrace();
  215 + }
  216 + }
  217 +
  218 + public void test(File file){
  219 + POIFSFileSystem fs;
  220 + try {
  221 + fs = new POIFSFileSystem(new FileInputStream(file));
  222 + HSSFWorkbook wb = new HSSFWorkbook(fs);
  223 + for(int j = 0; j < wb.getNumberOfSheets(); j++){
  224 + HSSFSheet sheet = wb.getSheetAt(j);
  225 + int firstRowNum = sheet.getFirstRowNum();
  226 + int lastRowNum = sheet.getLastRowNum();
  227 + int test = 0;
  228 + for(int k = firstRowNum; k <= lastRowNum; k++){
  229 + HSSFRow createRow = sheet.getRow(k);
  230 + int firstCellNum = createRow.getFirstCellNum();
  231 + int lastCellNum = createRow.getLastCellNum();
  232 + for(int i = firstCellNum; i < lastCellNum; i++){
  233 + HSSFCell cell = createRow.getCell(i);
  234 + HSSFCellStyle cellStyle = wb.createCellStyle();
  235 +
  236 + cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  237 + cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  238 + cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  239 + cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  240 + cell.setCellStyle(cellStyle);
  241 + test ++;
  242 + }
  243 + }
  244 + System.out.println("test = " + test);
  245 +
  246 + FileOutputStream fileOut = new FileOutputStream(file);
  247 + wb.write(fileOut);
  248 + fileOut.close();
  249 + }
  250 + } catch (Exception e) {
  251 + // TODO Auto-generated catch block
  252 + e.printStackTrace();
  253 + }
  254 +
  255 + }
  256 +
117 public String getCellValue(HSSFCell cell) { 257 public String getCellValue(HSSFCell cell) {
118 int cellType = 0; 258 int cellType = 0;
119 String result = ""; 259 String result = "";
src/main/resources/static/pages/forms/statement/waybill.html
@@ -359,7 +359,8 @@ @@ -359,7 +359,8 @@
359 }); 359 });
360 }); 360 });
361 $get('/realSchedule/exportWaybillMore',{date:date,line:line,strs:JSON.stringify(param)},function(result){ 361 $get('/realSchedule/exportWaybillMore',{date:date,line:line,strs:JSON.stringify(param)},function(result){
362 - window.open("/downloadFile/downloadList?fileName="+result.fileName); 362 +// window.open("/downloadFile/downloadList?fileName="+result.fileName); //下载压缩包
  363 + window.open("/downloadFile/download?fileName="+result.fileName);
363 }); 364 });
364 }); 365 });
365 366