我正在使用apache-poi-3.9来处理Excel文档。 我在TEXT类型的文档中有一个单元格(单元格值为445)。但是,当我在Java中检查该单元格类型时,它会显示NUMERIC。有没有办法让细胞类型成为excel文档中的内容。 注意:单元格定义为TEXT。它可能包含字母数字值或数值,但我需要该类型为Text。
答案 0 :(得分:3)
尝试将单元格类型设置为字符串:
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
或将数字格式设置为文本:
CellStyle style = workbook.createCellStyle();
DataFormat format = wb.createDataFormat();
style.setDataFormat(format.getFormat("@"));
cell.setCellStyle(style);
检查以下链接以获取更多详细信息:
http://poi.apache.org/spreadsheet/quick-guide.html#Working+with+different+types+of+cells
http://poi.apache.org/spreadsheet/quick-guide.html#DataFormats
使用以前的代码后,从单元格中提取格式化的值:
DataFormatter formatter = new DataFormatter();
String formatted_value = formatter.formatCellValue(cell);