读取EXCEL的公共方法

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
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;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class ExcelParse {
    /**
     * 
     * 解析EXCEL得到数据列表 <br>
     * 〈功能详细描述〉
     * 
     * @param excelFilePath:EXCEL文件路径
     * @param columnNames:EXCEL中的一行中的字段名数组
     * @param sheetIndex:读取第几个sheet页
     * @param startRow:从第几行开始读
     * @param maxRow:最大读取多少行
     * @return 数据列表
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static List<Map<String, Object>> parse(String excelFilePath, String[] columnNames, int sheetIndex,
            int startRow, int maxRow) {
        FileInputStream is = null;
        // 读取到的EXCEL数据列表
        List<Map<String, Object>> excelDataList = new ArrayList<Map<String, Object>>();

        try {
            is = new FileInputStream(excelFilePath);
            // 获取EXCEL工作簿
            Workbook workbook = WorkbookFactory.create(is);
            // 获取第几个sheetIndex页
            Sheet sheet = workbook.getSheetAt(sheetIndex);

            // 获取EXCEL中的行数
            int rowNumber = sheet.getPhysicalNumberOfRows();
            // 超过指定行
            if (rowNumber > maxRow) {
                return null;
            }

            // 遍历EXCEL取出每一行的数据
            for (int i = startRow - 1; i < rowNumber; i++) {
                Map<String, Object> lineMap = new HashMap<String, Object>();
                // 获取EXCEL中的行
                Row row = sheet.getRow(i);

                // 遍历每一列的数据
                for (int j = 0; j < columnNames.length; j++) {

                    // 获取该行对应的列(即每个单元格的值 )
                    Cell cell = row.getCell(j);
                    String value = cell.getStringCellValue();

                    // 数据行
                    lineMap.put(columnNames[j], value);
                }

                excelDataList.add(lineMap);
            }

        } catch (Exception e) {
            // 日志
        } finally {
            IOUtils.closeQuietly(is);
        }
        return excelDataList;
    }

    /**
     * 
     * 默认读取第一个sheet页,从第3行开始读取,最多读取1000行 <br>
     * 〈功能详细描述〉
     * 
     * @param excelFilePath:EXCEL文件路径
     * @param columnNames:EXCEL中的一行中的字段名数组
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static List<Map<String, Object>> parse(String excelFilePath, String[] columnNames) throws Exception {
        return parse(excelFilePath, columnNames, 0, 3, 1000);
    }

    public static void main(String[] args) throws Exception {
        String[] columnNames = { "a", "b", "c" };
        List<Map<String, Object>> excelDataList = parse("D:/excel.xls", columnNames);
        System.out.println(excelDataList);
    }
}

你可能感兴趣的:(Excel操作)