Excel字符串比较失败

blpfk2vs  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(130)

检查=D2,得到Aransas。当我尝试用=D2="Aransas"分配另一个单元格时,它会输出False。当我将C5赋值为=D2时,在C6中键入Aransas,然后尝试=C5=C6,它返回True。但是,C5="Aransas"仍然给我False
这到底是怎么回事?是单元格的格式问题还是我输入字符串的方式问题?我已经检查过这里了:excel string comparison is failing when it should not,但我看不出这是一个类型问题,因为它实际上只是比较两个字符串。

b4qexyjb

b4qexyjb1#

解决:由于某种原因,似乎非常明显的文本(例如:“Aransas”)并没有被这样对待。使用Excel,如果在所需文本前面放置撇号',则通常自动格式化的数字或日期等内容可以避免转换。我最终做的是将所有相关单元格更改为Text格式,然后 voilà,相等比较返回了它应该返回的结果。

bq3bfh9z

bq3bfh9z2#

package com.demo.mavenproject.MavenDemo;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelCompare {

    private static final Log LOGGER = LogFactory.getLog(ExcelCompare.class);

    public static void main(String[] args) {
        String file1Path = "C:\\Users\\kovsitar\\Documents\\TestExcel\\Excel1.xlsx";
        String file2Path = "C:\\Users\\kovsitar\\Documents\\TestExcel\\Excel2.xlsx";

        try (FileInputStream fis1 = new FileInputStream(file1Path);
             FileInputStream fis2 = new FileInputStream(file2Path);
             Workbook workbook1 = new XSSFWorkbook(fis1);
             Workbook workbook2 = new XSSFWorkbook(fis2)) {

            compareExcelFiles(workbook1, workbook2);

        } catch (IOException e) {
            LOGGER.error("Error reading Excel files: " + e.getMessage());
        }
    }

    private static void compareExcelFiles(Workbook workbook1, Workbook workbook2) {
        // Assuming both workbooks have the same number of sheets with the same names
        int numSheets = workbook1.getNumberOfSheets();

        for (int i = 0; i < numSheets; i++) {
            Sheet sheet1 = workbook1.getSheetAt(i);
            Sheet sheet2 = workbook2.getSheetAt(i);

            // Assuming both sheets have the same number of rows and columns
            int numRows = sheet1.getLastRowNum() + 1;

            for (int rowIdx = 0; rowIdx < numRows; rowIdx++) {
                Row row1 = sheet1.getRow(rowIdx);
                Row row2 = sheet2.getRow(rowIdx);

                int numCols = row1.getLastCellNum();

                for (int colIdx = 0; colIdx < numCols; colIdx++) {
                    Cell cell1 = row1.getCell(colIdx);
                    Cell cell2 = row2.getCell(colIdx);

                    if (!compareCells(cell1, cell2)) {
                        LOGGER.info("Mismatch found in Sheet: " + sheet1.getSheetName()
                                + ", Row: " + (rowIdx + 1) + ", Column: " + (colIdx + 1));
                    }
                }
            }
        }
    }

    private static boolean compareCells(Cell cell1, Cell cell2) {
        // Handle different cell types and values here
        if (cell1 == null && cell2 == null) {
            return true; // Both cells are empty, considered equal
        }

        if (cell1 == null || cell2 == null) {
            return false; // One cell is empty, considered not equal
        }

        if (cell1.getCellType() != cell2.getCellType()) {
            return false; // Different cell types are considered not equal
        }

        switch (cell1.getCellType()) {
            case NUMERIC:
                return cell1.getNumericCellValue() == cell2.getNumericCellValue();
            case STRING:
                return cell1.getStringCellValue().equals(cell2.getStringCellValue());
            case BOOLEAN:
                return cell1.getBooleanCellValue() == cell2.getBooleanCellValue();
            // Handle other cell types as needed
            default:
                return false;
        }
    }
}

字符串

相关问题