问题:系统要求导入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导入excel大量数据出现内存溢出的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
上一篇 :
那些年,微软葬送的产品〡清明特辑
下一篇 :
996.ICU被多家国内公司的浏览器列入了黑名单
-
微博官方回应机构大V刷单:已暂停账号商业接单
2026-05-15栏目: 教程
-
综合布线各系统清单计算
2026-05-15栏目: 教程
-
一行代码解决忘记密码问题 教你用CMD命令查看所有连接过的WIFI密码
2026-05-15栏目: 教程
-
年度最具看点黑客大赛GeekPwn2019即将开战,顶级黑
2026-05-15栏目: 教程
-
国家信息中心、中国移动、中国银联等联合发布区块链服务网络
2026-05-15栏目: 教程
