jpa解析excel,批量存入mysql

@Override
public String readExcel(String filePath, Long factorId, Boolean isXSSF) throws IOException {
List ls=new ArrayList<>();
int batchSize=3;
Workbook wb =null;
if(isXSSF){
wb=new XSSFWorkbook( new FileInputStream(new File(filePath)));
}else{
wb=new HSSFWorkbook( new FileInputStream(new File(filePath)));
}
Sheet sheetAt = wb.getSheetAt(0);
Row firstrow = sheetAt.getRow(0);
int minColIx = firstrow.getFirstCellNum();
int maxColIx = firstrow.getLastCellNum();
List stockids=new ArrayList<>();
for (int colIx = minColIx; colIx < maxColIx; colIx++){
Cell cell = firstrow.getCell(colIx);
//第一个是日期,但是也加入进来了,是为了保持下标索引的一致
String stockid=cell.getStringCellValue().trim();
stockids.add(stockid);
}
for(int i = 1; i < sheetAt.getLastRowNum()+1 ; i ++){
Row row = sheetAt.getRow(i);
Date date=null;
for (int colIx = minColIx; colIx < maxColIx; colIx++){
Cell cell = row.getCell(colIx);
if(colIx==minColIx){
date = cell.getDateCellValue();
}else {
String stockId=stockids.get(colIx);
BigDecimal factorValue=new BigDecimal(cell.getNumericCellValue());
InvestFactorDetailEntity entity=new InvestFactorDetailEntity();
entity.setDate(date);
entity.setCreateTime(new Date());
entity.setFactorValue(factorValue);
entity.setStockId(stockId);
entity.setFactorId(factorId);
ls.add(entity);
if(ls.size()%batchSize==0){
investmentFactorDetailDao.save(ls);
ls.clear();
}
}
}
}
investmentFactorDetailDao.save(ls);
ls.clear();
return null;
}



有合并单元格的情况
@Override
public String readExcel(String filePath,Boolean isXSSF,Long fileId) throws IOException {
List ls=new ArrayList<>();
Date date=new Date();
int batchSize=3;
String industryName=null;
BigDecimal industryWeight=null;
Workbook wb =null;
if(isXSSF){
wb=new XSSFWorkbook( new FileInputStream(new File(filePath)));
}else{
wb=new HSSFWorkbook( new FileInputStream(new File(filePath)));
}
Sheet sheetAt = wb.getSheetAt(0);
int sheetMergeCount = sheetAt.getNumMergedRegions();
for(int i = 0 ; i < sheetMergeCount ; i++){
CellRangeAddress ca = sheetAt.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();
Row fRow = sheetAt.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);
if(firstColumn==0){
industryName=fCell.getStringCellValue();
}
if(firstColumn==3){
industryWeight=new BigDecimal(fCell.getNumericCellValue());
}
}
IndustryInfoEntity entity1=industryInfoDao.findByIndustryName(industryName);
if(entity1!=null){
industryInfoDao.delete(entity1.getId());
industryDetailDao.deleteAllByIndustryId(entity1.getId());
}
entity1=new IndustryInfoEntity();
entity1.setIndustryName(industryName);
entity1.setIndustryWeight(industryWeight);
entity1.setCreateTime(date);
entity1.setClientId(1L);
entity1.setFileId(1L);
Long industryId=industryInfoDao.save(entity1).getId();
System.out.println("industryName="+industryName+",industryWeight="+industryWeight);

for(int i = 1; i < sheetAt.getLastRowNum()+1 ; i ++){
Row row = sheetAt.getRow(i);
Cell stockId = row.getCell(1);
Cell stockWeight = row.getCell(2);
IndustryDetailEntity entity=new IndustryDetailEntity();
entity.setCreateTime(date);
entity.setFileId(fileId);
entity.setStockId(stockId.getStringCellValue());
entity.setStockWeight(new BigDecimal(stockWeight.getNumericCellValue()));
entity.setIndustryId(industryId);
ls.add(entity);
if(ls.size()%batchSize==0){
industryDetailDao.save(ls);
ls.clear();
}

}
System.out.println(ls);
List aa =industryDetailDao.save(ls);
System.out.println(aa);
ls.clear();
return null;
}



你可能感兴趣的:(jpa解析excel,批量存入mysql)