エクセルファイルを読み込むツール類

1941 ワード

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