setCellType(CellType.STRING) 已弃用

setCellType(CellType.STRING) is deprecated

问题: setCellType 已弃用。

 row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellType(CellType.STRING);

目前已尝试:

已搜索替换。没有将单元格类型设置为 STRING 的有用来源。感谢帮助!

您可以直接调用 row.setCellValue(String) 您不必事先设置单元格类型。

来自文档:

@deprecated This method is deprecated and will be removed in POI 5.0.
     * Use explicit {@link #setCellFormula(String)}, <code>setCellValue(...)</code> or {@link #setBlank()}
     * to get the desired result.

之前:

       row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellType(                                
       contentValues.put(ITEMCODE, row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue());

之后:

InputStream strm =getClass().getResourceAsStream("Sample.xls"));
Workbook wb = WorkbookFactory.create(strm);
DataFormatter Stringform = new DataFormatter();
FormulaEvaluator Formeval = new HSSFFormulaEvaluator((HSSFWorkbook) wb);

Sheet sheet= wb.getSheetAt(0);
Iterator<Row> rit = sheet.rowIterator();

while(rit.hasNext()){

    Row row = rit.next();
    Cell cellValue = row.getCell(0);
    Formeval.evaluate(row.getCell(0)); // Returns string
    String cellValueStr = Stringform.formatCellValue(row.getCell(0),Formeval);
    
    ContentValues contentValues = new ContentValues();

    contentValues.put(DNNO, Stringform.formatCellValue(row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK),Formeval));
                                                                 
}

2021 UPDATE仍然没有解决方案,所以要实现这个(例如,将单元格类型设置为十进制),您可以使用此代码:

double someValue = 50.0;
CellStyle styleDecimal = workbook.createCellStyle();
styleDecimal.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
row.getCell(0).setCellStyle(styleDecimal);
try{
    Double.valueOf(someValue);
    row.getCell(0).setCellValue(Double.valueOf(someValue));
}catch (Exception ex){}

不直接对getCell进行操作,而是使用XSSFCell分别进行get和set操作

XSSFCell cell = (XSSFCell) row.getCell(cellNumber, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);