Java 如何使用XSSF检查Excel单元格的类型
我正在检查单元格是数字还是空的 所以我写了这个代码Java 如何使用XSSF检查Excel单元格的类型,java,xssf,Java,Xssf,我正在检查单元格是数字还是空的 所以我写了这个代码 ... int columnIndex = 1 while(columnIndex < numberOfColumns && matrixSheet.getRow(0).getCell(columnIndex).stringCellValue != '') { if (matrixSheet.getRow(rowIndex).getCell(columnIndex).getCellType() == Cell.CE
...
int columnIndex = 1
while(columnIndex < numberOfColumns && matrixSheet.getRow(0).getCell(columnIndex).stringCellValue != '') {
if (matrixSheet.getRow(rowIndex).getCell(columnIndex).getCellType() == Cell.CELL_TYPE_NUMERIC)
orderOfActivities.add(matrixSheet.getRow(rowIndex).getCell(columnIndex) - 1, matrixSheet.getRow(0).getCell(columnIndex))
columnIndex++
}
...
你确定第0行的列也是数字吗?@RobertKock我用Excel文件的格式更新了我的问题。在第0行和第0列中,值为字符串,但在其他单元格中,值为数字或空
A | B | C | D | E |
1 | Text1 Text2 Text3 ...
2 | Val1 2 1
3 | Val2 1 2
4 | Val3 1
5 | ...
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 java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelFileTest {
private static final String FILE_NAME = "D:\\Book2.xlsx";
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
Object[][] datatypes = {
{"Datatype", "Type", "Size(in bytes)"},
{"int", "Primitive", 2},
{"float", "Primitive", 4},
{"double", "Primitive", 8},
{"char", "Primitive", 1},
{"String", "Non-Primitive", "No fixed size"}
};
int rowNum = 0;
System.out.println("Creating excel");
for (Object[] datatype : datatypes) {
Row row = sheet.createRow(rowNum++);
int colNum = 0;
for (Object field : datatype) {
Cell cell = row.createCell(colNum++);
if (field instanceof String) {
cell.setCellValue((String) field);
} else if (field instanceof Integer) { //here check integer Type
cell.setCellValue((Integer) field);
}
}
}
try {
FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}