Merge pull request #2204 from 120011676/v5-dev

修复 ExcelUtil.getReader(xxx) 读取Excel后,Excel文件会发生变化
This commit is contained in:
Golden Looly 2022-03-19 09:13:59 +08:00 committed by GitHub
commit 85d8c3f0ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 26 deletions

View File

@ -63,7 +63,7 @@ public class ExcelReader extends ExcelBase<ExcelReader> {
* @param sheetIndex sheet序号0表示第一个sheet
*/
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
*/
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
* @since 4.0.7
*
*/
public class WorkbookUtil {
/**
* 创建或加载工作簿
* 创建或加载工作簿读写模式
*
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @return {@link Workbook}
* @since 3.1.1
*/
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文件
* @return {@link Workbook}
*/
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 password Excel工作簿密码如果无密码传{@code null}
* @return {@link Workbook}
*/
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 {
return WorkbookFactory.create(excelFile, password);
return WorkbookFactory.create(excelFile, password, readOnly);
} catch (Exception e) {
throw new POIException(e);
}
@ -110,7 +145,7 @@ public class WorkbookUtil {
return WorkbookFactory.create(IoUtil.toMarkSupportStream(in), password);
} catch (Exception e) {
throw new POIException(e);
} finally{
} finally {
IoUtil.close(in);
}
}
@ -133,29 +168,54 @@ public class WorkbookUtil {
}
/**
* 创建或加载SXSSFWorkbook工作簿
* 创建或加载SXSSFWorkbook工作簿读写模式
*
* @param excelFilePath Excel文件路径绝对路径或相对于ClassPath路径
* @return {@link SXSSFWorkbook}
* @since 4.1.13
*/
public static SXSSFWorkbook createSXSSFBook(String excelFilePath) {
return createSXSSFBook(FileUtil.file(excelFilePath), null);
return createSXSSFBook(excelFilePath, false);
}
/**
* 创建或加载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文件
* @return {@link SXSSFWorkbook}
* @since 4.1.13
*/
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 password Excel工作簿密码如果无密码传{@code null}
@ -163,7 +223,21 @@ public class WorkbookUtil {
* @since 4.1.13
*/
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是否为空
*
* @param sheet {@link Sheet}
@ -283,6 +356,7 @@ public class WorkbookUtil {
}
// -------------------------------------------------------------------------------------------------------- Private method start
/**
* 将普通工作簿转换为SXSSFWorkbook
*

View File

@ -5,6 +5,9 @@ import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;
import java.util.List;
import java.util.Map;
public class ExcelUtilTest {
@Test
@ -38,7 +41,7 @@ public class ExcelUtilTest {
}
@Test
public void toLocationTest(){
public void toLocationTest() {
final CellLocation a11 = ExcelUtil.toLocation("A11");
Assert.assertEquals(0, a11.getX());
Assert.assertEquals(10, a11.getY());
@ -46,10 +49,15 @@ public class ExcelUtilTest {
@Test
@Ignore
public void readAndWriteTest(){
ExcelReader reader = ExcelUtil.getReader("d:\\test/select.xls");
ExcelWriter writer = reader.getWriter();
public void readAndWriteTest() {
String filepath = "d:\\test/select.xls";
ExcelWriter writer = ExcelUtil.getWriter(filepath);
writer.writeCellValue(1, 2, "设置值");
writer.close();
ExcelReader reader = ExcelUtil.getReader(filepath);
List<Map<String, Object>> map = reader.readAll();
reader.close();
Assert.assertNotNull(map);
Assert.assertFalse(map.isEmpty());
}
}