问题: setCellType
已弃用。
row.getCell(0, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellType(CellType.STRING);
到目前为止尝试过:
寻找替代品。没有用于将单元格类型设置为 STRING 的有用来源。感谢帮助!
问题: 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));
}
不是直接在 getCell 上执行操作,而是使用 XSSFCell 分别进行 get & set 操作
XSSFCell cell = (XSSFCell) row.getCell(cellNumber, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellType(CellType.STRING);
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){}