/**
 * Company Name : 中贸促信息技术有限责任公司	
 * Project Name:investCenterWebsite
 * File Name:ImportExcel.java
 * Package Name:ccpit.base.utils
 * Date:2016年7月12日下午4:31:01
 * Copyright (c) 2016, liyang@ccpit.org All Rights Reserved.
 *
 */
package ccpit.base.utils.excelUtil;

import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;  
import java.util.Date;
import java.util.List;


import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
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 ccpit.base.user.User;

/**
 * ClassName: ImportExcel <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年7月12日 下午4:31:01 <br/>
 *
 * @author liyang
 * @version 
 * @since JDK 1.6
 */
public class  ImportExcel<T> {
		/**
		 * Excel 2003
		 */
		private final static String XLS = "xls";
		/**
		 * Excel 2007
		 */
		private final static String XLSX = "xlsx";
		/**
		 * 分隔符
		 */
		private final static String SEPARATOR = "|";

		/**
		 * 由Excel文件的Sheet导出至List
		 * 
		 * @param file
		 * @param sheetNum
		 * @return
		 */
		public static<T> List<List<Object>> exportListFromExcel(File file, int sheetNum)
				throws IOException {
			return exportListFromExcel(new FileInputStream(file),
					FilenameUtils.getExtension(file.getName()), sheetNum);
		}

		/**
		 * 由Excel流的Sheet导出至List
		 * 
		 * @param is
		 * @param extensionName
		 * @param sheetNum
		 * @return
		 * @throws IOException
		 */
		public static <T> List<List<Object>> exportListFromExcel(InputStream is,
				String extensionName, int sheetNum) throws IOException {

			Workbook workbook = null;

			if (extensionName.toLowerCase().equals(XLS)) {
				workbook = new HSSFWorkbook(is);
			} else if (extensionName.toLowerCase().equals(XLSX)) {
				workbook = new XSSFWorkbook(is);
			}

			return exportListFromExcel(workbook, sheetNum);
		}

		/**
		 * 由指定的Sheet导出至List
		 * 
		 * @param workbook
		 * @param sheetNum
		 * @return
		 * @throws IOException
		 */
		private static <T> List<List<Object>> exportListFromExcel(Workbook workbook,
				int sheetNum) {

			Sheet sheet = workbook.getSheetAt(sheetNum);

			// 解析公式结果
			FormulaEvaluator evaluator = workbook.getCreationHelper()
					.createFormulaEvaluator();

			List<List<Object>> list = new ArrayList<List<Object>>();

			int minRowIx = sheet.getFirstRowNum();
			int maxRowIx = sheet.getLastRowNum();
			for (int rowIx = minRowIx+1; rowIx <= maxRowIx; rowIx++) {
				List<Object> lst2 = new ArrayList<Object>();
				Row row = sheet.getRow(rowIx);
				short minColIx = row.getFirstCellNum();
				short maxColIx = row.getLastCellNum();
				for (short colIx = minColIx; colIx < maxColIx; colIx++) {
					Cell cell = row.getCell(new Integer(colIx));
					CellValue cellValue = evaluator.evaluate(cell);
					if(cellValue!=null){
						lst2.add(cellValue.getStringValue());	
					}else{
						lst2.add("");
					}
				}
				list.add(lst2);
			}
			return list;
		}
		/**
		 * 由list生成批量的sql语句
		 * @param List<String>
		 * @param sheetNum
		 * @return
		 */
		public static <T> List bathInsertSql(List<List<Object>> lst,List<String> fields,T t1,User user){
			//声明最终返回对象
			List<T> finalResult = new ArrayList<T>();
			
			Class clazz = null;
			try{
			//获取对象
			for(int i=0,size=lst.size();i<size;i++){
				List<Object> values = lst.get(i);
				for(int j=0,size2=values.size();j<size2;j++){
					 clazz = Class.forName(t1.getClass().getName());
					//获取属性名
					String fieldName = fields.get(j);
					//获取属性
					Field field = clazz.getDeclaredField(fieldName);
					//获取方法名
					String method2 = "set"+toUpperCaseFirstOne(fieldName);
					//获取方法进行执行
					Method m1 = clazz.getMethod(method2, field.getType());
					if(values.get(j)!=null){
						m1.invoke(t1,values.get(j));
					}
				}
				//设置未删除
				Method m2 = clazz.getDeclaredMethod("setWhetherDelete", boolean.class);
				m2.invoke(t1, false);
				//设置索引
				 m2 = clazz.getDeclaredMethod("setInformationIndex", String.class);
				 m2.invoke(t1,"1");
				//设置点击量
				 m2 = clazz.getDeclaredMethod("setClickVolume", Integer.class);
				m2.invoke(t1, 0);
				//增加创建者 
				m2 = clazz.getDeclaredMethod("setCreator",User.class);
				m2.invoke(t1, user);
				//增加创建时间
				m2 = clazz.getDeclaredMethod("setCreateTime",Date.class);
				m2.invoke(t1, new Date());
				finalResult.add(t1);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
			return finalResult;
		}
		
		//首字母转小写
	    public static String toLowerCaseFirstOne(String s)
	    {
	        if(Character.isLowerCase(s.charAt(0)))
	            return s;
	        else
	            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
	    }
	    //首字母转大写
	    public static String toUpperCaseFirstOne(String s)
	    {
	        if(Character.isUpperCase(s.charAt(0)))
	            return s;
	        else
	            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	    }
}

