import java.io.File;
import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil {
	
	* @Title: getWorkBook
	* @Description: 获取Excel文件对象
	* @param @param f
	* @param @return    
	* @return Workbook    
	* @throws
	 */
	public Workbook getWorkBook(File f) {
		Workbook wb = null;
			
		try (FileInputStream fis = new FileInputStream(f)) {
			
			if (f.getName().indexOf(".xlsx") > 1) {
				wb = new XSSFWorkbook(fis);
			} else {
				wb = new HSSFWorkbook(fis);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return wb;
	}
	
	
	* @Title: readExcel
	* @Description: 读取Excel文件
	* @param @param wb
	* @param @param sheetIndex		sheet页下标,从 0 开始
	* @param @param startReadLine	开始读取的行,从 0 开始
	* @param @param tailLine    	最后读取的行号
	* @return void    
	* @throws
	 */
	public void readExcel(Workbook wb, int sheetIndex, int startReadLine, int tailLine) {
		Sheet sheet = wb.getSheetAt(sheetIndex);
		Row row = null;
		
		for (int i = startReadLine; i < sheet.getLastRowNum() - tailLine + 1; i++) {
			row = sheet.getRow(i);
			
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				
			}
		}
	}
	
	
	* @Title: getMergedRegionValue
	* @Description: 获取合并单元格的值
	* @param @param sheet
	* @param @param row
	* @param @param column
	* @param @return    
	* @return String    
	* @throws
	 */
	public String getMergedRegionValue(Sheet sheet, int row, int column) {
		
		int sheetMergeCount = sheet.getNumMergedRegions();
		
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			
			if (row >= firstRow && row <= lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					Row fRow = sheet.getRow(firstRow);
					Cell fCell = fRow.getCell(firstColumn);
					return getCellValue(fCell);
				}
			}
		}
		
		return null;
	}
	
	
	* @Title: isMergeRow
	* @Description: 判断合并了行
	* @param @param sheet
	* @param @param row
	* @param @param column
	* @param @return    
	* @return boolean    
	* @throws
	 */
	public boolean isMergeRow(Sheet sheet, int row, int column) {
		
		int sheetMergeCount = sheet.getNumMergedRegions();
		
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			
			if (row == firstRow && row == lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					return true;
				}
			}
		}
		
		return false;
	}
	
	
	* @Title: isMergedRegion
	* @Description: 判断指定的单元格是否是合并单元格
	* @param @param sheet
	* @param @param row
	* @param @param column
	* @param @return    
	* @return boolean    
	* @throws
	 */
	public boolean isMergedRegion(Sheet sheet, int row, int column) {
		int sheetMergeCount = sheet.getNumMergedRegions();
		for (int i = 0; i < sheetMergeCount; i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			
			if (row >= firstRow && row <= lastRow) {
				if (column >= firstColumn && column <= lastColumn) {
					return true;
				}
			}
		}
		
		return false;
	}
	
	
	* @Title: hasMerged
	* @Description: 判断 Sheet 页中是否含有合并单元格
	* @param @param sheet
	* @param @return    
	* @return boolean    
	* @throws
	 */
	public boolean hasMerged(Sheet sheet) {
		return sheet.getNumMergedRegions() > 0 ? true : false;
	}
	
	
	* @Title: mergeRegion
	* @Description: 合并单元格
	* @param @param sheet
	* @param @param firstRow	开始行
	* @param @param lastRow		结束行
	* @param @param firstCol	开始列
	* @param @param lastCol 	 结束列
	* @return void    
	* @throws
	 */
	public void mergeRegion(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {  
        sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));  
    }
	
	
	* @Title: getCellValue
	* @Description: 获取单元格的值
	* @param @param cell
	* @param @return    
	* @return String    
	* @throws
	 */
	public String getCellValue(Cell cell) {
		if (cell == null) {
			return null;
		}
		
		
		
		if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
			return "";
		}
		
		if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
			return cell.getStringCellValue();
		}
		
		if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(cell.getBooleanCellValue());
		}
		
		if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
			
			返回的是 double 型,利用 HSSFDateUtil.isCellDateFormatted() 可判断是否是日期类型,但
			日期格式只能处理yyyy-MM-dd, d/m/yyyy h:mm, HH:mm 等不含中文文字的日期格式,如需识别中文,
			只能根据样式来判断,以下是Excel中所有自定义的中文样式,代码if语句中也已添加所有相关中文日期,嫌麻烦可以看文末 */
			
				yyyy年m月d日---31
				yyyy年m月------57
				m月d日---------58
				h"时"mm"分"--32
				h"时"mm"分"ss"秒"--33
				上午/下午h"时"mm"分"--55
				上午/下午h"时"mm"分"ss"秒"--56
			*/
			if (HSSFDateUtil.isCellDateFormatted(cell) || cell.getCellStyle().getDataFormat() == 31 || 
					cell.getCellStyle().getDataFormat() == 32 || cell.getCellStyle().getDataFormat() == 33 || 
					cell.getCellStyle().getDataFormat() == 55 || cell.getCellStyle().getDataFormat() == 56 || 
					cell.getCellStyle().getDataFormat() == 57 || cell.getCellStyle().getDataFormat() == 58) {
				DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				return df.format(cell.getDateCellValue());
			} else {
				cell.setCellType(Cell.CELL_TYPE_STRING);
				return cell.getStringCellValue();
			}
		}
		return null;
	}
	
}