|
@@ -77,8 +77,47 @@ public class EasyExcelUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ sourceWorkbook.close();
|
|
|
+ inputStream.close();
|
|
|
+
|
|
|
+ return targetWorkbook;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并Excel文件-并且复制数据验证
|
|
|
+ *
|
|
|
+ * @param sourceExcelPath 源文件路径(段落模板)
|
|
|
+ * @param targetExcelPath 目标文件路径(主模板)
|
|
|
+ * @param targetSheetIndex 目标sheet索引(要插入的sheet)
|
|
|
+ * @param targetRowNum 目标行索引(要插入的行)
|
|
|
+ * @param sourceSheetIndex 源文件被引用的sheet
|
|
|
+ * @return Workbook 返回工作簿
|
|
|
+ * @throws IOException 抛出IO异常
|
|
|
+ */
|
|
|
+ public static Workbook mergeExcel(String sourceExcelPath, String targetExcelPath, int targetSheetIndex, int targetRowNum, int sourceSheetIndex, int skipRow) throws IOException {
|
|
|
+ // 获取源Excel
|
|
|
+ InputStream inputStream = Files.newInputStream(Paths.get(sourceExcelPath));
|
|
|
+ Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
|
|
|
+ Sheet sourceSheet = sourceWorkbook.getSheetAt(sourceSheetIndex);
|
|
|
+
|
|
|
+ // 打开已存在的Excel工作簿
|
|
|
+ Workbook targetWorkbook = WorkbookFactory.create(Files.newInputStream(Paths.get(targetExcelPath)));
|
|
|
+ Sheet targetSheet = targetWorkbook.getSheetAt(targetSheetIndex); // 获取目标sheet
|
|
|
+
|
|
|
+ // 复制样式映射
|
|
|
+ Map<Short, Short> styleMap = copyCellStyle(sourceWorkbook, targetWorkbook, targetSheetIndex, sourceSheetIndex);
|
|
|
+
|
|
|
+ // 复制行和单元格
|
|
|
+ for (Row sourceRow : sourceSheet) {
|
|
|
+ Row targetRow = targetSheet.createRow(targetRowNum + sourceRow.getRowNum());
|
|
|
+ for (Cell sourceCell : sourceRow) {
|
|
|
+ Cell targetCell = targetRow.createCell(sourceCell.getColumnIndex());
|
|
|
+ copyCell(sourceCell, targetCell, targetWorkbook, styleMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 复制数据验证
|
|
|
- copyDataValidations(sourceSheet, targetSheet);
|
|
|
+ copyDataValidations(sourceSheet, targetSheet, skipRow);
|
|
|
|
|
|
sourceWorkbook.close();
|
|
|
inputStream.close();
|
|
@@ -87,7 +126,7 @@ public class EasyExcelUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 合并Excel文件-目标文件为WorkBook方式
|
|
|
+ * 合并Excel文件-目标文件为WorkBook方式且复制数据验证
|
|
|
*
|
|
|
* @param sourceExcelPath 源文件路径(段落模板)
|
|
|
* @param targetWorkbook 目标工作表(主模板)
|
|
@@ -96,7 +135,7 @@ public class EasyExcelUtil {
|
|
|
* @param sourceSheetIndex 源文件被引用的sheet
|
|
|
* @throws IOException 抛出IO异常
|
|
|
*/
|
|
|
- public static void mergeExcel(String sourceExcelPath, Workbook targetWorkbook, int targetSheetIndex, int targetRowNum, int sourceSheetIndex) throws IOException {
|
|
|
+ public static void mergeExcel(String sourceExcelPath, Workbook targetWorkbook, int targetSheetIndex, int targetRowNum, int sourceSheetIndex, int skipRow) throws IOException {
|
|
|
// 获取源Excel
|
|
|
InputStream inputStream = Files.newInputStream(Paths.get(sourceExcelPath));
|
|
|
Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
|
|
@@ -118,7 +157,43 @@ public class EasyExcelUtil {
|
|
|
}
|
|
|
|
|
|
// 复制数据验证
|
|
|
- copyDataValidations(sourceSheet, targetSheet);
|
|
|
+ copyDataValidations(sourceSheet, targetSheet, skipRow);
|
|
|
+
|
|
|
+ sourceWorkbook.close();
|
|
|
+ inputStream.close();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 合并Excel文件-目标文件为WorkBook方式
|
|
|
+ *
|
|
|
+ * @param sourceExcelPath 源文件路径(段落模板)
|
|
|
+ * @param targetWorkbook 目标工作表(主模板)
|
|
|
+ * @param targetSheetIndex 目标sheet索引(要插入的sheet)
|
|
|
+ * @param targetRowNum 目标行索引(要插入的行)
|
|
|
+ * @param sourceSheetIndex 源文件被引用的sheet
|
|
|
+ * @throws IOException 抛出IO异常
|
|
|
+ */
|
|
|
+ public static void mergeExcel(String sourceExcelPath, Workbook targetWorkbook, int targetSheetIndex, int targetRowNum, int sourceSheetIndex) throws IOException {
|
|
|
+ // 获取源Excel
|
|
|
+ InputStream inputStream = Files.newInputStream(Paths.get(sourceExcelPath));
|
|
|
+ Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
|
|
|
+ Sheet sourceSheet = sourceWorkbook.getSheetAt(sourceSheetIndex);
|
|
|
+
|
|
|
+ // 打开已存在的Excel工作簿
|
|
|
+ Sheet targetSheet = targetWorkbook.getSheetAt(targetSheetIndex); // 获取目标sheet
|
|
|
+
|
|
|
+ // 复制样式映射
|
|
|
+ Map<Short, Short> styleMap = copyCellStyle(sourceWorkbook, targetWorkbook, targetSheetIndex, sourceSheetIndex);
|
|
|
+
|
|
|
+ // 复制行和单元格
|
|
|
+ for (Row sourceRow : sourceSheet) {
|
|
|
+ Row targetRow = targetSheet.createRow(targetRowNum + sourceRow.getRowNum());
|
|
|
+ for (Cell sourceCell : sourceRow) {
|
|
|
+ Cell targetCell = targetRow.createCell(sourceCell.getColumnIndex());
|
|
|
+ copyCell(sourceCell, targetCell, targetWorkbook, styleMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
sourceWorkbook.close();
|
|
|
inputStream.close();
|
|
@@ -208,12 +283,15 @@ public class EasyExcelUtil {
|
|
|
*
|
|
|
* @param sourceSheet 源Sheet
|
|
|
* @param targetSheet 目标Sheet
|
|
|
+ * @param skipRow 跳过的行数
|
|
|
*/
|
|
|
- private static void copyDataValidations(Sheet sourceSheet, Sheet targetSheet) {
|
|
|
+ private static void copyDataValidations(Sheet sourceSheet, Sheet targetSheet, int skipRow) {
|
|
|
for (DataValidation validation : sourceSheet.getDataValidations()) {
|
|
|
// 设置要设置数据验证的单元格坐标
|
|
|
- CellRangeAddressList regions = new CellRangeAddressList(validation.getRegions().getCellRangeAddress(0).getFirstRow() + 4,
|
|
|
- validation.getRegions().getCellRangeAddress(0).getLastRow() + 4,
|
|
|
+ // 比如数据验证在第2列第3行那坐标就是(2-3),后续设置到目标excel也是沿用这个坐标
|
|
|
+ // skipRow会在在此基础上加行,比如skipRow是2,那么坐标就是(4-3),skipRow一般用于目标excel和原excel数据验证位置不一样的情况
|
|
|
+ CellRangeAddressList regions = new CellRangeAddressList(validation.getRegions().getCellRangeAddress(0).getFirstRow() + skipRow,
|
|
|
+ validation.getRegions().getCellRangeAddress(0).getLastRow() + skipRow,
|
|
|
validation.getRegions().getCellRangeAddress(0).getFirstColumn(),
|
|
|
validation.getRegions().getCellRangeAddress(0).getLastColumn());
|
|
|
// 获取数据源的数据验证信息
|