java - 如何使用 XSSF 检查 Excel 单元格的类型
问题描述
我正在尝试检查单元格是数字还是空。
所以我写了这段代码
...
int columnIndex = 1
while(columnIndex < numberOfColumns && matrixSheet.getRow(0).getCell(columnIndex).stringCellValue != '') {
if (matrixSheet.getRow(rowIndex).getCell(columnIndex).getCellType() == Cell.CELL_TYPE_NUMERIC)
orderOfActivities.add(matrixSheet.getRow(rowIndex).getCell(columnIndex) - 1, matrixSheet.getRow(0).getCell(columnIndex))
columnIndex++
}
...
但是,我收到了这个错误:
原因:groovy.lang.MissingMethodException:没有方法签名:org.apache.poi.xssf.usermodel.XSSFCell.minus() 适用于参数类型:(java.lang.Integer) 值:[1] 可能的解决方案:find (), is(java.lang.Object), find(groovy.lang.Closure), any(), print(java.lang.Object), with(groovy.lang.Closure)
在以下行:if (matrixSheet.getRow(rowIndex).getCell(columnIndex).getCellType() == Cell.CELL_TYPE_NUMERIC)
有人有什么想法吗?非常感谢!
更新 1
A | B | C | D | E |
1 | Text1 Text2 Text3 ...
2 | Val1 2 1
3 | Val2 1 2
4 | Val3 1
5 | ...
解决方案
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelFileTest {
private static final String FILE_NAME = "D:\\Book2.xlsx";
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
Object[][] datatypes = {
{"Datatype", "Type", "Size(in bytes)"},
{"int", "Primitive", 2},
{"float", "Primitive", 4},
{"double", "Primitive", 8},
{"char", "Primitive", 1},
{"String", "Non-Primitive", "No fixed size"}
};
int rowNum = 0;
System.out.println("Creating excel");
for (Object[] datatype : datatypes) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object field : datatype) {
Cell cell = row.createCell(colNum++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) { //here check integer Type
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}
推荐阅读
- swiftui - UIKit 中的预览问题(添加多个环境来处理 google-service.plist 后)
- wpf - 模板生成的对象与其父对象的属性之间的绑定
- javascript - currentWindow: true 没有使用 Chrome 扩展调用正确的选项卡
- base64 - 如何使用 LND REST API 对 r_hash 进行编码以检查付款发票
- python - 如何从公司资料框中的页面获取所有“href”
- sql - SQL Server 存储过程中 While 循环的替代方案
- javascript - 如何在反应js中获取文本值或锚标记值
- .net - 时间:2019-05-10 标签:c#monogame逐渐旋转玩家身后的相机
- react-native - react-navigation 和 BottomTabs 中额外选项卡的“更多”覆盖 - 如何?
- r - 使用 `mutate` 和 `across` 时如何保留旧列并重命名新列