diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e455a220..dfcc90594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * 【http 】 SoapClient.sendForResponse改为public(issue#I466NN@Gitee) * 【core 】 XmlUtil增加append重载(issue#I466Q0@Gitee) * 【poi 】 增加EscapeStrCellSetter(issue#I466ZZ@Gitee) +* 【poi 】 ExcelBase增加renameSheet、cloneSheet(issue#I466ZZ@Gitee) ### 🐞Bug修复 diff --git a/hutool-core/src/main/java/cn/hutool/core/text/csv/CsvConfig.java b/hutool-core/src/main/java/cn/hutool/core/text/csv/CsvConfig.java index 24f5f5a84..7f3584975 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/csv/CsvConfig.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/csv/CsvConfig.java @@ -11,7 +11,7 @@ import java.io.Serializable; * @author looly * @since 4.0.5 */ -public class CsvConfig> implements Serializable { +public class CsvConfig> implements Serializable { private static final long serialVersionUID = -8069578249066158459L; /** diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java index cffbe98d3..b366514dd 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelBase.java @@ -12,6 +12,7 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.Closeable; import java.util.ArrayList; @@ -107,6 +108,21 @@ public class ExcelBase> implements Closeable { return this.sheet; } + + /** + * 重命名当前sheet + * + * @param newName 新名字 + * @return this + * @see Workbook#setSheetName(int, String) + * @since 5.7.10 + */ + @SuppressWarnings("unchecked") + public T renameSheet(String newName) { + this.workbook.setSheetName(this.workbook.getSheetIndex(this.sheet), newName); + return (T) this; + } + /** * 自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。
* 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet。 @@ -144,6 +160,31 @@ public class ExcelBase> implements Closeable { return (T) this; } + /** + * 复制当前sheet为新sheet + * + * @param sheetIndex sheet位置 + * @param newSheetName 新sheet名 + * @param setAsCurrentSheet 是否切换为当前sheet + * @return this + * @since 5.7.10 + */ + @SuppressWarnings("unchecked") + public T cloneSheet(int sheetIndex, String newSheetName, boolean setAsCurrentSheet) { + Sheet sheet; + if (this.workbook instanceof XSSFWorkbook) { + XSSFWorkbook workbook = (XSSFWorkbook) this.workbook; + sheet = workbook.cloneSheet(sheetIndex, newSheetName); + } else { + sheet = this.workbook.cloneSheet(sheetIndex); + this.workbook.setSheetName(sheetIndex, newSheetName); + } + if (setAsCurrentSheet) { + this.sheet = sheet; + } + return (T) this; + } + /** * 获取指定坐标单元格,单元格不存在时返回{@code null} * @@ -281,7 +322,7 @@ public class ExcelBase> implements Closeable { */ public CellStyle createCellStyle(int x, int y) { final Cell cell = getOrCreateCell(x, y); - final CellStyle cellStyle = this.workbook.createCellStyle(); + final CellStyle cellStyle = this.workbook.createCellStyle(); cell.setCellStyle(cellStyle); return cellStyle; } @@ -293,7 +334,7 @@ public class ExcelBase> implements Closeable { * @see Workbook#createCellStyle() * @since 5.4.0 */ - public CellStyle createCellStyle(){ + public CellStyle createCellStyle() { return StyleUtil.createCellStyle(this.workbook); }