标签:code 标题 eal vat column 字符 ring pre input
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(); }
标签:code 标题 eal vat column 字符 ring pre input
原文地址:https://www.cnblogs.com/yifanSJ/p/13305161.html