package com.util;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 16094 on 2017/8/24.
 */
public class ReadExcel {

    private Workbook workbook;

    private static String desktopPath = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath();


    public ReadExcel(File excelFile) throws Exception {
        workbook = WorkbookFactory.create(new FileInputStream(excelFile));
    }

    public List<String> getData(int index) {

        try {
            List result = new ArrayList();
            int rowSize = 0;
            Cell cell = null;
            int numberOfSheets = Math.min(workbook.getNumberOfSheets(), index);
            for (int sheetIndex = 0; sheetIndex < numberOfSheets; sheetIndex++) {
                Sheet sheetAt = workbook.getSheetAt(sheetIndex);
                for (int rowIndex = 0; rowIndex <= sheetAt.getLastRowNum(); rowIndex++) {
                    Row row = sheetAt.getRow(rowIndex);
                    if (row == null) {
                        continue;
                    }
                    int tempRowSize = row.getLastCellNum() + 1;
                    if (tempRowSize > rowSize) {
                        rowSize = tempRowSize;
                    }
                    List mapList = new ArrayList();
                    // mapList.clear();
                    //boolean hasValue = false;
                    for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex = (short) (columnIndex + 1)) {
                        String value = "";
                        // String key = "";
                        cell = row.getCell(columnIndex);
                        if (cell != null) {
                            switch (cell.getCellType()) {
                                case 1:
                                    value = cell.getStringCellValue();
                                    //  key = "String";
                                    break;
                                case 0:
                                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                        Date date = cell.getDateCellValue();
                                        if (date != null)
                                            value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                                        else {
                                            value = "";
                                        }
                                        //  key = "Date";
                                    } else {
                                        value = new DecimalFormat("0").format(cell.getNumericCellValue());
                                        // key = "Integer";
                                    }
                                    break;
                                case 2:
                                    if (!cell.getStringCellValue().equals(""))
                                        value = cell.getStringCellValue();
                                    else {
                                        value = cell.getNumericCellValue() + "";
                                    }
                                    // key = "String";
                                    break;
                                case 3:
                                    break;
                                case 5:
                                    value = "";
                                    // key = "";
                                    break;
                                case 4:
                                    value = cell.getBooleanCellValue() == true ? "Y" : "N";
                                    //key = "boolean";
                                    break;
                                default:
                                    value = "";
                                    //key = "";
                            }
                        }
                        if (value == null || value.equals("")) {
                            break;
                        }
                        if ((columnIndex == 0) && (value.trim().equals(""))) {
                            break;
                        }
                        //stringStringMap.put(rightTrim(key), );
                        mapList.add(rightTrim(value));
                        //  hasValue = true;
                    }
                    result.add(mapList);
//                    if (hasValue) {
//                        result.add(mapList);
//                    }
                }
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String rightTrim(String str) {
        if (str == null) {
            return "";
        }
        int length = str.length();
        for (int i = length - 1; (i >= 0) &&
                (str.charAt(i) == ' '); i--) {
            length--;
        }
        return str.substring(0, length);
    }


    public static void main(String[] args) throws Exception {
        File file = new File(desktopPath + "/Outcomes.xlsx");
        List datas = new ReadExcel(file).getData(1);
        for (int i = 0; i < datas.size(); i++) {
            List listinfo = (List) datas.get(i);
            for (int j = 0; j < listinfo.size(); j++) {
                System.out.print(listinfo.get(j) + "				");
            }
        }
    }
}
