2. 导入

Posted yifansj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}

 

以上是关于2. 导入的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 数据库 - 使用UEStudio修改dmp文件版本号,解决imp命令恢复的数据库与dmp本地文件版本号不匹配导致的导入失败问题,“ORACLE error 12547”问题处理(代码片段

在下面的代码片段中的剩余 ='passthrough' 处的代码中出现语法错误

VS Code中自定义Emmet代码片段

MuleSoft 使用 RAML 片段将 RAML 规范导入 Anypoint Studio

有条件地导入 python 类的片段

VSCode自定义代码片段2——.vue文件的模板