How to read xlsx and xls type values in Java?

The difference between xlsx and xls

Differences: 1. xls is the default format used by excel versions before 2007; xlsx is the default format used by excel versions after 2007, including the 2007 version. 2. The XLSX format takes up less space than XLS.

How does Java handle xlsx and xls types and read them

For the two types, different types of files need to be read in two ways. The pseudo code is directly posted below.

public ResponseResult<Void> parseFile(MultipartFile files) {
    
    
        Workbook wb = null;
        InputStream is = null;
        try {
    
    
            String originalFilename = files.getOriginalFilename();
            String prefix = null;
            if (originalFilename != null) {
    
    
                prefix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            }
            is = files.getInputStream();
            if (("xls").equals(prefix)) {
    
    
                wb = new HSSFWorkbook(is);
            } else if (("xlsx").equals(prefix)) {
    
    
                wb = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
    
    
            log.error(e.getMessage());
        }
        
        if (wb != null) {
    
    
            int sheetNum = wb.getNumberOfSheets();
            for (int i = 0; i < sheetNum; i++) {
    
    
                //获取指定sheet页
                sheet = wb.getSheetAt(i);
                //获取sheet名称
                String sheetName = sheet.getSheetName();
                //获取最大行数
                int rowNum = sheet.getPhysicalNumberOfRows();
                //验证是否空文件
                if (rowNum == 0) {
    
    
                    continue;
                }
                //获取第一行
                row = sheet.getRow(0);
                if (row == null) {
    
    
                    continue;
                }
                //获取最大列数
                int column = row.getPhysicalNumberOfCells();
                for (int j = 0; j < column; j++) {
    
    
                    if (row.getCell(j) != null) {
    
    
                        //读取单个单元格的内容并进行格式化转化
                        String columnName = this.getCellValue(row.getCell(j));
                    }
                }
            }
        }
        return null;
    }

Because the format of the cells is not uniform, the content in the cells needs to be formatted.

private String getCellValue(Cell cell){
    
    
        String cellValue = "";
        if(cell == null) {
    
    
            return cellValue;
        }
        switch (cell.getCellType().getCode()) {
    
     // 判断excel单元格内容的格式,并对其进行转换,以便插入数据库
            case 0:
                cellValue = String.valueOf((long) cell.getNumericCellValue());
                break;
            case 2:
                cellValue = String.valueOf(cell.getDateCellValue());
                break;
            case 3:
                cellValue = "";
                break;
            case 4:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case 5:
                cellValue = String.valueOf(cell.getErrorCellValue());
                break;
            case 1:
            default:
                cellValue = cell.getStringCellValue();
                break;
        }
        return cellValue;
    }

Supongo que te gusta

Origin blog.csdn.net/icemeco/article/details/125540831
Recomendado
Clasificación