package com.fdkankan.agent.util;

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    private static Logger log = LoggerFactory.getLogger("programLog");

    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    public static  List<Map<String, String>> readExcelForManageAgent(String finalXlsxPath) {
        OutputStream out = null;
        try {
            List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
            // 读取Excel文档
            File finalXlsxFile = new File(finalXlsxPath);
            if (!finalXlsxFile.exists()) {
                return null;
            }
            Workbook workBook = getWorkbok(finalXlsxFile);
            // sheet 对应一个工作页
            Sheet sheet = workBook.getSheetAt(0);

            int rowNumber = sheet.getLastRowNum();
            log.info("原始数据总行数，除属性列：" + rowNumber);
            for (int i = 1; i <= rowNumber; i++) {
                Row row = sheet.getRow(i);
                Map<String, String> dataMap = new HashMap<String, String>();
                for (int j = 0; j < 5; ++j) {
                    Cell cell = row.getCell(j);
                    if (cell == null) {
                        break;
                    }
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    String str = cell.getStringCellValue();
                    dataMap.put(String.valueOf(j), str);
                }
                dataList.add(dataMap);
            }
            return dataList;
        } catch (Exception e) {
            StringWriter trace = new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            log.error(trace.toString());
            return null;
        }
    }


    /**
     * 判断Excel的版本,获取Workbook
     */
    public static Workbook getWorkbok(File file) throws IOException {
        try {
            Workbook wb = null;
            InputStream in = new FileInputStream(file);

            if (!in.markSupported()) {
                in = new PushbackInputStream(in, 8);
            }

            if (file.getName().endsWith(EXCEL_XLS)) {
                wb = new HSSFWorkbook(in);
            } else if (file.getName().endsWith(EXCEL_XLSX)) {
                wb = new XSSFWorkbook(in);
            } else {
                log.error("你的excel版本目前poi解析不了");
            }
            return wb;
        } catch (Exception e) {
            StringWriter trace = new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            log.error(trace.toString());
            return null;
        }
    }
}
