我正在阅读 Apache poi 指南:https://poi.apache.org/spreadsheet/quick-guide.html
我决定继续将代码输入到我的 Eclipse 项目中,在编写“阅读和重写工作簿”部分时,我在 Eclipse 中遇到了以下错误:
CellType cannot be resolved to a variable
我想知道为什么?我在 Stack Overflow 的另一篇文章中发现要更改此行:
cell.setCellType(CellType.STRING);
至:
cell.setCellType(HSSFCellType.STRING);//or XSSFCell
但是不行,是我的代码有问题吗? (顺便说一句,我从 apache 的网站复制并粘贴)
InputStream inp = new FileInputStream("workbook.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(CellType.STRING);
cell.setCellValue("a test");
//Write
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
最佳答案
您必须考虑 POI 版本。 CellType
仅适用于新版本的 POI,Cell
为旧版本。
我建议您使用 POI 版本 3.15。在此版本中,您不必在设置单元格类型之前设置单元格类型。 (它有 setStringValue
或 setDateValue
...适合您的数据类型)
关于java - CellType 无法解析为变量 setCellType 错误 Apache POI - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43701326/