package com.sys.bus.excel.tool;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ExcelImportTool {
	 private final static int pointLen = 2;
	 public static List<Map<String, Object>> readData(String dirPath, String fileName, int sheetIndex, int startRowIndex, int startColumnIndex, int readSize){
		 List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		 Workbook workbook = getWorkbook(dirPath, fileName);
		 if (workbook != null) {
			 FormulaEvaluator evaluator=workbook.getCreationHelper().createFormulaEvaluator();
		   Sheet sheet = workbook.getSheetAt(sheetIndex);
	       int rowNum = 0;
	       int columNum = 0;
	       int endRowIndex = startRowIndex + readSize - 1;
	       if (readSize == Integer.MAX_VALUE) {
	         endRowIndex = Integer.MAX_VALUE;
	       }
	       if (sheet != null) {
	         if (sheet.getRow(0) != null) {
	           columNum = sheet.getRow(0).getLastCellNum();
	         }
	         rowNum = sheet.getLastRowNum()+1;
	       }
	       Cell cell = null;
	       if (startRowIndex < rowNum) {
	         for (int i = startRowIndex; (i < rowNum) && (i <= endRowIndex); i++) {
	           if (startColumnIndex < columNum) {
	             Map<String, Object> item = new HashMap();
	             for (int j = startColumnIndex; j < columNum; j++) {
					Row row = sheet.getRow(i);
					if(row==null){
						throw new RuntimeException("Excel第"+(i+1)+"行数据异常！请删除报错行附近空行数据！(此问题是部分单元格录入了空格等数据造成)");
					}
	               cell = row.getCell(j);//  Cell(j, i);
	               item.put("F" + (j + 1), readCell(cell,evaluator));
	             }
	             if (dataList.size() > readSize) break;
	             dataList.add(item);
	           }
	         }
	       }
	     }
		
		 return dataList;
	 }
	 
	 public static Object readCell(Cell cell,FormulaEvaluator evaluator)
	 {
	  if(cell==null){
		  return null;
	  }
		 String str = null;
	   if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
		   str =  String.valueOf(cell.getBooleanCellValue());
       } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
		   str =  String.valueOf(cell.getNumericCellValue());
		   if(str.endsWith(".0")&&str.length()>pointLen){
			   str = str.substring(0,str.length()-pointLen);
		   }

	   } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
		   CellValue ecell =  evaluator.evaluate(cell);
		   switch (ecell.getCellType()) {
			   case Cell.CELL_TYPE_STRING:
				   str=ecell.getStringValue();
				   break;
			   case Cell.CELL_TYPE_NUMERIC:
				   str=String.valueOf(ecell.getNumberValue());
				   if(str.endsWith(".0")&&str.length()>pointLen){
					   str = str.substring(0,str.length()-pointLen);
				   }
				   break;
			   case Cell.CELL_TYPE_BOOLEAN:
				   str =  String.valueOf(cell.getBooleanCellValue());
				   break;
			   default:
				   break;
		   }
       } else {
		   str =  cell.getStringCellValue();
       }
       // System.out.println("------"+str);
		 return str;
	 }
	private static String getCellValue(CellValue cell) {
		String cellValue = null;


		return cellValue;
	}
	 public static List<Map<String, Object>> readData(String dirPath, String fileName, int sheetIndex, int startRowIndex, int startColumnIndex)
	   {
	     return readData(dirPath, fileName, sheetIndex, startRowIndex, startColumnIndex, Integer.MAX_VALUE);
	  }
	 
	 public static Workbook getWorkbook(String dirPath, String fileName)
	  {
	    Workbook wb = null;
	    Workbook xwb = null;
	    try {
	      File file = getFile(dirPath, fileName);
	      if (file != null) {
	    	  InputStream is = new FileInputStream(getRealPath(dirPath, fileName));
	    	  xwb = new XSSFWorkbook(is);
	      }
	    } catch (Exception e) {
	    }
	    return xwb;
	  }
	 
	 public static File getFile(String dirPath, String fileName) {
	   File file = new File(getRealPath(dirPath, fileName));
	   if (file.exists()) {
	     return file;
	   } 
	   return null;
	 } 
	 
	 public static void deleteFile(String dirPath, String fileName){
	    File file = new File(getRealPath(dirPath, fileName));
	    if (file.exists()) {
	      file.delete();
	    }
	  }
	  
	 public static String getRealPath(String dirPath, String fileName){
		 String realPath = "";
	     if ((!"".equals(dirPath)) && (!"".equals(fileName))) {
	       realPath = dirPath + File.separator + fileName;
	     }
	     return realPath;
	 }
	 
}
