美文网首页
【POI】读取excel文件工具类

【POI】读取excel文件工具类

作者: 如雨随行2020 | 来源:发表于2022-01-30 00:13 被阅读0次
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.util.Date;

public class ExcelUtil {
    public static String getStringValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell == null) {
            return "";
        }
        if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
            return "";
        }
        return cell.getStringCellValue();
    }

    public static Date getDateValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell == null) {
            return null;
        }
        return cell.getDateCellValue();
    }

    public static Integer getIntegerValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell == null) {
            return null;
        }
        if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
            return null;
        }
        return (int) cell.getNumericCellValue();
    }

    public static Long getLongValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell == null) {
            return null;
        }
        if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
            return null;
        }
        return (long) cell.getNumericCellValue();
    }

    public static Float getFloatValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell == null) {
            return null;
        }
        if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
            return null;
        }
        return (float)cell.getNumericCellValue();
    }

    public static Double getDoubleValue(Row row, int index) {
        Cell cell = row.getCell(index);
        if (cell == null) {
            return null;
        }
        if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
            return null;
        }
        return cell.getNumericCellValue();
    }
}

相关文章

网友评论

      本文标题:【POI】读取excel文件工具类

      本文链接:https://www.haomeiwen.com/subject/zzeraltx.html