解决Java导入excel大量数据出现内存溢出的问题
作者:dandanfeng1992 时间:2023-05-30 08:41:20
问题:系统要求导入40万条excel数据,采用poi方式,服务器出现内存溢出情况。
解决方法:由于HSSFWorkbook workbook = new HSSFWorkbook(path)一次性将excel load到内存中导致内存不够。
故采用读取csv格式。由于csv的数据以x1,x2,x3形成,类似读取txt文档。
private BufferedReader bReader;
/**
* 执行文件入口
*/
public void execute() {
try {
if(!path.endsWith(".csv")){
logger.info("-----该文件不是以CSV文件,请上传正确的文件格式------");
return ;
}
Long startTime = System.currentTimeMillis();
logger.info("------开始执行定时任务,时间=" + startTime);
readCSV(path);
Long endTime = System.currentTimeMillis();
logger.info("------结束定时任务,时间=" + endTime + "---耗时="
+ (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取csv并处理数据
* @param path
* @throws Exception
*/
private void readCSV(String path) throws Exception {
File file = new File(path);
try {
bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
String line = "";
//忽略第一行标题
for (int i = 0; i < 1; i++) {
line = bReader.readLine();
}
while((line = bReader.readLine()) != null){
if (line.trim() != "") {
//分割开来的即是对应的每个单元格,注意空的情况
String[] result = line.split(",");
}
}
}
} finally {
if (bReader != null) {
bReader.close();
}
}
}
来源:https://blog.csdn.net/dandanfeng1992/article/details/75661096
标签:excel,内存,溢出,java
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现定时任务的方法详解
2022-12-30 04:14:00
java字符串相似度算法
2023-11-26 12:33:25
Java实现简易学生管理系统
2022-10-16 19:26:49
![](https://img.aspxhome.com/file/2023/1/69301_0s.jpg)
Java实现分页的前台页面和后台代码
2021-07-22 17:10:04
![](https://img.aspxhome.com/file/2023/1/61381_0s.jpg)
Android实现花瓣飘落效果的步骤
2021-11-23 17:16:44
![](https://img.aspxhome.com/file/2023/4/100614_0s.gif)
Android用户注册界面简单设计
2023-07-13 02:59:50
![](https://img.aspxhome.com/file/2023/8/139028_0s.jpg)
在Unity中捕捉Android的常用按钮返回事件
2023-01-31 00:11:36
SpringBoot数据访问自定义使用Druid数据源的方法
2023-01-10 16:29:38
![](https://img.aspxhome.com/file/2023/1/91081_0s.jpg)
详解Spring IOC 容器启动流程分析
2023-10-20 00:50:56
![](https://img.aspxhome.com/file/2023/9/82449_0s.jpg)
深入探究Java线程的创建与构造方法
2023-05-29 22:02:45
![](https://img.aspxhome.com/file/2023/5/72315_0s.png)
C++ 让函数返回数组的方法
2022-12-04 06:49:33
springBoot之如何获取接口请求数据和返回数据实现日志
2023-11-23 10:43:58
java实现图片分割指定大小
2021-07-02 16:56:34
解析springboot整合谷歌开源缓存框架Guava Cache原理
2023-11-07 13:24:23
![](https://img.aspxhome.com/file/2023/0/62030_0s.png)
Android开发时尽管已root但是ddms还是没有data路径怎么办
2023-01-28 03:19:36
Java map 优雅的元素遍历方式说明
2022-11-12 16:57:28
java版微信公众平台消息接口应用示例
2022-10-04 10:22:58
Java函数式编程(六):Optional
2021-07-04 21:44:05
java查找图中两点之间所有路径
2022-10-04 03:08:11
出现SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.的解决方法
2021-09-01 12:02:09
![](https://img.aspxhome.com/file/2023/1/88221_0s.png)