@Override
@Transactional
public boolean input(MultipartFile multipartFile, HttpServletResponse response) {
if (null == multipartFile || multipartFile.isEmpty()) {
throw new CrmebException("上传的文件对象不存在...");
}
Workbook workbook = null;
String filename = multipartFile.getOriginalFilename();
try {
InputStream inp = multipartFile.getInputStream();
if (filename.endsWith("xls")){
//2003
workbook = new HSSFWorkbook(inp);
}else if(filename.endsWith("xlsx")){
//2007
workbook = new XSSFWorkbook(inp);
}
} catch (IOException e ) {
throw new CrmebException("获取文件信息失败");
}
Sheet sheet1 = workbook.getSheetAt(0);
ProductVirtual productVirtual = new ProductVirtual();
for (int i = 1; i <= sheet1.getLastRowNum(); i++) {
Row row = sheet1.getRow(i);
productVirtual.setProductId(getRInt(row.getCell(1)));
productVirtual.setCardNo(getRString(row.getCell(2)));
productVirtual.setCardPwd(getRString(row.getCell(3)));
productVirtual.setOrderId(getRString(row.getCell(4)));
productVirtual.setUid(getRInt(row.getCell(5)));
productVirtual.setCardType(getRInt(row.getCell(6)));
productVirtual.setCreateTime(new Date());
}
productVirtualDao.insert(productVirtual);
return true;
}
private String getRString(Cell cell) {
if (cell == null) {
return "";
} else {
if (cell.getCellTypeEnum().toString().equals("STRING")) {
return cell.getStringCellValue();
} else {
return String.valueOf(cell.getNumericCellValue());
}
}
}
private int getRInt(Cell cell) {
if (cell == null) {
return -1;
} else {
if (cell.getCellTypeEnum().toString().equals("STRING")) {
try {
return Integer.parseInt(cell.getStringCellValue());
} catch (Exception e) {
return -2;
}
} else {
return (int) cell.getNumericCellValue();
}
}
}
poi导入导出excel实现层
于 2022-04-21 04:53:04 首次发布