【问题标题】:CellType cannot be resolved to a variable setCellType error Apache POI - JavaCellType 无法解析为变量 setCellType 错误 Apache POI - Java
【发布时间】:2017-09-27 19:39:30
【问题描述】:

我正在阅读 Apache poi 指南:https://poi.apache.org/spreadsheet/quick-guide.html

我决定继续在我的 Eclipse 项目中输入代码,在编写“阅读和重写工作簿”部分时,我在 Eclipse 中遇到了这个错误:

CellType 无法解析为变量

我想知道为什么?我在 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();

【问题讨论】:

    标签: java excel eclipse apache


    【解决方案1】:

    您必须考虑 POI 版本。 CellType 仅适用于新版本的 POI,Cell 是旧版本。 我建议您应该使用 POI 版本 3.15。在此版本中,您不必在为其设置值之前设置单元格类型。 (它有 setStringValuesetDateValue... 用于您的数据类型)

    【讨论】:

    • 好的,下载了 POI 3.15 jar 文件.. 所以我将 cell.setCellType(CellType.STRING); 更改为 cell.setStringvalue(); ?抱歉,我不知道该怎么做..
    • 你是对的。您只需要使用setStringValue 为该单元格设置一个字符串。一个post for your reference
    【解决方案2】:

    将此cell.setCellType(CellType.STRING); 更改为:

       cell.setCellType(STRING);
    

    【讨论】:

    • 上面写着STRING cannot be resolved to a variable
    【解决方案3】:

    使用 XSSFCell.CELL_TYPE_STRING 或 CellType.STRING

    【讨论】:

      猜你喜欢
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多