mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
Merge pull request #2204 from 120011676/v5-dev
修复 ExcelUtil.getReader(xxx) 读取Excel后,Excel文件会发生变化
This commit is contained in:
commit
85d8c3f0ac
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
@ -110,7 +145,7 @@ public class WorkbookUtil {
|
|||||||
return WorkbookFactory.create(IoUtil.toMarkSupportStream(in), password);
|
return WorkbookFactory.create(IoUtil.toMarkSupportStream(in), password);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new POIException(e);
|
throw new POIException(e);
|
||||||
} finally{
|
} finally {
|
||||||
IoUtil.close(in);
|
IoUtil.close(in);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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
|
||||||
*
|
*
|
||||||
|
@ -5,6 +5,9 @@ import org.junit.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class ExcelUtilTest {
|
public class ExcelUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -38,7 +41,7 @@ public class ExcelUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toLocationTest(){
|
public void toLocationTest() {
|
||||||
final CellLocation a11 = ExcelUtil.toLocation("A11");
|
final CellLocation a11 = ExcelUtil.toLocation("A11");
|
||||||
Assert.assertEquals(0, a11.getX());
|
Assert.assertEquals(0, a11.getX());
|
||||||
Assert.assertEquals(10, a11.getY());
|
Assert.assertEquals(10, a11.getY());
|
||||||
@ -46,10 +49,15 @@ public class ExcelUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void readAndWriteTest(){
|
public void readAndWriteTest() {
|
||||||
ExcelReader reader = ExcelUtil.getReader("d:\\test/select.xls");
|
String filepath = "d:\\test/select.xls";
|
||||||
ExcelWriter writer = reader.getWriter();
|
ExcelWriter writer = ExcelUtil.getWriter(filepath);
|
||||||
writer.writeCellValue(1, 2, "设置值");
|
writer.writeCellValue(1, 2, "设置值");
|
||||||
writer.close();
|
writer.close();
|
||||||
|
ExcelReader reader = ExcelUtil.getReader(filepath);
|
||||||
|
List<Map<String, Object>> map = reader.readAll();
|
||||||
|
reader.close();
|
||||||
|
Assert.assertNotNull(map);
|
||||||
|
Assert.assertFalse(map.isEmpty());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user