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) { }
参考文档:
| 限定符和类型 | 字段和说明 |
|---|---|
protected Drawings |
drawings
图片管理器
|
protected static org.slf4j.Logger |
LOGGER
LOGGER
|
static Set<String> |
MUST_CHECK_PART
配置必要的安全检查项,解析Excel文件之前会检查是否包含这些必须项,只要有一个不包含就抛
ExcelReadException异常,
可以在外部移除/添加检查项,当前支持的资源类型看请查看Const.ContentType |
protected Sheet[] |
sheets
保存所有工作表,读取工作表之前必须调用
Sheet.load()方法加载初始信息 |
protected Styles |
styles
全局的样式管理器
|
protected Path |
tempDir
临时文件路径,读文件过程中产生的临时文件
|
protected ZipFile |
zipFile
Excel原始文件
|
| 限定符 | 构造器和说明 |
|---|---|
protected |
ExcelReader() |
|
ExcelReader(InputStream stream)
指定解析模式读取Excel文件
|
|
ExcelReader(Path path)
以只读"值"的方式读取指定路径的Excel文件
|
| 限定符和类型 | 方法和说明 |
|---|---|
Sheet[] |
all()
获取全部工作表,通过此方法获取的工作表在读取前需要先调用
load方法 |
protected ContentType |
checkContentType()
解析Content_Types并进行安全检查,必要安全检查不通过将抛
ExcelReadException异常,
必要检查项配置在MUST_CHECK_PART中,外部可以视情况进行添加/移除 |
void |
close()
关闭流并删除临时文件
|
static long |
coordinateToLong(String r)
将单元格坐标转为long类型,Excel单元格坐标由列+行组成如A1, B2等,
转为long类型后第
0-16位为列号17-48位为行号
单元格坐标 | 转换后long值
------------+------------
A1 | 65537
AA10 | 655387
|
AppInfo |
getAppInfo()
Excel文件基础信息包含作者、日期等信息,在windows操作系统上使用鼠标右键->属性->详细信息查看
|
CustomProperties |
getCustomProperties()
获取自定义属性
返回数据类型说明,时间返回
java.util.Date,布尔值返回Boolean,
整数类型分情况返回Integer或Long,浮点数返回BigDecimal |
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文件获取文件基础信息,
比如创建者、创建时间、分类等信息 |
SharedStrings |
getSharedStrings()
获取Shared String Table
|
int |
getSheetCount()
获取当前excel包含的工作表数量
|
Styles |
getStyles()
获取一个全局的样式对象
Styles |
ExcelType |
getType()
获取当前Excel的文件类型,返回
xlsx或xls,当文件不是excel时返回unknown |
static ExcelType |
getType(Path path)
判断文件格式,读取少量文件头字节来判断是否为BIFF和ZIP的文件签名
|
protected ExcelReader |
init(Path path)
初始化,初始化过程将进行内容检查,和创建全局属性(样式,字符共享区)以及工作表但不会实际读取工作表
|
List<Drawings.Picture> |
listPictures()
获取Excel包含的所有图片,
Drawings.Picture对象包含工作表的单元格行列信息,最重要的是包含localPath属性,
它是图片的临时路径可以通过此路径复制图片 |
static ExcelReader |
read(InputStream stream)
以只读"值"的方式读取Excel字节流
|
static ExcelReader |
read(Path path)
|
Sheet |
sheet(int index)
获取指定位置的工作表,此方法默认
load工作表所以外部无需再次调用load方法 |
Sheet |
sheet(String sheetName)
获取指定名称的工作表,此方法默认
load工作表所以外部无需再次调用load方法 |
protected Sheet |
sheetFactory()
通过OPTION创建相应工作表
|
Stream<Sheet> |
sheets()
返回一个工作表的流,它将按顺序解析当前excel包含所有工作表(含隐藏工作表),
此方法默认
load工作表所以外部无需再次调用load方法 |
static String |
toZipPath(String path)
将string转换为zip允许的路径,将相对路径的前缀去掉
|
protected static final org.slf4j.Logger LOGGER
protected Sheet[] sheets
Sheet.load()方法加载初始信息protected Path tempDir
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 - 读取异常public ExcelReader(Path path) throws IOException
path - excel绝对路径IOException - 读取异常public static ExcelReader read(Path path) throws IOException
path - excel文件路径ExcelReaderFileNotFoundException - 文件不存在IOException - 读取异常public static ExcelReader read(InputStream stream) throws IOException
stream - excel字节流ExcelReaderIOException - 读取异常public 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 getSheetCount()
public void close()
throws IOException
close 在接口中 Closeableclose 在接口中 AutoCloseableIOException - when fail close readerspublic AppInfo getAppInfo()
AppInfo通过此对象可以获取excel详细属性protected ContentType checkContentType()
ExcelReadException异常,
必要检查项配置在MUST_CHECK_PART中,外部可以视情况进行添加/移除protected ExcelReader init(Path path) throws IOException
path - excel文件路径ExcelReaderIOException - 读取异常protected Sheet sheetFactory()
public SharedStrings getSharedStrings()
public static ExcelType getType(Path path)
path - 临时文件路径ExcelType枚举,非excel格式时返回ExcelType.UNKNOWN类型protected AppInfo getGeneralInfo()
docProps/app.xml和docProps/core.xml文件获取文件基础信息,
比如创建者、创建时间、分类等信息public static long coordinateToLong(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 - 读取异常public static ZipEntry getEntry(ZipFile zipFile, String name)
'/'和'\\'
两种分隔符匹配,如果路径无法匹配则遍历压缩包所有文件并忽略大小写匹配zipFile - 压缩包name - 压缩文件路径,必须是一个完整的路径ZipEntry 否则返回nullpublic static String toZipPath(String path)
path - 实体路径public CustomProperties getCustomProperties()
返回数据类型说明,时间返回java.util.Date,布尔值返回Boolean,
整数类型分情况返回Integer或Long,浮点数返回BigDecimal
nullCopyright © 2024. All rights reserved.