package cn.com.origin.autocodemanager.common.operations;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.Pattern;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * @author wangjj Excel读取类
 * 
 */
public class ExcelOperation {

	public ExcelOperation() {

	}

	public static ArrayList<HashMap<String, Object>> readExcel(File fileExcel, int startRow, String headTiles[]) throws BiffException, IOException {
		ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
		if (fileExcel != null) {
			Workbook workBook = Workbook.getWorkbook(fileExcel);
			Sheet sheet = workBook.getSheet(0);
			if (headTiles != null && headTiles.length > 0) {
				for (int i = startRow; i < sheet.getRows(); i++) {
					HashMap<String, Object> hashMap = new HashMap<String, Object>();
					for (int k = 0; k < headTiles.length; k++) {
//						 System.out.println(headTiles + "[" + k + "]=" + headTiles[k]);
						if (sheet.findCell(headTiles[k]) != null) {
							Cell cell = sheet.getCell(sheet.findCell(headTiles[k]).getColumn(), i);
//							System.out.println("cell================" + cell);
							if (cell != null) {
								hashMap.put(headTiles[k], cell.getContents());
							}
						}
					}
					// System.out.println(hashMap);
					arrayList.add(hashMap);
				}
			}
			workBook.close();
		} else {
			// System.out.println("EXCEL文件为空");
		}
		return arrayList;
	}

	public static void writeExcel(String sheettile, File fileExcel, Object[] cloumnNames, ArrayList arrayList) throws IOException {
		WritableWorkbook wbook = Workbook.createWorkbook(fileExcel);
		wbook.createSheet(sheettile, 0);
		WritableSheet writableSheet = wbook.getSheet(0);
		Object[] titles = cloumnNames;
		WritableFont font = new WritableFont(WritableFont.COURIER, 9, WritableFont.BOLD, false, UnderlineStyle.SINGLE_ACCOUNTING, jxl.format.Colour.BLUE);
		WritableCellFormat wcfFC = new WritableCellFormat(font); // 用于设置单元格的样式,包括字体和颜色等.
		try {
			// wcfFC.setBackground(Colour.CORAL, Pattern.SOLID); // 设置单元格颜色
			wcfFC.setBackground(Colour.WHITE, Pattern.SOLID); // 设置
			wcfFC.setAlignment(Alignment.CENTRE);
			for (int i = 0; i < titles.length; i++) {
				Label l = new Label(i, 0, (String) titles[i]);
				l.setCellFormat(wcfFC); // 设置单元格的样式/格式
				writableSheet.setColumnView(i, 12);
				writableSheet.addCell(l);
			}
			if (arrayList != null && arrayList.size() > 0) {
				for (int n = 0; n < arrayList.size(); n++) {
					HashMap<?, ?> hashMap = (HashMap<?, ?>) arrayList.get(n);
					if (cloumnNames != null && cloumnNames.length > 0) {
						for (int k = 0; k < titles.length; k++) {
							String str = (String) hashMap.get(titles[k]);
							Label labelValue = new Label(k, n + 1, str);
							writableSheet.addCell(labelValue);
						}
					}
				}
			}
			wbook.write(); // 将数据写入excel文件.
			wbook.close();
		} catch (WriteException e) {
			e.printStackTrace();
		}

		if (fileExcel.exists()) {
			openFile(fileExcel.getAbsolutePath());
		}
	}

	public static void openFile(String targetFile) {
		File file = new File(targetFile);
		if (file.exists()) {
			Boolean value = false;
			value = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "INFO", "文件已经生成，是否打开？");
			if (value) {
				try {
					Runtime runtime = Runtime.getRuntime();
					runtime.exec("cmd /c  start exit &&\"" + targetFile + "\" ");
					// String as1[] = { "cmd.exe", "/c", "start", targetFile };
					// runtime.exec(as1);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			if (value.contains("."))
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	public static boolean isNumber(String value) {
		return isInteger(value) || isDouble(value);
	}

}
