excel 导入 poi

Controller 层

 @ApiOperation(value = "根据条件和project Id 导出 po management导出查询到的用户 ", notes = "")
    @PostMapping(value = "batchimport", produces = "application/json;charset=UTF-8")
    public RespBody<ResponseEntity> batchimport(MultipartFile file) {
        if (file == null) {
            return null;
        }
//        String name = file.getOriginalFilename();
        long maxByte = 1024 * 1024 * 300;
        String excelName = file.getOriginalFilename();
        if (StringUtils.isEmpty(excelName) || !(excelName.toLowerCase().endsWith(".xls") || excelName.toLowerCase().endsWith(".xlsx"))) {
            return new RespBody<>(ResponseEntity.status(HttpStatus.BAD_REQUEST).body("导入格式不正确,请重新导入"));
        }
        if (file.getSize() > maxByte && file.getSize() > 0) {
            return new RespBody<>(ResponseEntity.status(HttpStatus.BAD_REQUEST).body("导入Excel文件过大"));
        }
        try {
            financeImportService.importFana(file, excelName);
        } catch (Exception e) {
            return new RespBody<>(ResponseEntity.status(HttpStatus.BAD_REQUEST).body("系统异常"));
        }
        return new RespBody<>(ResponseEntity.ok("sucess"));

    }

sevices 层 

@Override
public void importFana(MultipartFile file, String excelName) throws IOException {

    Workbook wb = ExcleUtils.getWorkBook(file, excelName);
    // 得到第一个shell
    Sheet sheet = wb.getSheetAt(0);
    // 得到Excel的行数
    int totalRows = sheet.getPhysicalNumberOfRows();
    int totalCol = sheet.getRow(0).getPhysicalNumberOfCells();
    List<FinanceImport> lstData = new ArrayList<>();
    for (int i = 1; i < totalRows; i++) {
        Row row = null;
        if (excelName.toLowerCase().endsWith("xls")) {
            // Excel-2003
            row = (HSSFRow) sheet.getRow(i);
        } else if (excelName.toLowerCase().endsWith("xlsx")) {
            // Excel-2007
            row = (XSSFRow) sheet.getRow(i);
        }

        FinanceImport financeImport = new FinanceImport();
        //采购凭证
        financeImport.setPurcVouc(row.getCell(0).getStringCellValue());
        //项目
        financeImport.setItem(row.getCell(1).getStringCellValue());
        //WBS元素
        String wbsElement = row.getCell(2).getStringCellValue().substring(0, 11);
        //projectNo
        financeImport.setProjectNo(wbsElement);

        financeImport.setWbsElement(row.getCell(2).getStringCellValue());
        //批准标识
        financeImport.setApprMark(row.getCell(3).getStringCellValue());
        Date voucDate = row.getCell(4).getDateCellValue();
        //凭证日期
        financeImport.setVoucDate(voucDate);
        long l = getMillisOfDate(new Date(), voucDate);
        int days = new Long(l / (1000 * 60 * 60 * 24)).intValue();

        //agingDays
        financeImport.setAgingDays(days);
        //供应商/供应工厂
        financeImport.setSupplier(row.getCell(5).getStringCellValue());
        //短文本
        financeImport.setShortText(row.getCell(6).getStringCellValue());
        //科目分配类别
        financeImport.setSubjectCategory(row.getCell(7).getStringCellValue());
        row.getCell(8).setCellType(CellType.STRING);
        //采购订单数量
        financeImport.setPurcOrderNum(row.getCell(8).getStringCellValue());
        row.getCell(9).setCellType(CellType.STRING);
        //净价
        financeImport.setNetPrice(row.getCell(9).getStringCellValue());
        //采购订单数量*净价
        String poPlace = String.format("%.2f", new BigDecimal(row.getCell(8).getStringCellValue()).multiply(new BigDecimal(row.getCell(9).getStringCellValue())));
        //poPlace
        financeImport.setPoPlace(poPlace);

        row.getCell(10).setCellType(CellType.STRING);
        //价格单位
        financeImport.setPriceUnit(row.getCell(10).getStringCellValue());
        row.getCell(11).setCellType(CellType.STRING);
        //仍要交货(数量)
        financeImport.setStillDeliveryNum(row.getCell(11).getStringCellValue());

        row.getCell(12).setCellType(CellType.STRING);
        String poNotReceived = row.getCell(12).getStringCellValue();
        //Po not received  = 仍要交货(价值)
        financeImport.setPoNotReceived(poNotReceived);

        //Po received  ROUNDDOWN(PO placed - PO not received)(不超过它的最大整数)
        double poReceived = Math.floor(Double.parseDouble(poPlace) - Double.parseDouble(poNotReceived));
        financeImport.setPoReceived(String.valueOf((long) poReceived));
        //仍要交货(价值)
        financeImport.setStillDeliveryValue(row.getCell(12).getStringCellValue());

        row.getCell(13).setCellType(CellType.STRING);
        //仍要开票(数量)
        financeImport.setStillInvoiceNum(row.getCell(13).getStringCellValue());
        row.getCell(14).setCellType(CellType.STRING);
        //仍要开票(价值)
        financeImport.setStillInvoiceValue(row.getCell(14).getStringCellValue());

        //Po yet to pay  仍要开票(价值)
        String poYetToPay = row.getCell(14).getStringCellValue();
        financeImport.setPoYetToPay(poYetToPay);
        //利润中心
        financeImport.setProfitCenter(row.getCell(15).getStringCellValue());
        //资产
        row.getCell(16).setCellType(CellType.STRING);
        financeImport.setAssets(row.getCell(16).getStringCellValue());
        lstData.add(financeImport);
    }
    financeImportMapper.insertSelective(lstData);


}
发布了47 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_34233080/article/details/104825963