From 18831ab95d51d483a3e3fca6bac20755eded1a75 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Thu, 18 Jul 2024 15:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Excel=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=B5=8B=E8=AF=95=E7=B1=BB=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=A8=A1=E6=9D=BF=E5=AF=BC=E5=87=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/excel/ExcelExport.java | 71 ++++---------- .../common/utils/excel/ExcelImport.java | 71 +++++++++----- .../test/utils/excel/ExcelExportTest.java | 92 +++++++++++++++++++ .../test/utils/excel/ExcelImportTest.java | 45 +++++++++ 4 files changed, 201 insertions(+), 78 deletions(-) create mode 100644 common/src/test/java/com/jeesite/test/utils/excel/ExcelExportTest.java create mode 100644 common/src/test/java/com/jeesite/test/utils/excel/ExcelImportTest.java diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java index 6062530e..52ee737a 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java @@ -59,7 +59,7 @@ public class ExcelExport implements Closeable{ /** * 当前行号 */ - private int rownum; + private int rowNum; /** * 注解列表(Object[]{ ExcelField, Field/Method }) @@ -158,6 +158,16 @@ public class ExcelExport implements Closeable{ } this.createSheet(sheetName, title, headerList, headerWidthList); } + + /** + * 构造函数 + */ + public ExcelExport(ExcelImport excelImport) { + this.wb = excelImport.getWorkbook(); + this.styles = createStyles(wb); + this.sheet = excelImport.getSheet(); + this.rowNum = excelImport.getHeaderNum(); + } /** * 创建一个工作簿 @@ -286,12 +296,12 @@ public class ExcelExport implements Closeable{ * @param headerWidthList 表头字段宽度设置 */ public void createSheet(String sheetName, String title, List headerList, List headerWidthList) { - this.sheet = wb.createSheet(StringUtils.defaultString(sheetName, StringUtils.defaultString(title, "Sheet1"))); + this.sheet = wb.createSheet(StringUtils.defaultIfBlank(sheetName, StringUtils.defaultIfBlank(title, "Sheet1"))); this.styles = createStyles(wb); - this.rownum = 0; + this.rowNum = 0; // Create title if (StringUtils.isNotBlank(title)){ - Row titleRow = sheet.createRow(rownum++); + Row titleRow = sheet.createRow(rowNum++); titleRow.setHeightInPoints(30); Cell titleCell = titleRow.createCell(0); titleCell.setCellStyle(styles.get("title")); @@ -303,7 +313,7 @@ public class ExcelExport implements Closeable{ if (headerList == null){ throw new ExcelException("headerList not null!"); } - Row headerRow = sheet.createRow(rownum++); + Row headerRow = sheet.createRow(rowNum++); headerRow.setHeightInPoints(16); for (int i = 0; i < headerList.size(); i++) { Cell cell = headerRow.createCell(i); @@ -412,7 +422,7 @@ public class ExcelExport implements Closeable{ * @return 行对象 */ public Row addRow(){ - return sheet.createRow(rownum++); + return sheet.createRow(rowNum++); } /** @@ -633,54 +643,5 @@ public class ExcelExport implements Closeable{ e.printStackTrace(); } } - -// /** -// * 导出测试 -// */ -// public static void main(String[] args) throws Throwable { -// -// // 初始化表头 -// List headerList = ListUtils.newArrayList(); -// for (int i = 1; i <= 10; i++) { -// headerList.add("表头"+i); -// } -// -// // 初始化数据集 -// List rowList = ListUtils.newArrayList(); -// for (int i = 1; i <= headerList.size(); i++) { -// rowList.add("数据"+i); -// } -// List> dataList = ListUtils.newArrayList(); -// for (int i = 1; i <=100; i++) { -// dataList.add(rowList); -// } -// -// // 创建一个Sheet表,并导入数据 -// try(ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null)){ -// -// for (int i = 0; i < dataList.size(); i++) { -// Row row = ee.addRow(); -// for (int j = 0; j < dataList.get(i).size(); j++) { -// ee.addCell(row, j, dataList.get(i).get(j)); -// } -// } -// -// // 再创建一个Sheet表,并导入数据 -// ee.createSheet("表格2", "表格标题2", headerList, null); -// for (int i = 0; i < dataList.size(); i++) { -// Row row = ee.addRow(); -// for (int j = 0; j < dataList.get(i).size(); j++) { -// ee.addCell(row, j, dataList.get(i).get(j)+"2"); -// } -// } -// -// // 输出到文件 -// ee.writeFile("target/export.xlsx"); -// -// } -// -// log.debug("Export success."); -// -// } } diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java index 058fae70..4dc78b57 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java @@ -68,13 +68,32 @@ public class ExcelImport implements Closeable { /** * 构造函数 - * @param path 导入文件对象,读取第一个工作表 + * @param fileName 导入文件对象,读取第一个工作表 + * @throws InvalidFormatException + * @throws IOException + */ + public ExcelImport(String fileName) throws InvalidFormatException, IOException { + this(new File(fileName)); + } + + /** + * 构造函数 + * @param file 导入文件对象,读取第一个工作表 * @throws InvalidFormatException * @throws IOException */ public ExcelImport(File file) throws InvalidFormatException, IOException { this(file, 0, 0); } + /** + * 构造函数 + * @param fileName 导入文件对象,读取第一个工作表 + * @throws InvalidFormatException + * @throws IOException + */ + public ExcelImport(String fileName, int headerNum) throws InvalidFormatException, IOException { + this(new File(fileName), headerNum); + } /** * 构造函数 @@ -88,6 +107,19 @@ public class ExcelImport implements Closeable { this(file, headerNum, 0); } + /** + * 构造函数 + * @param fileName 导入文件对象 + * @param headerNum 标题行数,数据行号=标题行数+1 + * @param sheetIndexOrName 工作表编号或名称,从0开始 + * @throws InvalidFormatException + * @throws IOException + */ + public ExcelImport(String fileName, int headerNum, Object sheetIndexOrName) + throws InvalidFormatException, IOException { + this(new File(fileName), headerNum, sheetIndexOrName); + } + /** * 构造函数 * @param file 导入文件对象 @@ -170,7 +202,21 @@ public class ExcelImport implements Closeable { public Workbook getWorkbook() { return wb; } - + + /** + * 获取当前工作表 + */ + public Sheet getSheet() { + return sheet; + } + + /** + * 获取当前标题行数 + */ + public int getHeaderNum() { + return headerNum; + } + /** * 设置当前工作表和标题行数 * @author ThinkGem @@ -528,25 +574,4 @@ public class ExcelImport implements Closeable { } } -// /** -// * 导入测试 -// */ -// public static void main(String[] args) throws Throwable { -// -// ImportExcel ei = new ImportExcel("target/export.xlsx", 1); -// -// for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) { -// Row row = ei.getRow(i); -// if (row == null){ -// continue; -// } -// for (int j = 0; j < ei.getLastCellNum(); j++) { -// Object val = ei.getCellValue(row, j); -// System.out.print(val+", "); -// } -// System.out.println(); -// } -// -// } - } diff --git a/common/src/test/java/com/jeesite/test/utils/excel/ExcelExportTest.java b/common/src/test/java/com/jeesite/test/utils/excel/ExcelExportTest.java new file mode 100644 index 00000000..0e9fd323 --- /dev/null +++ b/common/src/test/java/com/jeesite/test/utils/excel/ExcelExportTest.java @@ -0,0 +1,92 @@ +/** + * Copyright (c) 2013-Now http://jeesite.com All rights reserved. + * No deletion without permission, or be held responsible to law. + */ +package com.jeesite.test.utils.excel; + +import com.jeesite.common.collect.ListUtils; +import com.jeesite.common.utils.excel.ExcelExport; +import com.jeesite.common.utils.excel.ExcelImport; +import org.apache.poi.ss.usermodel.Row; + +import java.io.File; +import java.util.List; + +/** + * 导出Excel文件测试类 + * @author ThinkGem + * @version 2020-3-5 + */ +public class ExcelExportTest { + + /** + * 导出测试 + */ + public static void main(String[] args) throws Throwable { + + File classPath = new File(ExcelExportTest.class.getResource("/").getFile()); + String fileName = classPath.getParentFile().getAbsoluteFile() + "/export.xlsx"; + + // 初始化表头 + List headerList = ListUtils.newArrayList(); + for (int i = 1; i <= 10; i++) { + headerList.add("表头"+i); + } + + // 初始化数据集 + List rowList = ListUtils.newArrayList(); + for (int i = 1; i <= headerList.size(); i++) { + rowList.add("数据"+i); + } + List> dataList = ListUtils.newArrayList(); + for (int i = 1; i <=100; i++) { + dataList.add(rowList); + } + + // 创建一个Sheet表,并导入数据 + try(ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null)){ + + for (int i = 0; i < dataList.size(); i++) { + Row row = ee.addRow(); + for (int j = 0; j < dataList.get(i).size(); j++) { + ee.addCell(row, j, dataList.get(i).get(j)); + } + } + + // 再创建一个Sheet表,并导入数据 + ee.createSheet("表格2", "表格标题2", headerList, null); + for (int i = 0; i < dataList.size(); i++) { + Row row = ee.addRow(); + for (int j = 0; j < dataList.get(i).size(); j++) { + ee.addCell(row, j, dataList.get(i).get(j)+"2"); + } + } + + // 输出到文件 + ee.writeFile(fileName); + + } + + System.out.println("Export success."); + + // 按模板导出,从第 3 行开始 + ExcelImport ei = new ExcelImport(fileName, 3); + try(ExcelExport ee = new ExcelExport(ei)){ + + for (int i = 0; i < dataList.size(); i++) { + Row row = ee.addRow(); + for (int j = 0; j < dataList.get(i).size(); j++) { + ee.addCell(row, j, dataList.get(i).get(j) + "-plus"); + } + } + + // 输出到文件 + ee.writeFile(fileName); + + } + + System.out.println("Export success by template."); + + } + +} diff --git a/common/src/test/java/com/jeesite/test/utils/excel/ExcelImportTest.java b/common/src/test/java/com/jeesite/test/utils/excel/ExcelImportTest.java new file mode 100644 index 00000000..a386a291 --- /dev/null +++ b/common/src/test/java/com/jeesite/test/utils/excel/ExcelImportTest.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2013-Now http://jeesite.com All rights reserved. + * No deletion without permission, or be held responsible to law. + */ +package com.jeesite.test.utils.excel; + +import com.jeesite.common.utils.excel.ExcelImport; +import org.apache.poi.ss.usermodel.Row; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; + +/** + * 导入Excel文件测试类 + * @author ThinkGem + * @version 2020-3-5 + */ +public class ExcelImportTest { + + /** + * 导入测试 + */ + public static void main(String[] args) throws Throwable { + + File classPath = new File(ExcelExportTest.class.getResource("/").getFile()); + String fileName = classPath.getParentFile().getAbsoluteFile() + "/export.xlsx"; + ExcelImport ei = new ExcelImport(fileName, 1); + + for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) { + Row row = ei.getRow(i); + if (row == null){ + continue; + } + for (int j = 0; j < ei.getLastCellNum(); j++) { + Object val = ei.getCellValue(row, j); + System.out.print(val+", "); + } + System.out.println(); + } + + System.out.println("Import success."); + } + +}