public class ExcelReader extends Object implements Closeable
ExcelReader提供一组静态的read(java.nio.file.Path)方法,支持Iterator和Stream+Lambda读取xls和xlsx文件,
你可以像操作集合类一样操作Excel。通过Row.to(java.lang.Class<T>)和Row.too(java.lang.Class<T>)方法可以将行数据转为指定对象,
还可以使用Row.toMap()方法转为LinkedHashMap,同时Row也提供更基础的类似于JDBC方式获取单元格的值。
使用ExcelReader读取文件时不需要提前判断文件格式,Reader已内置类型判断并加载相应的解析器,
ExcelReader默认只能解析xlsx格式,如果需要解析xls则必须将eec-e3-support添加到classpath,它包含一个
BIFF8Reader用于解析BIFF8编码的xls格式文件。为保证功能统一几乎所有接口都由eec定义由support实现,
大多数情况下ExcelReader和BIFF8Reader提供相同的功能,所以用ExcelReader读取excel文件时只需要一份代码
读取过程中可能会产生一些临时文件,比如SharedString索引等临时文件,所以读取结束后需要关闭流并删除临时文件,
建议使用try...with...resource块
一个典型的读取示例如下:
try (ExcelReader reader = ExcelReader.read(path)) {
// 读取所有工作表并打印
reader.sheets().flatMap(Sheet::rows)
.forEach(System.out::println);
} catch (IOException e) { }| 限定符和类型 | 字段和说明 |
|---|---|
static int |
COPY_ON_MERGED
解析模式-复制合并单元格的值
|
protected Drawings |
drawings
图片管理器
|
protected boolean |
hasFormula
是否包含公式标记,此标记不可信,因为它只检查是否包含
calcChain.xml文件,
并不检查单元格中的内嵌公式。 |
protected static org.slf4j.Logger |
LOGGER
LOGGER
|
static Set<String> |
MUST_CHECK_PART
配置必要的安全检查项,解析Excel文件之前会检查是否包含这些必须项,只要有一个不包含就抛
ExcelReadException异常,
可以在外部移除/添加检查项,当前支持的资源类型看请查看Const.ContentType |
protected int |
option
解析模式
0:
VALUE_ONLY
2: VALUE_AND_CALC
4: COPY_ON_MERGED
|
protected Sheet[] |
sheets
保存所有工作表,读取工作表之前必须调用
Sheet.load()方法加载初始信息 |
protected Styles |
styles
全局的样式管理器
|
protected Path |
tempDir
临时文件路径,读文件过程中产生的临时文件
|
static int |
VALUE_AND_CALC
解析模式-解析公式
|
static int |
VALUE_ONLY
解析模式-只解析值(默认)
|
protected ZipFile |
zipFile
Excel原始文件
|
| 限定符 | 构造器和说明 |
|---|---|
protected |
ExcelReader() |
|
ExcelReader(InputStream stream)
以只读"值"的方式读取Excel字节流
|
|
ExcelReader(InputStream stream,
int option)
指定解析模式读取Excel文件
|
|
ExcelReader(InputStream stream,
int bufferSize,
int cacheSize,
int option) |
|
ExcelReader(Path path)
以只读"值"的方式读取指定路径的Excel文件
|
|
ExcelReader(Path path,
int option)
指定解析模式读取Excel文件
|
|
ExcelReader(Path path,
int bufferSize,
int cacheSize,
int option)
指定解析模式读取Excel文件
|
| 限定符和类型 | 方法和说明 |
|---|---|
Sheet[] |
all()
获取全部工作表,通过此方法获取的工作表在读取前需要先调用
load方法 |
static long |
cellRangeToLong(String r)
将单元格坐标转为long类型,Excel单元格坐标由列+行组成如A1, B2等,
转为long类型后第
0-16位为列号17-48位为行号
单元格坐标 | 转换后long值
------------|------------
A1 | 65537
AA10 | 655387
|
protected ContentType |
checkContentType()
解析Content_Types并进行安全检查,必要安全检查不通过将抛
ExcelReadException异常,
必要检查项配置在MUST_CHECK_PART中,外部可以视情况进行添加/移除 |
void |
close()
关闭流并删除临时文件
|
ExcelReader |
copyOnMergeCells()
复制合并单元格,默认情况下合并单元格的值仅存储在第一个单元格中其他单元格没有值。
|
AppInfo |
getAppInfo()
Excel文件基础信息包含作者、日期等信息,在windows操作系统上使用鼠标右键->属性->详细信息查看
|
ZipEntry |
getEntry(String name)
从压缩包中获取一个压缩文件
|
static ZipEntry |
getEntry(ZipFile zipFile,
String name)
从压缩包中获取一个压缩文件,为了兼容windows和linux系统的路径会进行
'/'和'\\'
两种分隔符匹配,如果路径无法匹配则遍历压缩包所有文件并忽略大小写匹配 |
InputStream |
getEntryStream(String name)
从压缩包中获取一个压缩文件字节流
|
protected AppInfo |
getGeneralInfo()
解析
docProps/app.xml和docProps/core.xml文件获取文件基础信息,
比如创建者、创建时间、分类等信息 |
int |
getSize()
获取当前excel包含的工作表数量
|
Styles |
getStyles()
获取一个全局的样式对象
Styles |
ExcelType |
getType()
获取当前Excel的文件类型,返回
xlsx或xls,当文件不是excel时返回unknown |
static ExcelType |
getType(Path path)
判断文件格式,读取少量文件头字节来判断是否为BIFF和ZIP的文件签名
|
boolean |
hasFormula()
已过时。
此方法无法准确反映工作簿是否包含公式,这里仅检查是否包含一个通用的calcChain文件,
某些工具生成的Excel文件不包含calcChain文件而是内嵌在每个单元格中,这部分公式只有在读取单元格时才
明确知道,通过
Row.hasFormula(int)可以精准判断某些单元格是否包含公式。 |
protected ExcelReader |
init(Path path,
int bufferSize,
int cacheSize,
int option)
初始化,初始化过程将进行内容检查,和创建全局属性(样式,字符共享区)以及工作表但不会实际读取工作表
|
List<Drawings.Picture> |
listPictures()
获取Excel包含的所有图片,
Drawings.Picture对象包含工作表的单元格行列信息,最重要的是包含localPath属性,
它是图片的临时路径可以通过此路径复制图片 |
ExcelReader |
parseFormula()
强制解析公式,将所有工作表转换为
CalcSheet,也可以在单个工作表中使用Sheet.asCalcSheet()转换单个工作表 |
static ExcelReader |
read(InputStream stream)
以只读"值"的方式读取Excel字节流
|
static ExcelReader |
read(InputStream stream,
int option)
指定解析模式读取Excel文件
|
static ExcelReader |
read(InputStream stream,
int bufferSize,
int option)
指定解析模式读取Excel字节流
|
static ExcelReader |
read(InputStream stream,
int bufferSize,
int cacheSize,
int option)
指定解析模式读取Excel字节流
|
static ExcelReader |
read(Path path)
以只读"值"的方式读取指定路径的Excel文件
|
static ExcelReader |
read(Path path,
int option)
指定解析模式读取Excel文件
|
static ExcelReader |
read(Path path,
int bufferSize,
int option)
指定解析模式读取Excel文件
|
static ExcelReader |
read(Path path,
int bufferSize,
int cacheSize,
int option)
指定解析模式读取Excel文件,如果文件为
xls格式将加载org.ttzero.excel.reader.BIFF8Reader
如果要支持xls格式需要将eec-e3-support添加进classpath,如果未识别到文件类型则抛ExcelReadException |
Sheet |
sheet(int index)
获取指定位置的工作表,此方法默认
load工作表所以外部无需再次调用load方法 |
Sheet |
sheet(String sheetName)
获取指定名称的工作表,此方法默认
load工作表所以外部无需再次调用load方法 |
protected Sheet |
sheetFactory(int option)
通过OPTION创建相应工作表
|
Stream<Sheet> |
sheets()
返回一个工作表的流,它将按顺序解析当前excel包含所有工作表(含隐藏工作表),
此方法默认
load工作表所以外部无需再次调用load方法 |
static String |
toZipPath(String path)
将string转换为zip允许的路径,将相对路径的前缀去掉
|
protected static final org.slf4j.Logger LOGGER
public static final int VALUE_ONLY
public static final int VALUE_AND_CALC
public static final int COPY_ON_MERGED
protected Sheet[] sheets
Sheet.load()方法加载初始信息protected Path tempDir
protected int option
VALUE_ONLYVALUE_AND_CALCCOPY_ON_MERGEDprotected boolean hasFormula
calcChain.xml文件,
并不检查单元格中的内嵌公式。protected Drawings drawings
protected Styles styles
protected ZipFile zipFile
public static final Set<String> MUST_CHECK_PART
ExcelReadException异常,
可以在外部移除/添加检查项,当前支持的资源类型看请查看Const.ContentTypeprotected ExcelReader()
public ExcelReader(InputStream stream) throws IOException
stream - excel字节流IOException - if I/O error occurpublic ExcelReader(InputStream stream, int option) throws IOException
stream - excel字节流option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选IOException - if I/O error occurpublic ExcelReader(InputStream stream, int bufferSize, int cacheSize, int option) throws IOException
IOExceptionpublic ExcelReader(Path path) throws IOException
path - excel绝对路径IOException - if path not exists or I/O error occurpublic ExcelReader(Path path, int option) throws IOException
path - excel文件路径option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选IOException - if path not exists or I/O error occurpublic ExcelReader(Path path, int bufferSize, int cacheSize, int option) throws IOException
path - excel文件路径bufferSize - 共享字符区的大小,默认64cacheSize - 共享字符区缓存大小,默认512option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选IOException - if path not exists or I/O error occurpublic static ExcelReader read(Path path) throws IOException
path - excel绝对路径ExcelReaderIOException - if path not exists or I/O error occurpublic static ExcelReader read(InputStream stream) throws IOException
stream - excel字节流ExcelReaderIOException - if I/O error occurpublic static ExcelReader read(Path path, int option) throws IOException
path - excel文件路径option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderIOException - if path not exists or I/O error occurpublic static ExcelReader read(InputStream stream, int option) throws IOException
stream - excel字节流option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderIOException - if I/O error occurpublic static ExcelReader read(Path path, int bufferSize, int option) throws IOException
path - excel文件路径bufferSize - 共享字符区的大小,默认64option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderIOException - if path not exists or I/O error occurpublic static ExcelReader read(InputStream stream, int bufferSize, int option) throws IOException
stream - excel字节流bufferSize - 共享字符区的大小,默认64option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderIOException - if I/O error occurpublic static ExcelReader read(InputStream stream, int bufferSize, int cacheSize, int option) throws IOException
stream - excel字节流bufferSize - 共享字符区的大小,默认16cacheSize - 共享字符区的缓存大小,默认512option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderIOException - if I/O error occurpublic Stream<Sheet> sheets()
load工作表所以外部无需再次调用load方法public Sheet sheet(int index)
load工作表所以外部无需再次调用load方法index - 工作表在excel的下标(从0开始)IndexOutOfBoundExceptionpublic Sheet sheet(String sheetName)
load工作表所以外部无需再次调用load方法sheetName - 工作表名nullpublic Sheet[] all()
load方法public int getSize()
public void close()
throws IOException
close 在接口中 Closeableclose 在接口中 AutoCloseableIOException - when fail close readerspublic AppInfo getAppInfo()
AppInfo通过此对象可以获取excel详细属性@Deprecated public boolean hasFormula()
Row.hasFormula(int)可以精准判断某些单元格是否包含公式。true一定包含公式,返回false则不一定不包含public ExcelReader parseFormula()
CalcSheet,也可以在单个工作表中使用Sheet.asCalcSheet()转换单个工作表ExcelReaderpublic ExcelReader copyOnMergeCells()
null值,
需要在业务代码里特殊处理合并单元格,本工具内置处理合并单格,调用此方法可将值复制到合并中的其他单元格中。
本方法将当前所有工作表都转换为MergeSheet,也可以在单个工作表中使用Sheet.asMergeSheet() ()}转换单个工作表,
注意:MergeSheet性能不佳,建议读完合并单元格后使用Sheet.asSheet()将其转换为普通工作表,
它们之间可以无缝切换无需担心漏读或重复读的问题
|---------| |---------| |---------|
| | | 1 | | | 1 | 1 |
| 1 | => |----|----| => |----|----|
| | | | | | 1 | 1 |
|---------| |---------| |---------|
Merged(A1:B2) Default Copy
Value in A1
others are
`null`
ExcelReaderprotected ContentType checkContentType()
ExcelReadException异常,
必要检查项配置在MUST_CHECK_PART中,外部可以视情况进行添加/移除protected ExcelReader init(Path path, int bufferSize, int cacheSize, int option) throws IOException
path - excel文件路径bufferSize - 共享字符区的大小,默认64cacheSize - 共享字符区缓存大小,默认512option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderIOException - if I/O error occurpublic static ExcelReader read(Path path, int bufferSize, int cacheSize, int option) throws IOException
xls格式将加载org.ttzero.excel.reader.BIFF8Reader
如果要支持xls格式需要将eec-e3-support添加进classpath,如果未识别到文件类型则抛ExcelReadExceptionpath - excel文件路径bufferSize - 共享字符区的大小,默认16cacheSize - 共享字符区的缓存大小,默认512option - 解析模式,有VALUE_ONLY, VALUE_AND_CALC, COPY_ON_MERGED三种属性可选ExcelReaderFileNotFoundException - 文件不存在IOException - if I/O error occurprotected Sheet sheetFactory(int option)
option - the reader option.public static ExcelType getType(Path path)
path - 临时文件路径ExcelType枚举,非excel格式时返回ExcelType.UNKNOWN类型protected AppInfo getGeneralInfo()
docProps/app.xml和docProps/core.xml文件获取文件基础信息,
比如创建者、创建时间、分类等信息public static long cellRangeToLong(String r)
0-16位为列号17-48位为行号
单元格坐标 | 转换后long值 ------------|------------ A1 | 65537 AA10 | 655387
r - 单元格坐标public List<Drawings.Picture> listPictures()
Drawings.Picture对象包含工作表的单元格行列信息,最重要的是包含localPath属性,
它是图片的临时路径可以通过此路径复制图片nullpublic ZipEntry getEntry(String name)
name - 压缩文件路径,必须是一个完整的路径ZipEntry 否则返回nullpublic InputStream getEntryStream(String name) throws IOException
name - 压缩文件路径,必须是一个完整的路径InputStream 否则返回nullIOException - if I/O error occur.public static ZipEntry getEntry(ZipFile zipFile, String name)
'/'和'\\'
两种分隔符匹配,如果路径无法匹配则遍历压缩包所有文件并忽略大小写匹配zipFile - 压缩包name - 压缩文件路径,必须是一个完整的路径ZipEntry 否则返回nullCopyright © 2023. All rights reserved.