简体   繁体   English

Apache POI 无法替换空白 Excel 单元格

[英]Apache POI unable to replace blank excel cells

I am currently reading values from an excel spreadsheet and assigning this value to a String variable.我目前正在从 excel 电子表格中读取值并将此值分配给 String 变量。
Below is a sample of the code I am using:下面是我正在使用的代码示例:

        Workbook workbook = new XSSFWorkbook(iStream);
        Sheet firstSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = firstSheet.iterator();
        // Skips the header row
        iterator.next();

        // Iterate through the first sheet and get the cell values
        while (iterator.hasNext()) {

            Row nextRow = iterator.next();
            Iterator<Cell> cellIterator = nextRow.cellIterator();

            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();
                int columnIndex = cell.getColumnIndex();

                switch (columnIndex) {
                case 0:
                    String userName;
                    if (cell.getStringCellValue() == "") {
                        userName = "BlankCell";
                    } else {
                        userName = cell.getStringCellValue();
                    }                       
                    break;
                }

I have multiple blank cells within this excel spreadsheet which I am trying to replace with the phrase "BlankCell".我在这个 excel 电子表格中有多个空白单元格,我试图用短语“BlankCell”替换它们。
I have used this piece of code successfully in the past but for some reason it does not work anymore.我过去曾成功使用过这段代码,但由于某种原因它不再起作用了。
Is my logic in this instance wrong for replacing blank cells?我在这种情况下替换空白单元格的逻辑是否错误?

The string comparison by == isn't good practice, there's equals method for this. ==的字符串比较不是一个好习惯,有一个equals方法。

String userName;
if ("".equals(cell.getStringCellValue())) {
    userName = "BlankCell";
} else {
    userName = cell.getStringCellValue();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM