• 2. 导入


    2. 导入

    public static void main(String[] args) {
        String downloadPath = "存储路径";
        XSSFWorkbook xssfWorkbook = loadWorkBook(downloadPath);
        importOverseasLadingExcel(xssfWorkbook, orderFeeExportDto);
    }
    
    /**
     * URL转WorkBook
     */
    private XSSFWorkbook loadWorkBook(String url) throws Exception {
        InputStream inputStream = null;
        try {
            URL realUrl = new URL(url);
            if (url.startsWith("file:/")) {
                FileURLConnection httpUrl = (FileURLConnection) realUrl.openConnection();
                inputStream = httpUrl.getInputStream();
            } else {
                HttpURLConnection httpUrl = (HttpURLConnection) realUrl.openConnection();
                inputStream = httpUrl.getInputStream();
            }
    
            return new XSSFWorkbook(inputStream);
        } catch (Exception e) {
            throw e;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    /**
     * 导入
     */
    public void importOverseasLadingExcel(XSSFWorkbook wb, FinanceOrderFeeExportDto financeOrderFeeExportDto) {
        Sheet sheet = wb.getSheetAt(0);
        // 标题,也是要导入的字段
        Row rowHead = sheet.getRow(0);
        //行数
        int rowCount = sheet.getPhysicalNumberOfRows();
        //列数
        int columnCount = rowHead.getLastCellNum();
        // 头部
        List<String> headNames = new ArrayList<>();
        for (int i = 0; i < columnCount; i++) {
            String headName = rowHead.getCell(i).getStringCellValue();
            headNames.add(headName);
        }
    
        //将excel行数据转为对象
        List<OrderWorkDto> orderWorkDtoList = new ArrayList<OrderWorkDto>();
        for (int j = 1; j < rowCount; j++) {
            Row row = sheet.getRow(j);
            if (isBlankRow(row)) {
                continue;
            }
    
            OrderWorkDto orderWorkDto = new OrderWorkDto();
            for (int k = 0; k < columnCount - 1; k++) {
                String columeValue = transferCellType(row.getCell(k));
                if (k == 0) {
                    orderWorkDto.setDeliveryNumber(columeValue);
                } else if (k == 1) {
                    orderWorkDto.setPrice(columeValue);
                } else if (k == 2) {
                    orderWorkDto.setCuy(columeValue);
                } else if (k == 3) {
                    orderWorkDto.setAcction(columeValue);
                }
                orderWorkDto.setOperateId(financeOrderFeeExportDto.getOperateId());
            }
            orderWorkDtoList.add(orderWorkDto);
            //第10个保存一次
            if (orderWorkDtoList.size() % 10 == 0) {
                saveOrderWorkList(orderWorkDtoList);
                orderWorkDtoList = new ArrayList<OrderWorkDto>();
            }
        }
        //保存最后对象
        if (orderWorkDtoList.size() > 0) {
            saveOrderWorkList(orderWorkDtoList);
        }
    }
    
    public void saveOrderWorkList(List<OrderWorkDto> orderWorkDtoList) {
        financeOrderDao.saveOrderWorkList(orderWorkDtoList);
    }
    
    /**
     * 判断行是否为空
     */
    private static boolean isBlankRow(Row row) {
        boolean blank = true;
    
        Iterator<Cell> cellItr = row.iterator();
        while (cellItr.hasNext()) {
            Cell c = cellItr.next();
            if (c.getCellType() != HSSFCell.CELL_TYPE_BLANK) {
                blank = false;
                break;
            }
        }
        return blank;
    }
    
    /**
     * 单元格转字符串
     */
    private static String transferCellType(Cell cell) {
        if (cell == null) {
            return null;
        }
        cell.setCellType(Cell.CELL_TYPE_STRING);
        return cell.getStringCellValue();
    }
  • 相关阅读:
    Python零基础入门的基础案例
    今天不抠图,Python实现一键换底片!想换什么换什么(附源码)
    python基础:如何使用 pip 安装第三方库
    Python教程:matplotlib 绘制双Y轴曲线图
    福利来啦,送给大家一个小游戏的源码,不要错过哟
    招聘信息太多,哪家职位才是适合你的?Python采集招聘信息
    我的python菜鸟之路1
    我的SAS菜鸟之路3
    我的SAS菜鸟之路2
    猪鹿蝶1
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/13305161.html
Copyright © 2020-2023  润新知