解析excel

解析excel

package com.util;

import org.apache.poi.ss.usermodel.*;

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

/**
 * 解析excel返回
 * Map<Integer, Map<String, Object>> 带表头getSheetDataToMap方法
 * List<Map<Integer, Object>> 不带表头 getSheetDataToList方法
 * ExcelReader eh = new ExcelReader("/Users/ZYB/Downloads/会员.xlsx", "friendurl");
 * friendurl  参数为页名,可设置为null,则默认查找第一个页
 * <p>
 * Created by zyb on 16/8/1.
 */
public class ExcelReader {
    private final String filePath;
    private final String sheetName;
    private Sheet sheet;

    public ExcelReader(String filePath, String sheetName) {
        this.filePath = filePath;
        this.sheetName = sheetName;
        this.load();
    }

    @SuppressWarnings({ "MethodCanBeVariableArityMethod", "Annotation" })
    public static void main(String[] args) {
        //ExcelReader eh = new ExcelReader("/develop/IdeaProjects_assoft/links/WebContent/fileUpload/links/0712.xls", "Sheet0");
        //        ExcelReader eh = new ExcelReader("/develop/ASSOFT/中国施工企业管理协会/会员.xlsx", "friendurl");
        //        ExcelReader eh = new ExcelReader("D:/cui/学习/中施企/upload.xlsx", null);
        //
        //        List<Map<Integer, Object>> list = eh.getSheetDataToList();
        //输出所有数据
        //        for (int i = 0; i < list.size(); i++) {
        //            Map<Integer, Object> map = list.get(i);
        //            for (int j = 0; j < map.size(); j++) {
        //                System.out.println(map.get(j));
        //
        //            }
        //        }
    }

    /**
     * 获取某个sheet内的所有数据
     *
     * @return "List<Map<Integer, Object>>"结构数据集
     */
    public List<Map<Integer, Object>> getSheetDataToList() {
        List<Map<Integer, Object>> list = new ArrayList<Map<Integer, Object>>();
        int numOfRows = sheet.getLastRowNum() + 1;
        for (int i = 0; i < numOfRows; i++) {
            Row row = sheet.getRow(i);
            Map<Integer, Object> maps = new HashMap<Integer, Object>();
            if (row != null) {
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    //noinspection AutoBoxing
                    maps.put(j, this.getCellValue(cell));
                }
            }
            list.add(maps);
        }
        return list;
    }

    /**
     * 获取某个sheet内的所有数据
     * 获取第一行为表头,同时为Map<String, Object>中的key值
     *
     * @return "Map<Integer, Map<String, Object>>"结构结果集
     */
    public Map<Integer, Map<String, Object>> getSheetDataToMap() {
        Map<Integer, Map<String, Object>> maplist = new HashMap<Integer, Map<String, Object>>();
        Map<Integer, Object> map = new HashMap<Integer, Object>();
        int numOfRows = sheet.getLastRowNum() + 1;
        for (int i = 0; i < numOfRows; i++) {
            Row row = sheet.getRow(i);
            Map<String, Object> maps = new HashMap<String, Object>();
            if (row != null) {
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    if (i == 0) {
                        //noinspection AutoBoxing
                        map.put(j, this.getCellValue(cell));
                    } else {
                        //noinspection AutoBoxing
                        maps.put((String) map.get(j), this.getCellValue(cell));
                    }
                }
            }
            if (i > 0) {
                //noinspection AutoBoxing
                maplist.put(i - 1, maps);
            }
        }
        return maplist;
    }

    /**
     * 资源文件的加载
     */
    private void load() {
        FileInputStream inStream = null;
        try {
            inStream = new FileInputStream(new File(filePath));
            Workbook workBook = WorkbookFactory.create(inStream);
            if (sheetName == null) {
                sheet = workBook.getSheetAt(0);
            } else {
                sheet = workBook.getSheet(sheetName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取单元格内的数据
     *
     * @param cell Cell对象
     * @return String字符串
     */
    private String getCellValue(Cell cell) {
        String cellValue = "";
        DataFormatter formatter = new DataFormatter();
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
                    cellValue = formatter.formatCellValue(cell);
                } else {
                    double value = cell.getNumericCellValue();
                    int intValue = (int) value;
                    cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value);
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK:
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR:
                cellValue = "";
                break;
            default:
                cellValue = cell.toString().trim();
                break;
            }
        }
        return cellValue.trim();
    }
}

 上一篇
观察者模式实现 观察者模式实现
观察者模式实现package com.wh.observer; /** * 抽象观察者角色 * Created by zyb on 15/11/12. */ public interface Watchers { /**
2019-08-19
下一篇 
读取目录下文件、文件夹 读取目录下文件、文件夹
读取目录下文件、文件夹File f = new File("D:/app"); File[] childs = f.listFiles(); for (int i = 0; i < childs.length; i++) {
2019-08-19
  目录