package com.ag.secp.platform.office.excel;

import com.ag.until.ExcelPoiUtil;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
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.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/1/5.
 */
public class ImpExcel {
    /**
     * 转换excle 2003和2007
     * @param in
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static Workbook create(InputStream in) throws
            IOException,InvalidFormatException {
        if (!in.markSupported()) {
            in = new PushbackInputStream(in, 8);
        }
        if (POIFSFileSystem.hasPOIFSHeader(in)) {
            return new HSSFWorkbook(in);
        }
        if (POIXMLDocument.hasOOXMLHeader(in)) {
            return new XSSFWorkbook(OPCPackage.open(in));
        }
        throw new IllegalArgumentException("你的excel版本目前poi解析不了");
    }
    /**
     * 获取excel数据list
     *
     * @Title
     * @Description TODO(获取excel数据list)
     * @param stream
     * @param startRow
     * @param startCell
     *            *@param sheetIndex
     * @return
     */
    public static List<HashMap<Integer, String>> readExcelEmptyBody(InputStream stream, int startRow,
                                                                    int endRow, int startCell, int endCell, int sheetIndex) {
        if (startRow < endRow && endRow != -1) {
            return null;
        }
        if (startCell < endCell && endCell != -1) {
            return null;
        }
        Workbook wb =null;
        List<HashMap<Integer, String>> resultList = new ArrayList<HashMap<Integer, String>>();

        try {
            wb = create(stream);
        } catch (InvalidFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        Sheet sheet=null;
        int sheets= wb.getNumberOfSheets();
        int rows=0;
        for(int m=0;m<sheets;m++){
            rows = wb.getSheetAt(m).getLastRowNum();

            if(rows>0){
                sheet = wb.getSheetAt(m);
                break;
            }
        }

        if(endRow != -1) {
            rows = endRow;
        }
        for (int i = startRow; i <= rows; i++) {
            Row row = sheet.getRow(i);
            if(row == null){
                continue;
            }
            int cells = row.getLastCellNum();
            if (endCell != -1) {
                cells = endCell;
            }
            int count = 0;
            HashMap<Integer, String> cellMap = new HashMap<Integer, String>();
            for (int j = startCell; j <= cells; j++) {
                Cell cell = row.getCell(j);
                String cellStr = getStringCellValue(cell);
                //System.out.print("\t" + cellStr);
                if(cellStr!=""||!cellStr.equals(""))
                    cellMap.put(count, cellStr);
                count++;

            }
            resultList.add(cellMap);
        }
        return resultList;
    }
    /**
     * 获取excel数据list
     *
     * @Title
     * @Description TODO(获取excel数据list)
     * @param stream
     * @param startRow 开始行数 从0 开始
     * @param startCell 开始列
     * @param endCell 开始列
     * @param startCell 开始列
     *            *@param sheetIndex
     * @return
     */
    public static List readExcelBody(InputStream stream, int startRow,
                                     int endRow, int startCell, int endCell, int sheetIndex) {
        if (startRow < endRow && endRow != -1) {
            return null;
        }
        if (startCell < endCell && endCell != -1) {
            return null;
        }
        List resultList = new ArrayList();
        try {
            Workbook wb = create(stream);
            Sheet sheet = wb.getSheetAt(sheetIndex);
            int rows = sheet.getLastRowNum();
            if (rows <= 0) {
                sheet = wb.getSheetAt(wb.getActiveSheetIndex());
                rows = sheet.getLastRowNum();
            }

            if (endRow != -1) {
                rows = endRow;
            }
            for (int i = startRow; i <= rows; i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;
                int cells = row.getLastCellNum();
                if (endCell != -1) {
                    cells = endCell;
                }
                if (cells==-1)
                {
                    continue;
                }
                int count = 0;
                Map cellMap = new HashMap();
                for (int j = startCell; j <= cells; j++) {
                    Cell cell = row.getCell(j);
                    String cellStr = getStringCellValue(cell);

                    cellMap.put(count, cellStr);
                    count++;

                }

                resultList.add(cellMap);

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        List<Integer> nullArr = new ArrayList<Integer>();
        nullArr.add(null);
        resultList.removeAll(nullArr);

        return resultList;
    }

    /**
     * 获取excel数据list
     *
     * @Title
     * @Description TODO(获取excel数据list)
     * @param stream
     * @param startRow
     * @param startCell
     *            *@param sheetName 使用工作表的名称
     * @return
     */
    public static List readExcelBody(InputStream stream, int startRow,
                                     int endRow, int startCell, int endCell, String sheetName) {
        if (startRow < endRow && endRow != -1) {
            return null;
        }
        if (startCell < endCell && endCell != -1) {
            return null;
        }
        List resultList = new ArrayList();
        try {
            Workbook wb = create(stream);
            Sheet sheet = wb.getSheet(sheetName);
            int rows = sheet.getLastRowNum();


            if (endRow != -1) {
                rows = endRow;
            }
            for (int i = startRow; i <= rows; i++) {
                Row row = sheet.getRow(i);

                int cells = row.getLastCellNum();
                if (endCell != -1) {
                    cells = endCell;
                }
                if (cells==-1)
                {
                    continue;
                }
                int count = 0;
                Map cellMap = new HashMap();
                for (int j = startCell; j <= cells; j++) {
                    Cell cell = row.getCell(j);
                    String cellStr = getStringCellValue(cell);

                    cellMap.put(count, cellStr);
                    count++;

                }

                resultList.add(cellMap);

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        List<Integer> nullArr = new ArrayList<Integer>();
        nullArr.add(null);
        resultList.removeAll(nullArr);

        return resultList;
    }

    /**
     *
     * @Title 获取列值string
     * @Description TODO(这里用一句话描述这个方法的作用)
     * @param cell
     * @return
     */
    private static String getStringCellValue(Cell cell) {
        String strCell = "";
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_FORMULA:
                try {
				/*
				 * 此处判断使用公式生成的字符串有问题，因为HSSFDateUtil.isCellDateFormatted(cell)判断过程中cell
				 * .getNumericCellValue();方法会抛出java.lang.NumberFormatException异常
				 */
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        Date date = cell.getDateCellValue();
                        SimpleDateFormat sdf = new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss");
                        strCell = sdf.format(date);
                        // strCell = (date.getYear() + 1900) + "-" +
                        // (date.getMonth() + 1) +"-" +
                        // date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
                        break;
                    } else {
                        strCell = String.valueOf(cell.getNumericCellValue());
                    }
                } catch (IllegalStateException e) {
                    strCell = String.valueOf(cell.getRichStringCellValue());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                strCell = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    double d = cell.getNumericCellValue();
                    Date date = HSSFDateUtil.getJavaDate(d);
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss");
                    strCell = sdf.format(date);
                    break;
                } else {
                    //strCell = String.valueOf(cell.getNumericCellValue());
                    DecimalFormat df = new DecimalFormat("0");
                    strCell = df.format(cell.getNumericCellValue());
                    break;
                }
            case Cell.CELL_TYPE_BOOLEAN:
                strCell = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                strCell = "";
                break;
            default:
                strCell = "";
                break;
        }
        return strCell;
    }
    //判断Excel表格是否有内容
    public static int excelIsNull(InputStream stream) {
        int rows = 0;
        try {
            Workbook wb = new HSSFWorkbook(stream);
            Sheet sheet = wb.getSheetAt(0);
            rows = sheet.getLastRowNum();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return rows;
    }

    public static void main(String[] args) {
        File file = new File("F://船舶总计划0401.xls");

        InputStream in = null;
        try {
            in = new FileInputStream(file);
            //ExcelPoiUtil.excelIsNull(in);
            List resultList=	ExcelPoiUtil.readExcelBody(in, 3, -1, 0, -1, 0);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
