简体   繁体   English

如何使用XSSF检查Excel单元格的类型

[英]How to check type of Excel cell with XSSF

I'm trying to check if cell is numeric or empty. 我正在尝试检查单元格是数字还是空。

So I wrote this code 所以我写了这段代码

...
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++
}
...

However, I got this error : 但是,我得到了这个错误:

Reason: groovy.lang.MissingMethodException: No signature of method: org.apache.poi.xssf.usermodel.XSSFCell.minus() is applicable for argument types: (java.lang.Integer) values: [1] Possible solutions: find(), is(java.lang.Object), find(groovy.lang.Closure), any(), print(java.lang.Object), with(groovy.lang.Closure) 原因:groovy.lang.MissingMethodException:无方法签名:org.apache.poi.xssf.usermodel.XSSFCell.minus()适用于参数类型:(java.lang.Integer)值:[1]可能的解决方案:find (),is(java.lang.Object),find(groovy.lang.Closure),any(),print(java.lang.Object)和with(groovy.lang.Closure)

On the following line : if (matrixSheet.getRow(rowIndex).getCell(columnIndex).getCellType() == Cell.CELL_TYPE_NUMERIC) 在以下行上: if (matrixSheet.getRow(rowIndex).getCell(columnIndex).getCellType() == Cell.CELL_TYPE_NUMERIC)

Someone has any idea ? 有人有什么主意吗? Thanks a lot! 非常感谢!

UPDATE 1 更新1

     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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM