package org.fouram.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

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.fouram.core.base.model.PageData;
import org.springframework.web.multipart.MultipartFile;

/**
 * 从EXCEL导入到数据库 创建人：FH 创建时间：2014年12月23日
 * 
 * @version
 */
public class ExcelReadUtil {

	public static List<PageData> readExcel(String filePath, String fileName, int startRow, int startCol,  int sheetnum) {
		boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
		try {
			if (isExcel2007(fileName)) {
				isExcel2003 = false;
			}
			File target = new File(filePath, fileName);
			FileInputStream fi = new FileInputStream(target);
			return read(fi, startRow, startCol, sheetnum, isExcel2003);
		} catch (Exception e) {
			LoggerUtil.error(e);
			return null;
		}
	}

	public static List<PageData> readExcel(MultipartFile file, int startRow, int startCol, int sheetNum) {
		boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
		try {
			if (isExcel2007(file.getOriginalFilename())) {
				isExcel2003 = false;
			}
			InputStream fi = file.getInputStream();
			return read(fi, startRow, startCol, sheetNum, isExcel2003);
		} catch (Exception e) {
			LoggerUtil.error(e);
			return null;
		}
	}

	/**
	 *
	 * @param startrow
	 *            //开始行号
	 * @param startcol
	 *            //开始列号
	 * @param sheetnum
	 *            //sheet
	 * @return list
	 */
	@SuppressWarnings("resource")
	public static List<PageData> read(InputStream fi, int startRow, int startCol, int sheetNum, boolean isExcel2003) {
		List<PageData> varList = new ArrayList<>();
		try {
			Workbook wb;
			if (isExcel2003) {// 当excel是2003时,创建excel2003
				wb = new HSSFWorkbook(fi);
			} else {// 当excel是2007时,创建excel2007
				wb = new XSSFWorkbook(fi);
			}
			Sheet sheet = wb.getSheetAt(sheetNum); // sheet 从0开始
			int rowNum = sheet.getLastRowNum() + 1; // 取得最后一行的行号

			for (int i = startRow; i < rowNum; i++) { // 行循环开始
				Row row = sheet.getRow(i); // 行
				PageData varpd = new PageData();
				int cellNum = row.getLastCellNum(); // 每行的最后一个单元格位置
				for (int j = startCol; j < cellNum; j++) { // 列循环开始
					Cell cell = row.getCell(Short.parseShort(j + ""));
					String cellValue = null;
					if (null != cell) {
						DecimalFormat df = new DecimalFormat("#");
						switch (cell.getCellType()) { // 判断excel单元格内容的格式，并对其进行转换，以便插入数据库
						case 0:
							cellValue = df.format(cell.getNumericCellValue());
							break;
						case 1:
							cellValue = cell.getStringCellValue();
							break;
						case 2:
							cellValue = cell.getCellFormula();
							break;
						case 3:
							cellValue = "";
							break;
						case 4:
							cellValue = String.valueOf(cell.getBooleanCellValue());
							break;
						case 5:
							cellValue = String.valueOf(cell.getErrorCellValue());
							break;
						}
					} else {
						cellValue = "";
					}
					varpd.put("var" + j, cellValue);
				}
				varList.add(varpd);
			}

		} catch (Exception e) {
			LoggerUtil.error(e);
		}
		return varList;
	}

	// @描述：是否是2003的excel，返回true是2003
	public static boolean isExcel2003(String filePath) {
		return filePath.matches("^.+\\.(?i)(xls)$");
	}

	// @描述：是否是2007的excel，返回true是2007
	public static boolean isExcel2007(String filePath) {
		return filePath.matches("^.+\\.(?i)(xlsx)$");
	}

}
