mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code for poi
This commit is contained in:
parent
60efd24469
commit
4c278dcccb
@ -2,7 +2,6 @@ package cn.hutool.poi.excel;
|
|||||||
|
|
||||||
import cn.hutool.core.exceptions.DependencyException;
|
import cn.hutool.core.exceptions.DependencyException;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.io.IoUtil;
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.ReUtil;
|
import cn.hutool.core.util.ReUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
@ -10,6 +9,8 @@ import cn.hutool.poi.PoiChecker;
|
|||||||
import cn.hutool.poi.excel.cell.CellLocation;
|
import cn.hutool.poi.excel.cell.CellLocation;
|
||||||
import cn.hutool.poi.excel.sax.Excel03SaxReader;
|
import cn.hutool.poi.excel.sax.Excel03SaxReader;
|
||||||
import cn.hutool.poi.excel.sax.Excel07SaxReader;
|
import cn.hutool.poi.excel.sax.Excel07SaxReader;
|
||||||
|
import cn.hutool.poi.excel.sax.ExcelSaxReader;
|
||||||
|
import cn.hutool.poi.excel.sax.ExcelSaxUtil;
|
||||||
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -28,59 +29,92 @@ public class ExcelUtil {
|
|||||||
* 通过Sax方式读取Excel,同时支持03和07格式
|
* 通过Sax方式读取Excel,同时支持03和07格式
|
||||||
*
|
*
|
||||||
* @param path Excel文件路径
|
* @param path Excel文件路径
|
||||||
* @param sheetIndex sheet序号
|
* @param rid sheet rid,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
*/
|
*/
|
||||||
public static void readBySax(String path, int sheetIndex, RowHandler rowHandler) {
|
public static void readBySax(String path, int rid, RowHandler rowHandler) {
|
||||||
readBySax(FileUtil.file(path), sheetIndex, rowHandler);
|
readBySax(FileUtil.file(path), rid, rowHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过Sax方式读取Excel,同时支持03和07格式
|
||||||
|
*
|
||||||
|
* @param path Excel文件路径
|
||||||
|
* @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||||
|
* @param rowHandler 行处理器
|
||||||
|
* @since 5.4.4
|
||||||
|
*/
|
||||||
|
public static void readBySax(String path, String idOrRid, RowHandler rowHandler) {
|
||||||
|
readBySax(FileUtil.file(path), idOrRid, rowHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过Sax方式读取Excel,同时支持03和07格式
|
* 通过Sax方式读取Excel,同时支持03和07格式
|
||||||
*
|
*
|
||||||
* @param file Excel文件
|
* @param file Excel文件
|
||||||
* @param sheetIndex sheet序号
|
* @param rid sheet rid,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
*/
|
*/
|
||||||
public static void readBySax(File file, int sheetIndex, RowHandler rowHandler) {
|
public static void readBySax(File file, int rid, RowHandler rowHandler) {
|
||||||
if (ExcelFileUtil.isXlsx(file)) {
|
final ExcelSaxReader<?> reader = ExcelSaxUtil.createSaxReader(ExcelFileUtil.isXlsx(file), rowHandler);
|
||||||
read07BySax(file, sheetIndex, rowHandler);
|
reader.read(file, rid);
|
||||||
} else {
|
}
|
||||||
read03BySax(file, sheetIndex, rowHandler);
|
|
||||||
}
|
/**
|
||||||
|
* 通过Sax方式读取Excel,同时支持03和07格式
|
||||||
|
*
|
||||||
|
* @param file Excel文件
|
||||||
|
* @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||||
|
* @param rowHandler 行处理器
|
||||||
|
* @since 5.4.4
|
||||||
|
*/
|
||||||
|
public static void readBySax(File file, String idOrRid, RowHandler rowHandler) {
|
||||||
|
final ExcelSaxReader<?> reader = ExcelSaxUtil.createSaxReader(ExcelFileUtil.isXlsx(file), rowHandler);
|
||||||
|
reader.read(file, idOrRid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过Sax方式读取Excel,同时支持03和07格式
|
* 通过Sax方式读取Excel,同时支持03和07格式
|
||||||
*
|
*
|
||||||
* @param in Excel流
|
* @param in Excel流
|
||||||
* @param sheetIndex sheet序号
|
* @param rid sheet rid,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
*/
|
*/
|
||||||
public static void readBySax(InputStream in, int sheetIndex, RowHandler rowHandler) {
|
public static void readBySax(InputStream in, int rid, RowHandler rowHandler) {
|
||||||
in = IoUtil.toMarkSupportStream(in);
|
final ExcelSaxReader<?> reader = ExcelSaxUtil.createSaxReader(ExcelFileUtil.isXlsx(in), rowHandler);
|
||||||
if (ExcelFileUtil.isXlsx(in)) {
|
reader.read(in, rid);
|
||||||
read07BySax(in, sheetIndex, rowHandler);
|
}
|
||||||
} else {
|
|
||||||
read03BySax(in, sheetIndex, rowHandler);
|
/**
|
||||||
}
|
* 通过Sax方式读取Excel,同时支持03和07格式
|
||||||
|
*
|
||||||
|
* @param in Excel流
|
||||||
|
* @param idOrRid Excel中的sheet id或者rid编号,rid必须加rId前缀,例如rId1,如果为-1处理所有编号的sheet
|
||||||
|
* @param rowHandler 行处理器
|
||||||
|
* @since 5.4.4
|
||||||
|
*/
|
||||||
|
public static void readBySax(InputStream in, String idOrRid, RowHandler rowHandler) {
|
||||||
|
final ExcelSaxReader<?> reader = ExcelSaxUtil.createSaxReader(ExcelFileUtil.isXlsx(in), rowHandler);
|
||||||
|
reader.read(in, idOrRid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sax方式读取Excel07
|
* Sax方式读取Excel07
|
||||||
*
|
*
|
||||||
* @param in 输入流
|
* @param in 输入流
|
||||||
* @param sheetIndex Sheet索引,-1表示全部Sheet, 0表示第一个Sheet
|
* @param rid Sheet rid,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @return {@link Excel07SaxReader}
|
* @return {@link Excel07SaxReader}
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @deprecated 请使用 {@link #readBySax(InputStream, int, RowHandler)}
|
||||||
*/
|
*/
|
||||||
public static Excel07SaxReader read07BySax(InputStream in, int sheetIndex, RowHandler rowHandler) {
|
@Deprecated
|
||||||
|
public static Excel07SaxReader read07BySax(InputStream in, int rid, RowHandler rowHandler) {
|
||||||
try {
|
try {
|
||||||
return new Excel07SaxReader(rowHandler).read(in, sheetIndex);
|
return new Excel07SaxReader(rowHandler).read(in, rid);
|
||||||
} catch (NoClassDefFoundError e) {
|
} catch (NoClassDefFoundError e) {
|
||||||
throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
|
throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
|
||||||
}
|
}
|
||||||
@ -90,14 +124,16 @@ public class ExcelUtil {
|
|||||||
* Sax方式读取Excel07
|
* Sax方式读取Excel07
|
||||||
*
|
*
|
||||||
* @param file 文件
|
* @param file 文件
|
||||||
* @param sheetIndex Sheet索引,-1表示全部Sheet, 0表示第一个Sheet
|
* @param rid Sheet rid,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @return {@link Excel07SaxReader}
|
* @return {@link Excel07SaxReader}
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @deprecated 请使用 {@link #readBySax(File, int, RowHandler)}
|
||||||
*/
|
*/
|
||||||
public static Excel07SaxReader read07BySax(File file, int sheetIndex, RowHandler rowHandler) {
|
@Deprecated
|
||||||
|
public static Excel07SaxReader read07BySax(File file, int rid, RowHandler rowHandler) {
|
||||||
try {
|
try {
|
||||||
return new Excel07SaxReader(rowHandler).read(file, sheetIndex);
|
return new Excel07SaxReader(rowHandler).read(file, rid);
|
||||||
} catch (NoClassDefFoundError e) {
|
} catch (NoClassDefFoundError e) {
|
||||||
throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
|
throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
|
||||||
}
|
}
|
||||||
@ -107,14 +143,16 @@ public class ExcelUtil {
|
|||||||
* Sax方式读取Excel07
|
* Sax方式读取Excel07
|
||||||
*
|
*
|
||||||
* @param path 路径
|
* @param path 路径
|
||||||
* @param sheetIndex Sheet索引,-1表示全部Sheet, 0表示第一个Sheet
|
* @param rid Sheet rid,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @return {@link Excel07SaxReader}
|
* @return {@link Excel07SaxReader}
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @deprecated 请使用 {@link #readBySax(String, int, RowHandler)}
|
||||||
*/
|
*/
|
||||||
public static Excel07SaxReader read07BySax(String path, int sheetIndex, RowHandler rowHandler) {
|
@Deprecated
|
||||||
|
public static Excel07SaxReader read07BySax(String path, int rid, RowHandler rowHandler) {
|
||||||
try {
|
try {
|
||||||
return new Excel07SaxReader(rowHandler).read(path, sheetIndex);
|
return new Excel07SaxReader(rowHandler).read(path, rid);
|
||||||
} catch (NoClassDefFoundError e) {
|
} catch (NoClassDefFoundError e) {
|
||||||
throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
|
throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
|
||||||
}
|
}
|
||||||
@ -126,9 +164,11 @@ public class ExcelUtil {
|
|||||||
* @param in 输入流
|
* @param in 输入流
|
||||||
* @param sheetIndex Sheet索引,-1表示全部Sheet, 0表示第一个Sheet
|
* @param sheetIndex Sheet索引,-1表示全部Sheet, 0表示第一个Sheet
|
||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @return {@link Excel07SaxReader}
|
* @return {@link Excel03SaxReader}
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @deprecated 请使用 {@link #readBySax(InputStream, int, RowHandler)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static Excel03SaxReader read03BySax(InputStream in, int sheetIndex, RowHandler rowHandler) {
|
public static Excel03SaxReader read03BySax(InputStream in, int sheetIndex, RowHandler rowHandler) {
|
||||||
try {
|
try {
|
||||||
return new Excel03SaxReader(rowHandler).read(in, sheetIndex);
|
return new Excel03SaxReader(rowHandler).read(in, sheetIndex);
|
||||||
@ -145,7 +185,9 @@ public class ExcelUtil {
|
|||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @return {@link Excel03SaxReader}
|
* @return {@link Excel03SaxReader}
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @deprecated 请使用 {@link #readBySax(File, int, RowHandler)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static Excel03SaxReader read03BySax(File file, int sheetIndex, RowHandler rowHandler) {
|
public static Excel03SaxReader read03BySax(File file, int sheetIndex, RowHandler rowHandler) {
|
||||||
try {
|
try {
|
||||||
return new Excel03SaxReader(rowHandler).read(file, sheetIndex);
|
return new Excel03SaxReader(rowHandler).read(file, sheetIndex);
|
||||||
@ -162,7 +204,9 @@ public class ExcelUtil {
|
|||||||
* @param rowHandler 行处理器
|
* @param rowHandler 行处理器
|
||||||
* @return {@link Excel03SaxReader}
|
* @return {@link Excel03SaxReader}
|
||||||
* @since 3.2.0
|
* @since 3.2.0
|
||||||
|
* @deprecated 请使用 {@link #readBySax(String, int, RowHandler)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public static Excel03SaxReader read03BySax(String path, int sheetIndex, RowHandler rowHandler) {
|
public static Excel03SaxReader read03BySax(String path, int sheetIndex, RowHandler rowHandler) {
|
||||||
try {
|
try {
|
||||||
return new Excel03SaxReader(rowHandler).read(path, sheetIndex);
|
return new Excel03SaxReader(rowHandler).read(path, sheetIndex);
|
||||||
|
@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUtil;
|
|||||||
import cn.hutool.core.exceptions.DependencyException;
|
import cn.hutool.core.exceptions.DependencyException;
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.poi.excel.sax.handler.RowHandler;
|
||||||
import cn.hutool.poi.exceptions.POIException;
|
import cn.hutool.poi.exceptions.POIException;
|
||||||
import org.apache.poi.ooxml.util.SAXHelper;
|
import org.apache.poi.ooxml.util.SAXHelper;
|
||||||
import org.apache.poi.ss.usermodel.DataFormatter;
|
import org.apache.poi.ss.usermodel.DataFormatter;
|
||||||
@ -192,6 +193,20 @@ public class ExcelSaxUtil {
|
|||||||
return DateUtil.date(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value, false));
|
return DateUtil.date(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 {@link ExcelSaxReader}
|
||||||
|
*
|
||||||
|
* @param isXlsx 是否为xlsx格式(07格式)
|
||||||
|
* @param rowHandler 行处理器
|
||||||
|
* @return {@link ExcelSaxReader}
|
||||||
|
* @since 5.4.4
|
||||||
|
*/
|
||||||
|
public static ExcelSaxReader<?> createSaxReader(boolean isXlsx, RowHandler rowHandler) {
|
||||||
|
return isXlsx
|
||||||
|
? new Excel07SaxReader(rowHandler)
|
||||||
|
: new Excel03SaxReader(rowHandler);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数字类型值
|
* 获取数字类型值
|
||||||
*
|
*
|
||||||
|
@ -25,7 +25,7 @@ public class ExcelSaxReadTest {
|
|||||||
@Test
|
@Test
|
||||||
public void excel07Test() {
|
public void excel07Test() {
|
||||||
// 工具化快速读取
|
// 工具化快速读取
|
||||||
ExcelUtil.read07BySax("aaa.xlsx", 0, createRowHandler());
|
ExcelUtil.readBySax("aaa.xlsx", 0, createRowHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -33,7 +33,7 @@ public class ExcelSaxReadTest {
|
|||||||
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
|
||||||
reader.read("aaa.xls", 1);
|
reader.read("aaa.xls", 1);
|
||||||
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
|
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
|
||||||
ExcelUtil.read03BySax("aaa.xls", 1, createRowHandler());
|
ExcelUtil.readBySax("aaa.xls", 1, createRowHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -60,7 +60,7 @@ public class ExcelSaxReadTest {
|
|||||||
|
|
||||||
private RowHandler createRowHandler() {
|
private RowHandler createRowHandler() {
|
||||||
return (sheetIndex, rowIndex, rowlist) -> {
|
return (sheetIndex, rowIndex, rowlist) -> {
|
||||||
// Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
|
// Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
|
||||||
if (5 != rowIndex && 6 != rowIndex) {
|
if (5 != rowIndex && 6 != rowIndex) {
|
||||||
// 测试样例中除第五行、第六行都为非空行
|
// 测试样例中除第五行、第六行都为非空行
|
||||||
Assert.assertTrue(CollUtil.isNotEmpty(rowlist));
|
Assert.assertTrue(CollUtil.isNotEmpty(rowlist));
|
||||||
@ -105,14 +105,14 @@ public class ExcelSaxReadTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void dateReadTest(){
|
public void dateReadTest() {
|
||||||
ExcelUtil.readBySax("d:/test/sax_date_test.xlsx", 0, (i, i1, list) ->
|
ExcelUtil.readBySax("d:/test/sax_date_test.xlsx", 0, (i, i1, list) ->
|
||||||
Console.log(StrUtil.join(", ", list)));
|
Console.log(StrUtil.join(", ", list)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void readBlankTest(){
|
public void readBlankTest() {
|
||||||
File file = new File("D:/test/b.xlsx");
|
File file = new File("D:/test/b.xlsx");
|
||||||
|
|
||||||
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
|
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user