解决Java导入excel大量数据出现内存溢出的问题
时间:2021-01-02 15:13:03|栏目:JAVA代码|点击: 次
问题:系统要求导入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();
}
}
}
栏 目:JAVA代码
下一篇:SpringBoot配置及使用Schedule过程解析
本文标题:解决Java导入excel大量数据出现内存溢出的问题
本文地址:http://www.codeinn.net/misctech/39571.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




