背景
如果你经常使用excel,想将excel的数据快速插入数据库,或者读取excel的内容,做一些逻辑处理。那么本文可以帮助你。
采用poi jar
直接将文件转为文件流,后续获取流来操作数据。
java
public Integer doUploadMetaQ(InputStream inputStream) {
List<RiskAppBasicDataDO> riskAppBasicDataDOList = new ArrayList<>();
try {
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(RiskAppBasicDataConstant.ZERO);
for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
RiskAppBasicDataDO riskAppBasicDataDO = new RiskAppBasicDataDO(StringUtils.EMPTY);
Row row = sheet.getRow(i);
riskAppBasicDataDO.setAppName(row.getCell(0)==null?"":row.getCell(0).toString());
riskAppBasicDataDO.setServiceType(ServiceTypeEnum.METAQ.getValue());
riskAppBasicDataDO.setDetailType(row.getCell(2)==null?"":row.getCell(2).toString());
riskAppBasicDataDO.setServiceName(row.getCell(1)==null?"":row.getCell(1).toString());
riskAppBasicDataDO.setNamespace(row.getCell(3)==null?"":row.getCell(3).toString());
log.info("appName is {} ",riskAppBasicDataDO.getAppName());
riskAppBasicDataDOList.add(riskAppBasicDataDO);
}
} catch (IOException e) {
e.printStackTrace();
}
if (CollectionUtils.isEmpty(riskAppBasicDataDOList)) return RiskAppBasicDataConstant.ZERO;
return riskAppBasicDataDao.batchInsert(riskAppBasicDataDOList);
}