网站技术介绍,产品艺术设计专业,企业网站的建立不能缺少哪些细节,怎么查在建工地的信息一、需求描述
Excel表格里面大约有20个sheet页#xff0c;每个sheet页65535条数据#xff0c;需要读取全部数据#xff0c;并导入至数据库。
找了好多种方式#xff0c;EasyExcel比较符合#xff0c;下面看代码。
二、实现方式 采用EasyExcel框架的doReadAll()方法 1、…一、需求描述
Excel表格里面大约有20个sheet页每个sheet页65535条数据需要读取全部数据并导入至数据库。
找了好多种方式EasyExcel比较符合下面看代码。
二、实现方式 采用EasyExcel框架的doReadAll()方法 1、DemoController /*** 导入文件分析*/public void importAll(){String fileName D:\\aa.xlsx;//读取所有Sheet的数据.每次读完一个Sheet就会调用这个方法EasyExcel.read(fileName, new EasyExceGeneralDatalListener(empService)).doReadAll();} 2、EmpService
import java.util.List;
import java.util.Map;public interface EmpService
{public void importData(ListMapInteger, String dataList);
}3、EmpServiceImpl
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.springframework.stereotype.Service;import com.ruoyi.imp.service.EmpService;
import com.ruoyi.jdbc.util.JDBCDruidUtils;Service
public class EmpServiceImpl implements EmpService
{/** 测试用Excel导入超过10w条数据,经过测试发现,使用Mybatis的批量插入速度非常慢,所以这里可以使用 数据分批JDBC分批插入事务来继续插入速度会非常快*/Overridepublic void importData(ListMapInteger, String dataList) {//结果集中数据为0时,结束方法.进行下一次调用if (dataList.size() 0) {return;}//JDBC分批插入事务操作完成对20w数据的插入Connection conn null;PreparedStatement ps null;try {long startTime System.currentTimeMillis();System.out.println(此次共dataList.size() 条数据导入开始时间: startTime ms);// JDBCDruidUtils.getConnection() 此部分为JDBC连接百度一下就有conn JDBCDruidUtils.getConnection();//控制事务:默认不提交conn.setAutoCommit(false);String sql insert into tp_import_all (wy,kh,ch,dk,on,wlc,ncv,mc,jmc) values;sql (?,?,?,?,?,?,?,?,?);ps conn.prepareStatement(sql);//循环结果集:这里循环不支持烂布袋表达式for (int i 0; i dataList.size(); i) {MapInteger, String item dataList.get(i);ps.setString(1, item.get(0));ps.setString(2, item.get(1));ps.setString(3, item.get(2));ps.setString(4, item.get(3));ps.setString(5, item.get(4));ps.setString(6, item.get(5));ps.setString(7, item.get(6));ps.setString(8, item.get(7));ps.setString(9, item.get(8));//将一组参数添加到此 PreparedStatement 对象的批处理命令中。ps.addBatch();}System.out.println(importData方法executeBatch()...开始执行);//执行批处理ps.executeBatch();System.out.println(importData方法executeBatch()...执行结束);System.out.println(importData方法commit()...开始提交);//手动提交事务conn.commit();System.out.println(importData方法commit()...提交结束);long endTime System.currentTimeMillis();System.out.println(dataList.size() 条,导入结束时间: endTime ms);System.out.println(导入用时: (endTime - startTime) ms);} catch (Exception e) {e.printStackTrace();System.out.println(catch Exception e);} finally {//关连接JDBCDruidUtils.close(conn, ps);System.out.println(JDBCDruidUtils.close 关连接);}}}4、EasyExceGeneralDatalListener
import java.util.ArrayList;
import java.util.List;
import java.util.Map;import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.imp.service.EmpService;// 事件监听
public class EasyExceGeneralDatalListener extends AnalysisEventListenerMapInteger, String {/*** 处理业务逻辑的Service,也可以是Mapper*/private EmpService empService;/*** 用于存储读取的数据*/private ListMapInteger, String dataList new ArrayListMapInteger, String();public EasyExceGeneralDatalListener() {}public EasyExceGeneralDatalListener(EmpService empService) {this.empService empService;}Overridepublic void invoke(MapInteger, String data, AnalysisContext context) {//数据add进入集合dataList.add(data);//size是否为100000条:这里其实就是分批.当数据等于20w的时候执行一次插入if (dataList.size() ExcelConstants.GENERAL_ONCE_SAVE_TO_DB_ROWS) {//ExcelConstants.GENERAL_ONCE_SAVE_TO_DB_ROWS Integer类型控制条数//存入数据库:数据小于1w条使用Mybatis的批量插入即可;saveData();//清理集合便于GC回收dataList.clear();}}/*** 保存数据到DB** param* MethodName: saveData* return: void*/private void saveData() {System.out.println(saveData方法importData...开始);// empService.importData(dataList);System.out.println(saveData方法importData...结束);// 回收dataList.clear();}/*** Excel中所有数据解析完毕会调用此方法** param: context* MethodName: doAfterAllAnalysed* return: void*/Overridepublic void doAfterAllAnalysed(AnalysisContext context) {System.out.println(doAfterAllAnalysed方法saveData...开始);// 调用方法saveData();System.out.println(doAfterAllAnalysed方法saveData...结束);// 回收dataList.clear();}
}三、其他问题 1、所用版本 dependency groupIdcom.alibaba/groupId artifactIdeasyexcel/artifactId version2.2.10/version /dependency 2、可能会碰到null的报错可以检查下poi版本是否冲突这边用的4.1的版本
如果出现poi-ooxml是4.x版本poi是3.x 可能会出错
文章转载自: http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.znqmh.cn.gov.cn.znqmh.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.mgmyt.cn.gov.cn.mgmyt.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.xnzmc.cn.gov.cn.xnzmc.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.bfmq.cn.gov.cn.bfmq.cn http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn http://www.morning.fcrw.cn.gov.cn.fcrw.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.demoux.com.gov.cn.demoux.com http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.prprz.cn.gov.cn.prprz.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.mzzqs.cn.gov.cn.mzzqs.cn