From 741f4ce20d2b9fcac326e479587499e710b4061a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Mon, 24 Apr 2023 08:25:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/UnitTest/TestMergeExcel.java | 19 ++ .../FengHuang/Util/ExcelCommonUtil.java | 165 ++++++++++++++++++ .../FengHuang/Yp/Controller/YpController.java | 2 - 3 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/main/java/UnitTest/TestMergeExcel.java diff --git a/src/main/java/UnitTest/TestMergeExcel.java b/src/main/java/UnitTest/TestMergeExcel.java new file mode 100644 index 0000000..b627ff6 --- /dev/null +++ b/src/main/java/UnitTest/TestMergeExcel.java @@ -0,0 +1,19 @@ +package UnitTest; + +import com.dsideal.FengHuang.Util.ExcelCommonUtil; + +import java.util.ArrayList; + +public class TestMergeExcel { + public static void main(String[] args) { + //这里是xls文件 + String[] filePaths = {"c:/Test/7840.xlsx","c:/Test/8210.xlsx"}; + ArrayList list = new ArrayList<>(); + for (String path : filePaths) { + list.add(path); + } + String path = "c:/Test"; + String fileName = "Result.xlsx"; + ExcelCommonUtil.mergeExcel(list, path, fileName); + } +} diff --git a/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java b/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java index bc7af54..d6d97e3 100644 --- a/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java +++ b/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java @@ -843,4 +843,169 @@ public class ExcelCommonUtil { } sheet.removeMergedRegion(index);//移除合并单元格 } + + /** + * #合并多个excel文件 + * + * @param fileLists excel文件路径 + * @param path 目标文件保存目录 + * @param fileName 目标文件名称 + */ + public static void mergeExcel(List fileLists, String path, String fileName) { + // 创建新的excel工作簿 + XSSFWorkbook newExcelWorkBook = new XSSFWorkbook(); + // 遍历需要合并的excel文件 + for (String excelName : fileLists) { + try (InputStream in = new FileInputStream(excelName)) { + // 创建工作簿 + XSSFWorkbook tmpWorkBook = new XSSFWorkbook(in); + // 获取工作簿中的Sheet个数 + int len = tmpWorkBook.getNumberOfSheets(); + if (len <= 1) { + XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(0); + XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName()); + // 复制sheet内容 + copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet); + } else { + for (int i = 0; i < len; i++) { + XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(i); + XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName()); + // 复制sheet内容 + copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet); + } + } + // 关闭tmpWorkBook工作簿 + tmpWorkBook.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + // 新生成的excel文件 + if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) { + fileName += ".xlsx"; + } + String excelFileName = path + File.separator + fileName; + // 判断文件是否存在 + File excelFile = new File(excelFileName); + if (excelFile.exists()) { + // 存在则删除 + excelFile.delete(); + } + // 使用输出流写出 + try (FileOutputStream fos = new FileOutputStream(excelFileName)) { + newExcelWorkBook.write(fos); + fos.flush(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + newExcelWorkBook.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + System.out.println("excel文件合并成功,合并后文件路径:" + excelFileName); + } + + /** + * #复制sheet到新的excel文件中 + * + * @param workbook excel工作簿 + * @param tmpSheet 来源sheet + * @param newExcelSheet 新生成的sheet + */ + public static void copyExcelSheet(XSSFWorkbook workbook, XSSFSheet tmpSheet, XSSFSheet newExcelSheet) { + // 合并单元格 + mergeSheetAllRegion(tmpSheet, newExcelSheet); + // 设置单元格列宽度 + // 获取最后一个单元格位置 + int len = tmpSheet.getRow(tmpSheet.getFirstRowNum()).getLastCellNum(); + for (int i = 0; i < len; i++) { + newExcelSheet.setColumnWidth(i, tmpSheet.getColumnWidth(i)); + } + // 复制每行内容 + Iterator it = tmpSheet.iterator(); + while (it.hasNext()) { + XSSFRow tmpRow = (XSSFRow) it.next(); + // 创建新行 + XSSFRow newExcelRow = newExcelSheet.createRow(tmpRow.getRowNum()); + // 复制行 + copyExcelRow(workbook, tmpRow, newExcelRow); + } + } + + /** + * #合并单元格 + * + * @param tmpSheet 来源sheet + * @param newExcelSheet 目标sheet + */ + private static void mergeSheetAllRegion(XSSFSheet tmpSheet, XSSFSheet newExcelSheet) { + int num = tmpSheet.getNumMergedRegions(); + CellRangeAddress cellRange = null; + for (int i = 0; i < num; i++) { + cellRange = tmpSheet.getMergedRegion(i); + newExcelSheet.addMergedRegion(cellRange); + } + } + + /** + * #复制excel中的行到新的sheet中 + * + * @param workbook 目标工作簿 + * @param tmpRow 来源excel行 + * @param newExcelRow 目标excel行 + */ + public static void copyExcelRow(XSSFWorkbook workbook, XSSFRow tmpRow, XSSFRow newExcelRow) { + // 设置行高 + newExcelRow.setHeight(tmpRow.getHeight()); + // 获取所有列 + Iterator it = tmpRow.cellIterator(); + while (it.hasNext()) { + XSSFCell tmpCell = (XSSFCell) it.next(); + // 创建单元格 + XSSFCell newExcelCell = newExcelRow.createCell(tmpCell.getColumnIndex()); + // 复制单元格 + copyExcelCell(workbook, tmpCell, newExcelCell); + } + } + + /** + * #复制单元格 + * + * @param workbook 目标工作簿 + * @param tmpCell 来源excel单元格 + * @param newExcelCell 目标excel单元格 + */ + public static void copyExcelCell(XSSFWorkbook workbook, XSSFCell tmpCell, XSSFCell newExcelCell) { + XSSFCellStyle newExcelStyle = workbook.createCellStyle(); + // 复制单元格样式 + newExcelStyle.cloneStyleFrom(tmpCell.getCellStyle()); + // 单元格样式 + newExcelCell.setCellStyle(newExcelStyle); + if (tmpCell.getCellComment() != null) { + newExcelCell.setCellComment(tmpCell.getCellComment()); + } + // 不同数据类型处理 + CellType tmpCellType = tmpCell.getCellType(); + + newExcelCell.setCellType(tmpCellType); + if (tmpCellType == CellType.NUMERIC) { + if (DateUtil.isCellDateFormatted(tmpCell)) { + newExcelCell.setCellValue(tmpCell.getDateCellValue()); + } else { + newExcelCell.setCellValue(tmpCell.getNumericCellValue()); + } + } else if (tmpCellType == CellType.STRING) { + newExcelCell.setCellValue(tmpCell.getRichStringCellValue()); + } else if (tmpCellType == CellType.BLANK) { + } else if (tmpCellType == CellType.BOOLEAN) { + newExcelCell.setCellValue(tmpCell.getBooleanCellValue()); + } else if (tmpCellType == CellType.ERROR) { + newExcelCell.setCellErrorValue(tmpCell.getErrorCellValue()); + } else if (tmpCellType == CellType.FORMULA) { + newExcelCell.setCellFormula(tmpCell.getCellFormula()); + } else { + } + } } diff --git a/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java b/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java index 637cb82..3994914 100644 --- a/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java +++ b/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java @@ -253,7 +253,6 @@ public class YpController extends Controller { renderJson(CommonUtil.returnMessageJson(false, "上传文件类型错误!系统只允许上传jpg格式!")); return; } - String uuid = UUID.randomUUID().toString(); //判断目录是不是存在 File file = new File(PathKit.getWebRootPath() + "/upload"); @@ -269,7 +268,6 @@ public class YpController extends Controller { kv.set("base64", base64); kv.set("uuid", uuid); renderJson(kv); - //path : /FengHuang/upload/sfzh+".jpg" }