poi获取excel的数据

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;

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

public class ReadExcel {
    public static void main(String[] args) {
        try {
            List<Map<String, String>> maps = readExcel("e:/User/xxx/xx.xls");
            for (Map<String,String> map : maps){
                Set<Map.Entry<String,String>> es = map.entrySet();
                for (Map.Entry<String, String> entry : es) {
                    System.out.println(entry.getValue()+"\t");
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    根据excel文件路径返回list list对应一张表的数据,list里面是多个map集合,一个map对应一行数据
    public static List<Map<String,String>> readExcel(String fileName) throws Exception {
        List<Map<String,String>> sheetList = new ArrayList<>();
        FileInputStream fis = new FileInputStream(fileName);
        Workbook workbook = null;
        if(fileName.toLowerCase().endsWith("xlsx")){
            workbook = new XSSFWorkbook(fis);
        }else if (fileName.toLowerCase().endsWith("xls")){
            workbook = new HSSFWorkbook(fis);
        }
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        rowIterator.next();//跳过第一行
        while (rowIterator.hasNext()){
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            Map<String,String> mapRow = new HashMap<String,String>();
            while (cellIterator.hasNext()){
                Cell cell = cellIterator.next();
                int a = cell.getCellType();//获取列的类型
                if(a==0){
                    double d = cell.getNumericCellValue();
                    mapRow.put(cell.getColumnIndex()+"",d+"");
                }else if (a==1){
                    String v = cell.getStringCellValue();
                    mapRow.put(cell.getColumnIndex()+"",v);
                }
            }
            sheetList.add(mapRow);
        }
        return sheetList;
    }


    public static List<Map<String,String>> readExcel1(InputStream inputStream, String fileName) throws Exception {
        List<Map<String,String>> sheetList = new ArrayList<>();
        InputStream fis = inputStream;
        Workbook workbook = null;
        if(fileName.toLowerCase().endsWith("xlsx")){
            workbook = new XSSFWorkbook(fis);
        }else if (fileName.toLowerCase().endsWith("xls")){
            workbook = new HSSFWorkbook(fis);
        }
        Sheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        rowIterator.next();//跳过第一行
        while (rowIterator.hasNext()){
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            Map<String,String> mapRow = new HashMap<String,String>();
            while (cellIterator.hasNext()){
                Cell cell = cellIterator.next();
                int a = cell.getCellType();//获取列的类型
                if(a==0){
                    double d = cell.getNumericCellValue();
                    mapRow.put(cell.getColumnIndex()+"",d+"");
                }else if (a==1){
                    String v = cell.getStringCellValue();
                    mapRow.put(cell.getColumnIndex()+"",v);
                }
            }
            sheetList.add(mapRow);
        }
        return sheetList;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值