package com.uas.report.jasperreports.engine.export;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import com.uas.report.util.StringUtils;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPrintText;
import net.sf.jasperreports.engine.export.Cut;
import net.sf.jasperreports.engine.export.JRExporterGridCell;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.XlsRowLevelInfo;
import net.sf.jasperreports.engine.export.data.NumberTextValue;
import net.sf.jasperreports.engine.export.data.StringTextValue;
import net.sf.jasperreports.engine.export.data.TextValue;
import net.sf.jasperreports.engine.util.JRStyledText;

/**
 * 报表导出为Xls
 * 
 * @author sunyj
 * @since 2016年12月14日 下午8:10:56
 */
public class CustomJRXlsExporter extends JRXlsExporter {

	/**
	 * 单元格宽度
	 */
	public static final int CELL_WIDTH = 50;

	/**
	 * 单元格高度
	 */
	public static final int CELL_HEIGHT = 12;

	/**
	 * 单元格边框宽度
	 */
	public static final short CELL_BORDER_WIDTH = 1;

	/**
	 * 单元格边框颜色
	 */
	public static final short CELL_BORDER_CORLOR = IndexedColors.GREY_25_PERCENT.index;

	/**
	 * 是否自定义单元格格式
	 */
	private boolean customCellStyle = false;

	public CustomJRXlsExporter(boolean ifCustomCellStyle) {
		this.customCellStyle = ifCustomCellStyle;
	}

	@Override
	protected void setColumnWidth(int col, int width, boolean autoFit) {
		if (customCellStyle) {
			width = CELL_WIDTH;
		}
		super.setColumnWidth(col, width, autoFit);
	}

	@Override
	protected void setRowHeight(int rowIndex, int lastRowHeight, Cut yCut, XlsRowLevelInfo levelInfo) {
		if (customCellStyle) {
			lastRowHeight = CELL_HEIGHT;
		}
		super.setRowHeight(rowIndex, lastRowHeight, yCut, levelInfo);
	}

	@Override
	protected HSSFCellStyle getLoadedCellStyle(StyleInfo style) {
		HSSFCellStyle cellStyle = super.getLoadedCellStyle(style);
		if (customCellStyle) {
			cellStyle.setBorderTop(CELL_BORDER_WIDTH);
			cellStyle.setBorderLeft(CELL_BORDER_WIDTH);
			cellStyle.setBorderBottom(CELL_BORDER_WIDTH);
			cellStyle.setBorderRight(CELL_BORDER_WIDTH);
			cellStyle.setTopBorderColor(CELL_BORDER_CORLOR);
			cellStyle.setLeftBorderColor(CELL_BORDER_CORLOR);
			cellStyle.setBottomBorderColor(CELL_BORDER_CORLOR);
			cellStyle.setRightBorderColor(CELL_BORDER_CORLOR);
		}
		return cellStyle;
	}

	@Override
	protected void createTextCell(JRPrintText textElement, JRExporterGridCell gridCell, int colIndex, int rowIndex,
			JRStyledText styledText, StyleInfo baseStyle, short forecolor) throws JRException {
		if (customCellStyle) {
			String textStr = styledText.getText();
			TextValue textValue = getTextValue(textElement, textStr);
			// 类型为String，但是实际值为数值，需设置为字符串类型，否则会提示转为数值型
			if ((textValue instanceof StringTextValue && isNumber(textStr)) || textValue instanceof NumberTextValue) {
				baseStyle.setDataFormat(dataFormat.getFormat("@"));
			}
			super.createTextCell(textElement, gridCell, colIndex, rowIndex, styledText, baseStyle, forecolor);

			// 数值类型需明确指定，否则会提示转为数值型
			if (textValue instanceof NumberTextValue) {
				setNumberTextValue(textStr);
			}
		} else {
			super.createTextCell(textElement, gridCell, colIndex, rowIndex, styledText, baseStyle, forecolor);
		}
	}

	/**
	 * 判断字符串是否为数值
	 * 
	 * @param textStr
	 * @return
	 */
	private boolean isNumber(String textStr) {
		if (StringUtils.isEmpty(textStr)) {
			return false;
		}
		try {
			// 数值可能为1,320.6的形式
			Double.parseDouble(textStr.replaceAll(",", ""));
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 设置单元格格式为数值类型
	 * 
	 * @param textStr
	 */
	private void setNumberTextValue(String textStr) {
		if (isNumber(textStr)) {
			cell.setCellValue(Double.parseDouble(textStr.replaceAll(",", "")));
			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		}
	}
}
