package co.dc.aptiv.common;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class XLSService {

	private static Log log = LogFactory.getLog(XLSService.class);
	// 设置cell编码解决中文高位字节截断
	// private static short XLS_ENCODING = HSSFWorkbook.ENCODING_UTF_16;

	// 定制日期格式
	private static String DATE_FORMAT = "yyyy/mm/dd hh:mm:ss"; // "m/d/yy h:mm"

	// 定制浮点数格式
	private static String NUMBER_FORMAT = "#,##0.00";

	private String xlsFileName;

	public HSSFWorkbook workbook;

	public HSSFSheet sheet0;
	
	public HSSFSheet sheet1;

	private HSSFRow row;

	private HSSFCellStyle doubleStyle;

	/**
	 * 初始化Excel
	 * 
	 * @param fileName
	 *            导出文件名
	 */
	public XLSService(String fileName) {
		this.xlsFileName = fileName;
		this.workbook = new HSSFWorkbook();
		this.sheet0 = workbook.createSheet("已结算");
		this.sheet1 = workbook.createSheet("全部");
		this.sheet0.setDefaultColumnWidth(20);
		this.sheet1.setDefaultColumnWidth(20);
		this.doubleStyle = workbook.createCellStyle();
	}
	
	/**
	 * 导出Excel文件
	 * 
	 * @throws Exception
	 */
	public void exportXLS() throws Exception{
		FileOutputStream fOut = new FileOutputStream(xlsFileName);
		workbook.write(fOut);
		fOut.flush();
		fOut.close();
	}

	/**
	 * 增加一行
	 * 
	 * @param index
	 *            行号
	 */
	public void createRow(int sheetIndex , int index) {
		if(sheetIndex == 0 ){
			this.row = this.sheet0.createRow(index);
		}else{
			this.row = this.sheet1.createRow(index);			
		}
	}

	/**
	 * 设置单元格
	 * 
	 * @param index
	 *            列号
	 * @param value
	 *            单元格填充值
	 */
	public void setStringCell(int index, String value) {
		HSSFCell cell = this.row.createCell((short) index);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		// cell.setEncoding(XLS_ENCODING);
		cell.setCellValue(value);
	}

	/**
	 * 设置单元格
	 * 
	 * @param index
	 *            列号
	 * @param value
	 *            单元格填充值
	 */
	public void setCalendarCell(int index, Calendar value) {
		HSSFCell cell = this.row.createCell((short) index);
		// cell.setEncoding(XLS_ENCODING);
		cell.setCellValue(value.getTime());
		HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
		cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
	}

	/**
	 * 设置单元格
	 * 
	 * @param index
	 *            列号
	 * @param value
	 *            单元格填充值
	 */
	public void setIntCell(int index, int value) {
		HSSFCell cell = this.row.createCell((short) index);
		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(value);
	}

	/**
	 * 设置单元格
	 * 
	 * @param index
	 *            列号
	 * @param value
	 *            单元格填充值
	 */
	public void setDoubleCell(int index, double value) {
		HSSFCell cell = this.row.createCell((short) index);
		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(value);
		HSSFDataFormat format = workbook.createDataFormat();
		// 设置cell样式为定制的浮点数格式
		doubleStyle.setDataFormat(format.getFormat(NUMBER_FORMAT));
		// 设置该cell浮点数的显示格式
		cell.setCellStyle(doubleStyle);
	}

	public HSSFWorkbook getWorkbook() {
		return workbook;
	}

	public void setWorkbook(HSSFWorkbook workbook) {
		this.workbook = workbook;
	}

	public HSSFSheet getSheet0() {
		return sheet0;
	}

	public void setSheet0(HSSFSheet sheet0) {
		this.sheet0 = sheet0;
	}

	public HSSFSheet getSheet1() {
		return sheet1;
	}

	public void setSheet1(HSSFSheet sheet1) {
		this.sheet1 = sheet1;
	}

	public HSSFCellStyle getDoubleStyle() {
		return doubleStyle;
	}

	public void setDoubleStyle(HSSFCellStyle doubleStyle) {
		this.doubleStyle = doubleStyle;
	}
}
