package co.dc.aptiv.utils;

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

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;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

public class XLSUtil {

	private static Log log = LogFactory.getLog(XLSUtil.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;

	/**
	 * 初始化Excel
	 * 
	 * @param fileName
	 *            导出文件名
	 */
	public XLSUtil(String fileName) {
		this.xlsFileName = fileName;
		this.workbook = new HSSFWorkbook();
		this.sheet0 = workbook.createSheet("装箱单");
		// this.sheet1 = workbook.createSheet("当日统计");
		this.sheet0.setDefaultColumnWidth(10);
		// this.sheet1.setDefaultColumnWidth(12);
	}

	/**
	 * 导出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);
		HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
		HSSFDataFormat format = workbook.createDataFormat();
		cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
		cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
	}

	// 创建一个居中的cell
	public void setCell(int index, String value) {
		HSSFCell cell = this.row.createCell((short) index);
		cell.setCellValue(value);
		HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cell.setCellStyle(cellStyle);
	}

	/**
	 * 添加合并单元格
	 * 
	 * @param firstRow
	 *            从第几行开始 从0计数
	 * @param lastRow
	 *            到第几行结束
	 * @param firstCol
	 *            从第几列开始 从0计数
	 * @param lastCol
	 *            到第几列结束
	 * @param isSetBorder
	 *            是否设置边框
	 * @param value
	 *            值
	 */
	public void addMergedRegionAndSetBorder(int firstRow, int lastRow,
			int firstCol, int lastCol, boolean isSetBorder, String value,
			HSSFCellStyle cellStyle) {
		CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow,lastRow, firstCol, lastCol);
		if (isSetBorder) {
			RegionUtil.setBorderBottom(1, cellRangeAddress, this.sheet0,
					this.workbook);
			RegionUtil.setBorderTop(1, cellRangeAddress, this.sheet0,
					this.workbook);
			RegionUtil.setBorderLeft(1, cellRangeAddress, this.sheet0,
					this.workbook);
			RegionUtil.setBorderRight(1, cellRangeAddress, this.sheet0,
					this.workbook);
		}
		this.sheet0.addMergedRegion(cellRangeAddress);
		HSSFCell cell = this.sheet0.getRow(cellRangeAddress.getFirstRow())
				.getCell(cellRangeAddress.getFirstColumn());
		cell.setCellStyle(cellStyle);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(value);
	}

	/**
	 * 添加合并单元格
	 * 
	 * @param firstRow
	 *            从第几行开始 从0计数
	 * @param lastRow
	 *            到第几行结束
	 * @param firstCol
	 *            从第几列开始 从0计数
	 * @param lastCol
	 *            到第几列结束
	 * @param isSetBorder
	 *            是否设置边框
	 */
	public void addMergedRegionAndSetBorder(int firstRow, int lastRow,
			int firstCol, int lastCol, boolean isSetBorder) {
		CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow,
				lastRow, firstCol, lastCol);
		if (isSetBorder) {
			RegionUtil.setBorderBottom(1, cellRangeAddress, this.sheet0,
					this.workbook);
			RegionUtil.setBorderTop(1, cellRangeAddress, this.sheet0,
					this.workbook);
			RegionUtil.setBorderLeft(1, cellRangeAddress, this.sheet0,
					this.workbook);
			RegionUtil.setBorderRight(1, cellRangeAddress, this.sheet0,
					this.workbook);
		}
		this.sheet0.addMergedRegion(cellRangeAddress);
	}
	
	/**
	 * 设置列宽
	 * @param columnIndex 列
	 * @param width 宽度
	 */
	public void setColumnWidth(int columnIndex, int width){
		this.sheet0.setColumnWidth(columnIndex, width);
	}
	/**
	 * 设置行高
	 * @param rowIndex 行
	 * @param height 高度
	 */
	public void setRowHeight(int rowIndex,short height){
		this.sheet0.getRow(rowIndex).setHeight(height);
	}

}
