org.beanfuse.transfer.importer.reader.ExcelItemReader.java Source code

Java tutorial

Introduction

Here is the source code for org.beanfuse.transfer.importer.reader.ExcelItemReader.java

Source

//$Id: ExcelReader.java,v 1.1 2007-3-24 ?12:30:52 chaostone Exp $
/*
 * Copyright c 2005-2009
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 * 
 */
/********************************************************************************
 * @author chaostone
 * 
 * MODIFICATION DESCRIPTION
 * 
 * Name           Date          Description 
 * ============         ============        ============
 *chaostone      2007-3-24         Created
 *  
 ********************************************************************************/

package org.beanfuse.transfer.importer.reader;

import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.beanfuse.transfer.Transfer;

/**
 * Excel????
 * 
 * @author chaostone
 * 
 */
public class ExcelItemReader implements ItemReader {

    public static Logger logger = LoggerFactory.getLogger(ExcelItemReader.class);

    /** ? */
    public static int DEFAULT_HEADINDEX = 0;

    public static NumberFormat numberFormat;

    static {
        numberFormat = NumberFormat.getInstance();
        numberFormat.setGroupingUsed(false);
    }
    public static final int sheetNum = 0;

    int headIndex;
    /**
     * ??? ???0,1
     */
    int indexInSheet;

    /**
     * 0?????
     */
    int attrCount = 0;

    /**
     * ?
     */
    HSSFWorkbook workbook;

    public ExcelItemReader() {
        super();
    }

    public ExcelItemReader(InputStream is) {
        try {
            this.workbook = new HSSFWorkbook(is);
            this.headIndex = DEFAULT_HEADINDEX;
            this.indexInSheet = headIndex + 1;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public ExcelItemReader(HSSFWorkbook workbook, int headIndex) {
        super();
        this.workbook = workbook;
        this.headIndex = headIndex;
        this.indexInSheet = headIndex + 1;
    }

    public void setWorkbook(HSSFWorkbook wb) {
        this.workbook = wb;
    }

    /**
     * ??
     */
    public String[] readDescription() {
        HSSFSheet sheet = workbook.getSheetAt(0);
        return readLine(sheet, 0);
    }

    public String[] readTitle() {
        HSSFSheet sheet = workbook.getSheetAt(0);
        String[] attrs = readLine(sheet, headIndex);
        attrCount = attrs.length;
        return attrs;
    }

    /**
     * ????
     * 
     * @param sheet
     * @param rowIndex
     * @return
     */
    protected String[] readLine(HSSFSheet sheet, int rowIndex) {
        HSSFRow row = sheet.getRow(rowIndex);
        if (logger.isDebugEnabled()) {
            logger.debug("values count:{}" + row.getLastCellNum());
        }
        List attrList = new ArrayList();
        for (short i = 0; i < row.getLastCellNum(); i++) {
            HSSFCell cell = row.getCell(i);
            if (null != cell) {
                String attr = cell.getRichStringCellValue().getString();
                if (StringUtils.isEmpty(attr)) {
                    break;
                } else {
                    attrList.add(attr.trim());
                }
            } else {
                break;
            }
        }
        String[] attrs = new String[attrList.size()];
        attrList.toArray(attrs);
        logger.debug("has attrs {}", attrs);
        return attrs;
    }

    public Object read() {
        HSSFSheet sheet = workbook.getSheetAt(sheetNum);
        if (indexInSheet > sheet.getLastRowNum()) {
            return null;
        }
        HSSFRow row = sheet.getRow(indexInSheet);
        indexInSheet++;
        // ,
        if (row == null) {
            return new Object[attrCount];
        } else {
            Object[] values = new Object[((attrCount != 0) ? attrCount : row.getLastCellNum())];
            for (short k = 0; k < values.length; k++) {
                String celValue = getCelValue(row.getCell(k));
                if (null != celValue) {
                    celValue = celValue.trim();
                }
                values[k] = celValue;
            }
            return values;
        }
    }

    /**
     * @see ?cell??
     * @param cell
     * @param objClass
     * @return
     */
    private String getCelValue(HSSFCell cell) {
        if ((cell == null) || (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK))
            return "";
        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            return cell.getRichStringCellValue().getString();
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            return numberFormat.format(cell.getNumericCellValue());
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
            if (cell.getBooleanCellValue())
                return "true";
            else
                return "false";
        } else {
            return "";
        }
    }

    public String getFormat() {
        return Transfer.EXCEL;
    }

    public int getHeadIndex() {
        return headIndex;
    }

    public void setHeadIndex(int headIndex) {
        this.headIndex = headIndex;
    }

}