开源EasyExcel统计数据总数
时间: 2024-01-17 09:18:34 浏览: 155
easyExcel千万级别数据下载
5星 · 资源好评率100%
以下是使用开源EasyExcel统计数据总数的示例代码:
```java
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.util.StringUtils;
public class ExcelUtils {
public static int countTotalRows(String filePath) {
int totalRows = 0;
ReadWorkbook readWorkbook = EasyExcel.read(filePath).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
ReadListener<Object> readListener = new ReadListener<Object>() {
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
// 异常处理
}
@Override
public void invoke(Object data, AnalysisContext context) {
// 每读取一行数据,totalRows加1
totalRows++;
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 读取完成后的操作
}
};
EasyExcel.read(readWorkbook, readSheet).registerReadListener(readListener).doRead();
return totalRows;
}
}
```
你可以调用`countTotalRows`方法来统计Excel文件中的总行数。请确保已经导入了EasyExcel的依赖库。
阅读全文