CellType
常量名 | 类型 | 值(int) |
---|---|---|
CELL_TYPE_NUMERIC | 数值型 | 0 |
CELL_TYPE_STRING | 字符串型 | 1 |
CELL_TYPE_FORMULA | 公式型 | 2 |
CELL_TYPE_BLANK | 空值 | 3 |
CELL_TYPE_BOOLEAN | 布尔型 | 4 |
CELL_TYPE_ERROR | 错误 | 5 |
对于Class XSSFCell中的以上这些属性是继承自Interface Cell的。
POI中单元格的数据类型
- 获取:int getCellType(),返回值为int类型
Excel:
POI读取单元格的CellType代码示例:
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class Demo {
@Test
public void test(){
File file = new File("src/demo.xlsx");
try {
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
if (row.getRowNum()<2){
continue;
}
for (Cell cell : row) {
if (cell.getColumnIndex()==1){
switch (cell.getCellType()){
case 0 : {
System.out.print(cell.getRow().getCell(0).getStringCellValue()+":");
System.out.println("CELL_TYPE_NUMERIC:数值型");
break;
}
case 1 : {
System.out.print(cell.getRow().getCell(0).getStringCellValue()+":");
System.out.println("CELL_TYPE_STRING:字符串型");
break;
}
case 2 : {
System.out.print(cell.getRow().getCell(0).getStringCellValue()+":");
System.out.println("CELL_TYPE_FORMULA:公式型");
break;
}
case 3 : {
System.out.print(cell.getRow().getCell(0).getStringCellValue()+":");
System.out.println("CELL_TYPE_BLANK:空值");
break;
}
case 4 : {
System.out.print(cell.getRow().getCell(0).getStringCellValue()+":");
System.out.println("CELL_TYPE_BOOLEAN:布尔型");
break;
}
case 5 : {
System.out.print(cell.getRow().getCell(0).getStringCellValue()+":");
System.out.println("CELL_TYPE_ERROR :错误");
break;
}
}
}
}
}
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
}
运行结果: