package com.rehome.jpahefengweather.service.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelXlsx {

    private String filePath = null;

    //ReadCityExcelXls
    public ReadExcelXlsx(String dir, String fileName) {
        if (dir != null && fileName != null) {
            this.filePath = dir + File.separator + fileName;
        }
    }


    public void testReadExcel() {
        // 要读取的Excel文件路径
        File file = new File(this.filePath);
        if (!file.exists()) {
            System.out.println("excel文件不存在");
            return;
        }
        List excelList = this.readExcel(file);
        System.out.println("list中的数据打印出来");
        if (excelList != null && excelList.size() > 0) {
            System.out.println(String.valueOf(excelList.size()));
        }
        for (int i = 0; i < excelList.size(); i++) {
            List list = (List) excelList.get(i);
            for (int j = 0; j < list.size(); j++) {
                System.out.print(list.get(j));
            }
            System.out.println();
        }

    }

    // 去读Excel的方法readExcel，该方法的入口参数为一个File对象
    public List readExcel(File file) {
        try {
            //用流的方式先读取到你想要的excel的文件
            FileInputStream fis = new FileInputStream(file);
            //Get the workbook instance for XLSX file
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            // Excel的页签数量
            int sheet_size = workbook.getNumberOfSheets();
            for (int index = 0; index < sheet_size; index++) {
                List<List> outerList = new ArrayList<List>();
                // 每个页签创建一个Sheet对象
                XSSFSheet sheet = workbook.getSheetAt(index);
                //获取第一行
                int firstrow = sheet.getFirstRowNum();
                //获取最后一行
                int lastrow = sheet.getLastRowNum();
                //循环行数依次获取列数
                for (int i = firstrow; i < lastrow + 1; i++) {
                    //获取哪一行i
                    Row row = sheet.getRow(i);
                    if (row != null) {
                        //获取这一行的第一列
                        int firstcell = row.getFirstCellNum();
                        //获取这一行的最后一列
                        int lastcell = row.getLastCellNum();
                        //创建一个集合，用处将每一行的每一列数据都存入集合中
                        List<String> list = new ArrayList<>();
                        for (int j = firstcell; j < lastcell; j++) {
                            //获取第j列
                            Cell cell = row.getCell(j);
                            if (cell != null) {
                                list.add(cell.toString());
                            }
                        }
                        outerList.add(list);
                    }
                }
                fis.close();
                return outerList;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}