修复 ExcelUtil.getReader(xxx) 读取Excel后,Excel文件会发生变化

This commit is contained in:
Say.li 2022-03-18 17:06:00 +08:00
parent f52ec39d44
commit 1b6a27aa89
No known key found for this signature in database
GPG Key ID: A8C7258DF25850AF
2 changed files with 96 additions and 22 deletions

View File

@ -63,7 +63,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
* @param sheetIndex sheet序号0表示第一个sheet * @param sheetIndex sheet序号0表示第一个sheet
*/ */
public ExcelReader(File bookFile, int sheetIndex) { public ExcelReader(File bookFile, int sheetIndex) {
this(WorkbookUtil.createBook(bookFile), sheetIndex); this(WorkbookUtil.createBook(bookFile, true), sheetIndex);
} }
/** /**
@ -73,7 +73,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
* @param sheetName sheet名第一个默认是sheet1 * @param sheetName sheet名第一个默认是sheet1
*/ */
public ExcelReader(File bookFile, String sheetName) { public ExcelReader(File bookFile, String sheetName) {
this(WorkbookUtil.createBook(bookFile), sheetName); this(WorkbookUtil.createBook(bookFile, true), sheetName);
} }
/** /**

View File

@ -22,29 +22,52 @@ import java.io.OutputStream;
* *
* @author looly * @author looly
* @since 4.0.7 * @since 4.0.7
*
*/ */
public class WorkbookUtil { public class WorkbookUtil {
/** /**
* 创建或加载工作簿 * 创建或加载工作簿读写模式
* *
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径 * @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @return {@link Workbook} * @return {@link Workbook}
* @since 3.1.1 * @since 3.1.1
*/ */
public static Workbook createBook(String excelFilePath) { public static Workbook createBook(String excelFilePath) {
return createBook(FileUtil.file(excelFilePath), null); return createBook(excelFilePath, false);
} }
/** /**
* 创建或加载工作簿 * 创建或加载工作簿
* *
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @param readOnly 是否只读模式打开true:不可编辑false:可编辑
* @return {@link Workbook}
* @since 3.1.1
*/
public static Workbook createBook(String excelFilePath, boolean readOnly) {
return createBook(FileUtil.file(excelFilePath), null, readOnly);
}
/**
* 创建或加载工作簿读写模式
*
* @param excelFile Excel文件 * @param excelFile Excel文件
* @return {@link Workbook} * @return {@link Workbook}
*/ */
public static Workbook createBook(File excelFile) { public static Workbook createBook(File excelFile) {
return createBook(excelFile, null); return createBook(excelFile, false);
}
/**
* 创建或加载工作簿
*
* @param excelFile Excel文件
* @param readOnly 是否只读模式打开true:不可编辑false:可编辑
* @return {@link Workbook}
*/
public static Workbook createBook(File excelFile, boolean readOnly) {
return createBook(excelFile, null, readOnly);
} }
/** /**
@ -73,15 +96,27 @@ public class WorkbookUtil {
} }
/** /**
* 创建或加载工作簿只读模式 * 创建或加载工作簿读写模式
* *
* @param excelFile Excel文件 * @param excelFile Excel文件
* @param password Excel工作簿密码如果无密码传{@code null} * @param password Excel工作簿密码如果无密码传{@code null}
* @return {@link Workbook} * @return {@link Workbook}
*/ */
public static Workbook createBook(File excelFile, String password) { public static Workbook createBook(File excelFile, String password) {
return createBook(excelFile, password, false);
}
/**
* 创建或加载工作簿
*
* @param excelFile Excel文件
* @param password Excel工作簿密码如果无密码传{@code null}
* @param readOnly 是否只读模式打开true:不可编辑false:可编辑
* @return {@link Workbook}
*/
public static Workbook createBook(File excelFile, String password, boolean readOnly) {
try { try {
return WorkbookFactory.create(excelFile, password); return WorkbookFactory.create(excelFile, password, readOnly);
} catch (Exception e) { } catch (Exception e) {
throw new POIException(e); throw new POIException(e);
} }
@ -133,29 +168,54 @@ public class WorkbookUtil {
} }
/** /**
* 创建或加载SXSSFWorkbook工作簿 * 创建或加载SXSSFWorkbook工作簿读写模式
* *
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径 * @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @return {@link SXSSFWorkbook} * @return {@link SXSSFWorkbook}
* @since 4.1.13 * @since 4.1.13
*/ */
public static SXSSFWorkbook createSXSSFBook(String excelFilePath) { public static SXSSFWorkbook createSXSSFBook(String excelFilePath) {
return createSXSSFBook(FileUtil.file(excelFilePath), null); return createSXSSFBook(excelFilePath, false);
} }
/** /**
* 创建或加载SXSSFWorkbook工作簿 * 创建或加载SXSSFWorkbook工作簿
* *
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @param readOnly 是否只读模式打开true:不可编辑false:可编辑
* @return {@link SXSSFWorkbook}
* @since 4.1.13
*/
public static SXSSFWorkbook createSXSSFBook(String excelFilePath, boolean readOnly) {
return createSXSSFBook(FileUtil.file(excelFilePath), null, readOnly);
}
/**
* 创建或加载SXSSFWorkbook工作簿读写模式
*
* @param excelFile Excel文件 * @param excelFile Excel文件
* @return {@link SXSSFWorkbook} * @return {@link SXSSFWorkbook}
* @since 4.1.13 * @since 4.1.13
*/ */
public static SXSSFWorkbook createSXSSFBook(File excelFile) { public static SXSSFWorkbook createSXSSFBook(File excelFile) {
return createSXSSFBook(excelFile, null); return createSXSSFBook(excelFile, false);
} }
/** /**
* 创建或加载SXSSFWorkbook工作簿只读模式 * 创建或加载SXSSFWorkbook工作簿
*
* @param excelFile Excel文件
* @param readOnly 是否只读模式打开true:不可编辑false:可编辑
* @return {@link SXSSFWorkbook}
* @since 4.1.13
*/
public static SXSSFWorkbook createSXSSFBook(File excelFile, boolean readOnly) {
return createSXSSFBook(excelFile, null, readOnly);
}
/**
* 创建或加载SXSSFWorkbook工作簿读写模式
* *
* @param excelFile Excel文件 * @param excelFile Excel文件
* @param password Excel工作簿密码如果无密码传{@code null} * @param password Excel工作簿密码如果无密码传{@code null}
@ -163,7 +223,21 @@ public class WorkbookUtil {
* @since 4.1.13 * @since 4.1.13
*/ */
public static SXSSFWorkbook createSXSSFBook(File excelFile, String password) { public static SXSSFWorkbook createSXSSFBook(File excelFile, String password) {
return toSXSSFBook(createBook(excelFile, password)); return createSXSSFBook(excelFile, password, false);
}
/**
* 创建或加载SXSSFWorkbook工作簿
*
* @param excelFile Excel文件
* @param password Excel工作簿密码如果无密码传{@code null}
* @param readOnly 是否只读模式打开true:不可编辑false:可编辑
* @return {@link SXSSFWorkbook}
* @since 4.1.13
*/
public static SXSSFWorkbook createSXSSFBook(File excelFile, String password, boolean readOnly) {
return toSXSSFBook(createBook(excelFile, password, readOnly));
} }
/** /**
@ -271,7 +345,6 @@ public class WorkbookUtil {
} }
/** /**
*
* sheet是否为空 * sheet是否为空
* *
* @param sheet {@link Sheet} * @param sheet {@link Sheet}
@ -283,6 +356,7 @@ public class WorkbookUtil {
} }
// -------------------------------------------------------------------------------------------------------- Private method start // -------------------------------------------------------------------------------------------------------- Private method start
/** /**
* 将普通工作簿转换为SXSSFWorkbook * 将普通工作簿转换为SXSSFWorkbook
* *