java 上传压缩文件 获取压缩文件中的excel文件并读取相关数据

直接上代码

 public static void main(String[] args) throws IOException {
        List<List<String>>list= new ArrayList<>();
        //System.out.println(getZipFiles1().length);
        try (ZipInputStream in = new ZipInputStream(
                new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.zip")
                , Charset.forName("gbk"))) {
            ZipEntry zip=null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            //获取压缩文件中的每个子文件
            while((zip=in.getNextEntry())!=null) {
                if (zip.isDirectory()) {
                    //如果是目录,不处理
                    continue;
                }
                String zipFileName = zip.getName();
                System.out.println(zip.getName());
                if (zipFileName != null && zipFileName.indexOf(".") != -1 && (zipFileName.toUpperCase().endsWith("XLSX") ||zipFileName.toUpperCase().endsWith("XLS") )) {
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = in.read(buffer)) > -1) {
                        baos.write(buffer, 0, len);
                    }
                    baos.flush();
                    InputStream stream1 = new ByteArrayInputStream(baos.toByteArray()); //excel 流
                    //读取EXCEL中的值
                    List<List<String>> list1 =readExcelInfo(stream1, zipFileName) ;
                    if(list1 != null){
                        list.addAll(list1);
                    }
                    baos.reset();
                    continue;
                }

            }
            System.out.println(list);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    public static List<List<String>> readExcelInfo(InputStream is, String fileName) throws Exception {
        Workbook workbook = getWorkBook(is, fileName);
        int sheetNum = workbook.getNumberOfSheets();
        List<List<String>> dataList = new ArrayList<List<String>>();
        for (int index = 0; index < sheetNum; index++) {
            Sheet sheet = workbook.getSheetAt(index);
            if (sheet == null) {
                continue;
            }
            Row row1 = sheet.getRow(0);
            //  sheet.getRow(0).getCell(1);

            short firstNum = row1.getLastCellNum();
            for (int rowIndex = 3; rowIndex <= sheet.getLastRowNum(); rowIndex++) {   //默认从第二行开始读取
                Row row = sheet.getRow(rowIndex);
                if (row == null) {
                    continue;
                }
                List<String> cellList = new ArrayList<String>();
                for (int cellIndex = 0; cellIndex < firstNum; cellIndex++) {
                    Cell cell = row.getCell(cellIndex);
                    if (cell != null) {
                       String v= getCellValue(cell);
                       // a== - 时 跳过
                        if(v != null &&!"-" .equals(v.trim())){
                            cellList.add(v);
                        }
                    }else{
                        cellList.add("");
                    }
                }
                //每一条数据的orgcode

                cellList.add(fileName.substring(1+fileName.lastIndexOf("/"),fileName.lastIndexOf(".")));

                if(!cellList.isEmpty()){
                    dataList.add(cellList);
                }
            }
        }
        is.close();
       // System.out.println(dataList);
        return dataList;
    }

    public static Workbook getWorkBook(InputStream is, String fileName) throws Exception {

        Workbook workbook = null;
        if (fileName.toUpperCase().endsWith("XLS")) {
            workbook = new HSSFWorkbook(is);
        } else if (fileName.toUpperCase().endsWith("XLSX")) {
            workbook = new XSSFWorkbook(is);
        }

        return workbook;
    }


    public static String getCellValue(Cell cell) {
        CellType cellType = cell.getCellTypeEnum();
        String cellValue = "";
        if (cell == null || cell.toString().trim().equals("")) {
            return null;
        }

        if (cellType == CellType.STRING) {
            cellValue = cell.getStringCellValue().trim();
            return cellValue = StringUtil.isEmpty(cellValue) ? "" : cellValue;
        }
        if (cellType == CellType.NUMERIC) {
            if (HSSFDateUtil.isCellDateFormatted(cell)) {  //判断日期类型
                cellValue = String.valueOf(cell.getDateCellValue().getTime());
            } else {  //否
                cellValue = new DecimalFormat("#.######").format(cell.getNumericCellValue());
            }
            return cellValue;
        }
        if (cellType == CellType.BOOLEAN) {
            cellValue = String.valueOf(cell.getBooleanCellValue());
            return cellValue;
        }
        return null;

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值