cherry.goods.excel.ExcelReader.java Source code

Java tutorial

Introduction

Here is the source code for cherry.goods.excel.ExcelReader.java

Source

/*
 * Copyright 2014,2015 agwlvssainokuni
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cherry.goods.excel;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC;
import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;

import java.io.Closeable;
import java.math.BigDecimal;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Microsoft Excel?<br />
 */
public class ExcelReader implements Closeable {

    /** ??Microsoft Excel */
    private final Workbook workbook;

    /** ?? */
    private Sheet currentSheet;

    /** ?? */
    private Iterator<Row> rowIterator;

    /**
     * Microsoft Excel???<br />
     * ?????????{@link #setCurrentSheet(int)}???
     * 
     * @param workbook ??Microsoft Excel
     */
    public ExcelReader(Workbook workbook) {
        this.workbook = workbook;
        setCurrentSheet(this.workbook.getActiveSheetIndex());
    }

    /**
     * @return ??Microsoft Excel??
     */
    public int getNumberOfSheets() {
        return workbook.getNumberOfSheets();
    }

    /**
     * @param sheetIndex ?
     * @return ???????
     */
    public String getSheetName(int sheetIndex) {
        return workbook.getSheetName(sheetIndex);
    }

    /**
     * ???<br />
     * 
     * @param sheetIndex ?????
     */
    public void setCurrentSheet(int sheetIndex) {
        currentSheet = workbook.getSheetAt(sheetIndex);
        rowIterator = currentSheet.rowIterator();
    }

    /**
     * ?<br />
     * ???1(1)
     * 
     * @return 1(1)
     */
    public String[] read() {
        if (!rowIterator.hasNext()) {
            return null;
        }
        Row row = rowIterator.next();
        int lastCellNum = row.getLastCellNum();
        if (lastCellNum < 0) {
            return new String[0];
        }
        String[] record = new String[lastCellNum];
        for (Cell cell : row) {
            record[cell.getColumnIndex()] = getCellValueAsString(cell);
        }
        return record;
    }

    /**
     * Microsoft Excel??<br />
     * ??????
     */
    @Override
    public void close() {
        // NOTHING
    }

    /**
     * ??????<br />
     * 
     * @param cell ??
     * @return ?
     */
    private String getCellValueAsString(Cell cell) {
        switch (cell.getCellType()) {
        case CELL_TYPE_NUMERIC:
            return getNumericCellValueAsString(cell);
        case CELL_TYPE_STRING:
            return cell.getStringCellValue();
        case CELL_TYPE_FORMULA:
            switch (cell.getCachedFormulaResultType()) {
            case CELL_TYPE_NUMERIC:
                return getNumericCellValueAsString(cell);
            case CELL_TYPE_STRING:
                return cell.getStringCellValue();
            case CELL_TYPE_BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            default:
                return null;
            }
        case CELL_TYPE_BLANK:
            return "";
        case CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        default:
            return null;
        }
    }

    /**
     * ??????
     * 
     * @param cell ??
     * @return ?
     */
    private String getNumericCellValueAsString(Cell cell) {
        BigDecimal value = BigDecimal.valueOf(cell.getNumericCellValue());
        try {
            return value.toBigIntegerExact().toString();
        } catch (ArithmeticException ex) {
            return value.toPlainString();
        }
    }

}