ExcelReadUtil.java 1.32 KB
package com.cjs.cms.util.poi;

import java.util.ArrayList;
import java.util.List;

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

/**
 * 读取Excel内容
 * 
 * @author tongyufu
 *
 */
public class ExcelReadUtil {

    private ExcelReadConfig config;

    public ExcelReadUtil(ExcelReadConfig config) {
        this.config = config;
    }

    /**
     * 读取Excel内容
     * @param input
     * @throws Exception
     */
    public List<String[]> read() throws Exception {
        List<String[]> dataList = new ArrayList<String[]>();
        Workbook wb = WorkbookFactory.create(config.getSourceFile());
        Sheet sheet = wb.getSheetAt(0);
        int rowCount = sheet.getLastRowNum() + 1;
        Row row = null;

        for (int i = config.getStartRow(); i < rowCount; i++) {
            row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            short cellCount = row.getLastCellNum();
            String[] rowValue = new String[cellCount];
            for (short j = 0; j < cellCount; j++) {
                rowValue[j] = ExcelUtil.getCellValue(row.getCell(j));
            }
            dataList.add(rowValue);
        }
        return dataList;
    }

}