java 读取excel数据
本文共介绍两种方式,第一种是常规POI读取,第二种是大文件读取。
依赖包
<poi.version>4.1.2</poi.version><!-- excel工具 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>${poi.version}</version></dependency><!-- 读取大量excel数据时使用 --><dependency><groupId>com.monitorjbl</groupId><artifactId>xlsx-streamer</artifactId><version>2.1.0</version></dependency>
第一种方式:
常规POI读取
package com.platform.modules.admin.controller;import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;public class ReadExcel {public static void main(String[] args) throws Exception {long t1 = new Date().getTime();excel();long t2 = new Date().getTime();System.out.println((t2-t1)/1000 + "秒");}public static void excel() throws Exception {File file = new File("C:\\Users\\Lenovo\\Desktop\\数据导入模板及填写格式 (2).xlsx");if (!file.exists()){throw new Exception("文件不存在!");}InputStream in = new FileInputStream(file);// 读取整个ExcelXSSFWorkbook sheets = new XSSFWorkbook(in);// 获取第一个表单SheetXSSFSheet sheetAt = sheets.getSheetAt(0);//默认第一行为标题行,i = 0XSSFRow titleRow = sheetAt.getRow(0);// 循环获取每一行数据for (int i = 1; i < sheetAt.getPhysicalNumberOfRows(); i++) {XSSFRow row = sheetAt.getRow(i);// 读取每一格内容StringBuilder sb = new StringBuilder();for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {XSSFCell titleCell = titleRow.getCell(index);XSSFCell cell = row.getCell(index);cell.setCellType(CellType.STRING);if (cell.getStringCellValue().equals("")) {continue;}sb.append(cell);}System.out.println(i + "\t" + sb);}}
}
第二种方式:
大文件excel读取
package com.platform.modules.admin.controller;import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;import java.io.File;
import java.io.FileInputStream;
import java.util.*;public class ReadBigExcel {public static Map<String, List<String>> test(File file) throws Exception{FileInputStream in = new FileInputStream(file);Map<String,List<String>> mapData = new HashMap<String, List<String>>();Workbook wk = StreamingReader.builder().rowCacheSize(100) //缓存到内存中的行数,默认是10.bufferSize(4096) //读取资源时,缓存到内存的字节大小,默认是1024.open(in); //打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件int sheetNums = wk.getNumberOfSheets();for(int i = 0 ; i < sheetNums;i ++){List<String> sheetData = new ArrayList<String>();Sheet sheet = wk.getSheetAt(i);String sheetName = wk.getSheetName(i);//遍历所有的行int k = 0;for (Row row : sheet) {StringBuilder sb = new StringBuilder();//遍历所有的列for (Cell cell : row) {sb.append(cell.getStringCellValue());}System.out.println(k++ + "\t" + sb.toString());}}return mapData;}public static void main(String[] args) throws Exception {File file = new File("C:\\Users\\Lenovo\\Desktop\\数据导入模板及填写格式 (2).xlsx");long t1 = new Date().getTime();test(file);long t2 = new Date().getTime();System.out.println((t2-t1)/1000 + "秒");}
}
执行效率比对,横坐标为数据量,纵坐标为执行耗时秒
可以看出大文件读取POI的执行效率比常规POI好很多。
框起来的这一块是执行常规POI所耗资源,读取大文件POI对CPU和内存的占用也叫常规POI低很多,最重要的是不会引起内存溢出。
java 读取excel数据相关推荐
- Java读取Excel数据:基于Apache POI(一)
Java读取Excel数据:基于Apache POI(一) Java本身不支持直接读取微软的Excel表格数据.第三方的Apache提供了一个库POI用以支持Java读写Excel表格数据. 首先需要 ...
- JAVA读取Excel数据
JAVA读取Excel数据 下载 jxl.jar 导入jxl.jar 读取程序 写入Excel 写入txt 下载 jxl.jar 找到一个博主发的,下载好后去掉.zip后缀 jxl.jar下载 - 天 ...
- 项目实战 Java读取Excel数据
项目实战 Java读取Excel数据 前言 实现步骤 导入POI依赖 示例Excel表结构 编写读取Excel工具类 实现思路 读取Excel数据工具类实现代码 取出从excel中获取的数据,并插入到 ...
- java 读取Excel数据(POI)(一个sheet或者多个sheet)
1.添加依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml< ...
- java读取excel数据_Java读取Excel内容(转)
借助于apathe的poi.jar,由于上传文件不支持.jar所以请下载后将文件改为.jar,在应用程序中添加poi.jar包,并将需要读取的excel文件放入根目录即可 本例使用java来读取exc ...
- java读取excel数据的方法是_java怎么读取excel文件里的数据
展开全部 下面是一个简单的读取例子,如果报"java.io.IOException: Invalid header signature; read 4503608217567241, exp ...
- JAVA读取excel数据(插入oracle数据库)
本实例做的是读取execl(只能读取.xls的execl,即只能读取03版的),如果是.xlsx类型的话 手工转化一下即可,应用的jar包是apache的poi系类的jar包和ojdbc14.jar的 ...
- 在JAVA读取Excel数据的日期格式
在ExcelReader类中.getStringCellValue()方法里: public static String getStringCellValue(Cell cell) { if(cell ...
- java读取excel数据保存到数据库中_java读取excel的内容(可保存到数据库中)
//** poi jar包 // public classReadExcel { @SuppressWarnings("static-access")private staticS ...
最新文章
- 赠书:Java面试一战到底!
- 34 多线程同步之Event
- vue-property-decorator vue typescript写法
- Kitten编程猫的工程文件 bcm,能发布成Android平台的apk文件吗
- 想要更快地使用AtomicLong? 等待它。
- 如何让你在开发者工具中查看源代码有语法高亮和暗黑主题的效果
- 新手与大佬学习方式的差异
- Linux内核:网络过滤器简介与示例代码
- 算法 Tricks(五)—— 二进制逻辑运算
- centos nginx php_Centos7下NGINX+PHP的安装及配置
- java 爬虫大型教程(一)
- android opengl教程
- 安卓9安装xpose
- ffmpeg视频切片方案
- 虚拟机VMware访问Window共享文件
- signature=de4fefc549f99f0b0c76a2cec8e340bf,Diagnostics based on faulty signature
- 佛祖,你为什么不帮我
- 农村房屋房产证怎么申请
- 计算机主机板开机原理与维修,主机板损坏电脑会出现什么现象
- python适合做网页吗_python是否适合网页编程详解
热门文章
- python利用flask_mail、sendgrid发送邮件
- IP切换代理 免费资源共享
- CCS:Type region `APP_CODE_MEM' overflowed by 641240 b
- 乐高 42083 布加迪 Chiron(多图流量预警)
- Steam账号注册--流程
- 常用颜色RGB、灰度值
- 大一计算机基础excel文档,大一计算机应用基础办公自动化软件深入Excel复习用PPT课件.ppt...
- ubuntu 运行c语言文件路径,ubuntu上解压目录里的文件到指定文件夹
- python实现一元三次方程求根-二分法
- 关于iPhone5耳机一个响解决办法(部分原因)