读取Excel文件,并用Map存储

package com.example.demo.getUniversitWebInfo1.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.poi.EncryptedDocumentException;
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;
import org.apache.poi.ss.util.CellRangeAddress;
import org.eclipse.jetty.util.StringUtil;

public class Test {
     
    public static void main(String[] args) throws IOException,EncryptedDocumentException{
     
        File file = new File("Excel文件路径");
        FileInputStream stream = new FileInputStream(file);
        List<Map<String, Object>> list = readXls(stream,1,0);
        System.out.println(list);
        System.out.println("====================");
        for (int i = 0; i < list.size(); i++) {
     
            System.out.println(list.get(i));
            Map<String, Object> myMap = list.get(i);
            Set<String> set = myMap.keySet();
            Iterator<String> it = set.iterator();
            while (it.hasNext()){
     
                String key = it.next();
                Object value = myMap.get(key);
                System.out.println("key的值:"+key);
                if (key.contains("姓")){
     
                    System.out.println("姓名对应的值为:"+value);
                }else if (key.contains("编号")){
     
                    System.out.println("编号:"+value);
                }
            }
        }
    }

    /**
     *
     * @Description:
     * @param is 文件流
     * @param rowStart 从第几行开始读取(不包含)
     * @param colStart 从第几列开始读取(不包含)
     * @return List>
     */
    public static List<Map<String, Object>> readXls(InputStream is, int rowStart, int colStart) throws IOException, EncryptedDocumentException {
     
        Workbook hssfWorkbook = WorkbookFactory.create(is);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        // Read the Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
     
            Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
     
                continue;
            }
            List<List<Map<String, String>>> headers = new ArrayList<>();
            //read the sheet header
            for(int i = 0; i < rowStart; i++){
     
                List<Map<String, String>> headerList = new ArrayList<>();
                Row headerRow = hssfSheet.getRow(i);
                for(int j = colStart; j < headerRow.getLastCellNum(); j++){
     
                    Map<String, String> map = new HashMap<>();
                    map.put("cellVal", headerRow.getCell(j)== null ? "" : headerRow.getCell(j).toString());
                    Map<String, String> mergedMap = isMergedRegion(hssfSheet, i, j);
                    if(mergedMap.get("isMerged").equals("yes")){
     
                        map.put("group", mergedMap.get("index"));
                    }
                    headerList.add(map);
                }
                if(i == rowStart - 1){
     
                    headers.add(headerList);
                }else{
     
                    headers.add(dealList(headerList));
                }
            }
            //拼接表头
            List<String> keys = joinHeader(headers);
            System.out.println(keys);
            for (int rowNum = rowStart; rowNum < hssfSheet.getLastRowNum(); rowNum++) {
     
                Row hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
     
                    Map<String, Object> map = new HashMap<String, Object>();
                    for (int i = colStart, j = hssfRow.getLastCellNum(); i < j; i++) {
     
                        map.put(keys.get(i - colStart), hssfRow.getCell(i));
                    }
                    list.add(map);
                }
            }
        }
        hssfWorkbook.close();
        return list;
    }

    //判断是否是合并单元格,并返回sheet中该合并单元格的索引
    public static Map<String, String> isMergedRegion(Sheet sheet,int row ,int column) {
     
        Map<String, String> map = new HashMap<>();
        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {
     
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if(row >= firstRow && row <= lastRow){
     
                if(column >= firstColumn && column <= lastColumn){
     
                    map.put("isMerged", "yes");
                    map.put("index", i+"");
                    return map;
                }
            }
        }
        map.put("isMerged", "no");
        return map;
    }

    /**
     * @Description:
     * @param
     * @return List
     */
    public static List<Map<String,String>> dealList(List<Map<String, String>> headerList) {
     
        if(headerList != null && !headerList.isEmpty()){
     
            for(int i = 1; i < headerList.size(); i++){
     
                Map<String, String> map = headerList.get(i);
                Map<String, String> map1 = headerList.get(i-1);
                if(StringUtil.isEmpty(map.get("cellVal")) && map1.containsKey("group") && map.containsKey("group") && map.get("group").equals(map1.get("group"))){
     
                    headerList.set(i, headerList.get(i-1));
                }
            }
            return headerList;
        }
        return null;
    }

    public static List<String> joinHeader(final List<List<Map<String, String>>> headers) {
     
        final List<Iterator<Map<String, String>>> it = new LinkedList<>();
        for (List<Map<String, String>> l : headers) {
     
            it.add(l.iterator());
        }

        final List<String> combined = new ArrayList<>();
        int index = 1;
        boolean flag = false;
        outer:
        while (true) {
     
            final StringBuilder sb = new StringBuilder();
            if(flag){
     
                index = 1;
            }
            for (final Iterator<Map<String, String>> i : it) {
     
                if (!i.hasNext()) {
     
                    break outer;
                }
                Map<String, String> map = i.next();
                sb.append(map.get("cellVal")+"-");
            }
            String str = sb.toString();
            str = str.replaceAll("-{1,}$", "");
            if(combined.contains(str)){
     
                str = str + "-"+index;
                index++;
                flag = false;
            }else{
     
                flag = true;
            }
            combined.add(str);
        }
        for (final Iterator<Map<String, String>> i : it) {
     
            if (i.hasNext()) {
     
                throw new IllegalArgumentException("Lists not the same length.");
            }
        }
        return combined;
    }
}

读取Excel文件,并用Map存储。拿到键的值,用键来获取到键所对应的值。

你可能感兴趣的:(poi)