java 遠程文件url 轉爲輸入流

URL url = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設置超時間爲3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403錯誤
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到輸入流
InputStream inputStream = conn.getInputStream();

 

public static AjaxModel parseExcelForInfo(InputStream inputStream, String fileName, int taskId) {
    try {
        //創建workbook對象
        Workbook workbook = null;
        if (fileName.contains(".xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else if (fileName.contains(".xls")) {
            workbook = new HSSFWorkbook(inputStream);
        } else {
            return AjaxModel.failed(-1, "文件類型不正確");
        }
        //獲取第一個sheet表
        Sheet sheetAt = workbook.getSheetAt(0);
        if (sheetAt != null) {
            // TODO 校驗excel頭
            Row headRow = sheetAt.getRow(0);
            for (int i = 0; i < BusinessSettlementConstants.TEMPLATE_COULMN.length; i++) {
                if (!FileUtil.getCellFormatValue(headRow.getCell(i)).trim().equals(BusinessSettlementConstants.TEMPLATE_COULMN[i])) {
                    LOGGER.info("parseExcelForInfo excel頭部信息順序不正確,getCellFormatValue(headRow.getCell(i)):{}," +
                                    "BusinessSettlementConstants.TEMPLATE_COULMN[i]:{},taskId:{}", FileUtil.getCellFormatValue(headRow.getCell(i)),
                            BusinessSettlementConstants.TEMPLATE_COULMN[i], taskId);
                    return AjaxModel.failed("excel標題頭順序不正確:" + FileUtil.getCellFormatValue(headRow.getCell(i)));
                }
            }
            int startRowNum = sheetAt.getFirstRowNum() + 1;
            int lastRowNum = sheetAt.getLastRowNum();

            LOGGER.info("解析excel開始taskId:{},從【{}】行開始,到第【{}】行結束", taskId, startRowNum, lastRowNum);
            List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
            for (int rowNum = startRowNum; rowNum <= lastRowNum; rowNum++) {
                // 每一行數據
                Row row = sheetAt.getRow(rowNum);
                Map<String, String> map = new HashMap<>();
                LOGGER.info("parseExcelForInfo row:{}", row);
                if (row != null && row.getCell(0) != null && StringUtils.isNotEmpty(row.getCell(0).getStringCellValue()) && row.getCell(2) != null && row.getCell(4) != null) {
                    LOGGER.info("parseExcelForInfo row:{},cell:{}", row, row.getCell(0));
                    // 姓名
                    map.put("userName", FileUtil.getCellFormatValue(row.getCell(0)));
                    
                    dataList.add(map);
                }
            }
            LOGGER.info("--------------解析完成 dataList:{}", dataList);
            if (dataList.size() <= 0) {
                return AjaxModel.failed(-1, "解析表格數據爲空");
            }
            AjaxModel success = AjaxModel.success();
            success.getData().put("dataList", dataList);
            return success;
        } else {
            LOGGER.info("sheet內容爲空");
            return AjaxModel.failed(-1, "表格內容爲空");
        }
    } catch (Exception e) {
        LOGGER.error("parseExcelForInfo 解析異常", e);
    }
    return AjaxModel.failed(-1, "解析表格異常");
}

 

public static String getCellFormatValue(Cell cell) {
    cell.setCellType(CellType.STRING);
    return cell.getStringCellValue();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章