private String getFilePath(TemplateFile templateFile) { String filePath = templateFile.getFilePath(); if (BusinessConstant.TEMPLATE_FILE_TYPE_GROUP.equals(templateFile.getType())) filePath = templateFile.getGroupFilePath(); return filePath; }
/** * saveMerage 合并多个excel文件 * * @param args * @return void * @exception */ @RemoteMethod public void saveMerage(List<String> args, String templateFileId) { HSSFWorkbook hSSFtargetWorkbook = new HSSFWorkbook(); // XSSFWorkbook xSSFtargetWorkbook = new XSSFWorkbook(); Workbook workbook = null; FileOutputStream out = null; InputStream sourceStream = null; try { // 保存上传的合并文件 TemplateFile templateFile = templateFileDaoImpl.findById(templateFileId); String uuid = UUID.randomUUID().toString(); String templateFilePath = getFilePath(templateFile); String uploadFilePath = formTemplateService.getUploadFilePath(null, BusinessConstant.FORM_UPLOAD) + "/" + uuid + FileUploadUtils.getFileType(templateFilePath); templateFile.setGroupFileName(templateFile.getFileName()); templateFile.setGroupFilePath(uuid + FileUploadUtils.getFileType(templateFilePath)); templateFileDaoImpl.saveOrUpdate(templateFile); for (String detaiId : args) { TemplateShareGroupDetail templateShareGroupDetail = templateShareGroupDetailDaoImpl.findById(detaiId); String filePath = formTemplateService.downLoadFilePath( templateShareGroupDetail.getAccount(), templateShareGroupDetail.getCreate_date()) + "/" + templateShareGroupDetail.getFilePath(); sourceStream = new FileInputStream(filePath); // 每个实例的文件路径 workbook = WorkbookFactory.create(sourceStream); if (".xls".equals(FileUploadUtils.getFileType(templateShareGroupDetail.getFilePath())) || ".et".equals(FileUploadUtils.getFileType(templateShareGroupDetail.getFilePath()))) { out = new FileOutputStream(new File(uploadFilePath)); // 合并后的文件路径 PoiUtils.copySheet2003( hSSFtargetWorkbook.createSheet( templateShareGroupDetail.getAccount().getPerson().getName()), (HSSFSheet) workbook.getSheetAt(0), hSSFtargetWorkbook, (HSSFWorkbook) workbook, true, true); hSSFtargetWorkbook.write(out); } // 由于金格office编辑器在编辑07版本的excel时会将其内在格式转换为03版本,故poi无法将以.xlsx为后缀的excel读取为XSSFWorkBook对象,此功能暂注释 // else if // (".xlsx".equals(getFileType(templateShareGroupDetail.getFilePath()))) // {// 2007 // out = new FileOutputStream(new File("d:\\hhh2.xlsx")); // PoiUtils.copySheet2007(xSSFtargetWorkbook.createSheet(templateShareGroupDetail.getAccount().getPerson().getName()), // (XSSFSheet) workbook.getSheetAt(0), xSSFtargetWorkbook, // (XSSFWorkbook) workbook, true); // xSSFtargetWorkbook.write(out); // } else throw new BusinessException(ERR_COMPONENT + "101"); } templateFileLogService.addOperationLog( templateFile, BusinessConstant.OPERATION_SHARE_GROUP_MODIFY, null); } catch (Exception e) { e.printStackTrace(); throw new BusinessException(ERR_COMPONENT + "101"); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } }