[英]Apache POI unable to replace blank excel cells
我目前正在从 excel 电子表格中读取值并将此值分配给 String 变量。
下面是我正在使用的代码示例:
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;
}
我在这个 excel 电子表格中有多个空白单元格,我试图用短语“BlankCell”替换它们。
我过去曾成功使用过这段代码,但由于某种原因它不再起作用了。
我在这种情况下替换空白单元格的逻辑是否错误?
==
的字符串比较不是一个好习惯,有一个equals
方法。
String userName;
if ("".equals(cell.getStringCellValue())) {
userName = "BlankCell";
} else {
userName = cell.getStringCellValue();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.