package org.fouram.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 导出Excel工具类
 * 
 * @author Liup
 */
public class ExcelExportUtil {

	private static XSSFCellStyle getTitleStyle(XSSFWorkbook workbook) {
		// 设置表格标题栏的样式
		XSSFCellStyle titleStyle = workbook.createCellStyle();
//		titleStyle.setFillBackgroundColor(color);
		 titleStyle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
		titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		titleStyle.setBorderBottom(BorderStyle.THIN);
		titleStyle.setBorderLeft(BorderStyle.THIN);
		titleStyle.setBorderRight(BorderStyle.THIN);
		titleStyle.setBorderTop(BorderStyle.THIN);
		titleStyle.setAlignment(HorizontalAlignment.CENTER);
		titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		// 设置标题栏字体
		XSSFFont titleFont = workbook.createFont();
		// titleFont.setColor(HSSFColor.WHITE.index);
		titleFont.setFontHeightInPoints((short) 12);
		// titleFont.setBold(true);
		// 把字体应用到当前的样式
		titleStyle.setFont(titleFont);
		return titleStyle;
	}

	private static XSSFCellStyle getStyle(XSSFWorkbook workbook) {
		// 生成表格中非标题栏的样式
		XSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.WHITE.index);// 背景色
		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		style.setBorderBottom(BorderStyle.THIN);
		style.setBorderLeft(BorderStyle.THIN);
		style.setBorderRight(BorderStyle.THIN);
		style.setBorderTop(BorderStyle.THIN);
		style.setAlignment(HorizontalAlignment.CENTER);
		// 生成表格中非标题栏的字体
		XSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.BLACK.index);
		font.setFontHeightInPoints((short) 12);
		// font.setBold(true);
		// 把字体应用到当前的样式
		style.setFont(font);
		return style;
	}

	public static void exportExcelFile(String sheetName, List<Map<String, Object>> dataList, String[] headers,
			String filePath) {

		// 声明一个工作薄
		XSSFWorkbook workbook = new XSSFWorkbook();
		// 生成一个表格
		XSSFSheet sheet = workbook.createSheet(sheetName);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);

		XSSFCellStyle style = getStyle(workbook);
		XSSFCellStyle titleStyle = getTitleStyle(workbook);

		// 产生表格标题行
		XSSFRow row = sheet.createRow(0);
		for (short i = 0; i < headers.length; i++) {
			XSSFCell cell = row.createCell(i);
			cell.setCellStyle(titleStyle);
			XSSFRichTextString text = new XSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}

		// 遍历集合数据，产生数据行
		Iterator<Map<String, Object>> it = dataList.iterator();
		int index = 0;
		while (it.hasNext()) {
			index++;
			row = sheet.createRow(index);
			Map<String, Object> data = it.next();
			int i = 0;
			for (String key : data.keySet()) {
				XSSFCell cell = row.createCell(i);
				cell.setCellStyle(style);
				XSSFRichTextString text = new XSSFRichTextString(Tools.formatString(data.get(key)));
				cell.setCellValue(text);
				i++;
			}
		}
		OutputStream out = null;
		try {
			out = new FileOutputStream(filePath);
			workbook.write(out);
		} catch (IOException e) {
			LoggerUtil.error(e);
		} finally {
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
		}
	}

	public static void exportExcelFile(String haveFilePath, List<Map<String, Object>> dataList, String filePath,
			int index) {
		// 声明一个工作薄
		File f = new File(haveFilePath);
		XSSFWorkbook workbook = null;
		try {
			FileInputStream fis = new FileInputStream(f);
			workbook = new XSSFWorkbook(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 生成一个表格
		XSSFSheet sheet = workbook.getSheetAt(0);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);

		XSSFCellStyle style = getStyle(workbook);

		// 遍历集合数据，产生数据行
		Iterator<Map<String, Object>> it = dataList.iterator();
		XSSFRow row = null;
		while (it.hasNext()) {
			row = sheet.createRow(index);
			Map<String, Object> data = it.next();
			int i = 0;
			for (String key : data.keySet()) {
				XSSFCell cell = row.createCell(i);
				cell.setCellStyle(style);
				XSSFRichTextString text = new XSSFRichTextString(Tools.formatString(data.get(key)));
				cell.setCellValue(text);
				i++;
			}
			index++;
		}
		OutputStream out = null;
		try {
			out = new FileOutputStream(filePath);
			workbook.write(out);
		} catch (IOException e) {
			LoggerUtil.error(e);
		} finally {
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
		}
	}

	public static void exportExcelStream(String sheetName, List<Map<String, Object>> dataList, String[] headers,
			HttpServletResponse response) {
		// 声明一个工作薄
		XSSFWorkbook workbook = new XSSFWorkbook();
		// 生成一个表格
		XSSFSheet sheet = workbook.createSheet(sheetName);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);

		XSSFCellStyle style = getStyle(workbook);
		XSSFCellStyle titleStyle = getTitleStyle(workbook);

		// 产生表格标题行
		XSSFRow row = sheet.createRow(0);
		for (short i = 0; i < headers.length; i++) {
			XSSFCell cell = row.createCell(i);
			cell.setCellStyle(titleStyle);
			XSSFRichTextString text = new XSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}

		// 遍历集合数据，产生数据行
		Iterator<Map<String, Object>> it = dataList.iterator();
		int index = 0;
		while (it.hasNext()) {
			index++;
			row = sheet.createRow(index);
			Map<String, Object> data = it.next();
			int i = 0;
			for (String key : data.keySet()) {
				XSSFCell cell = row.createCell(i);
				cell.setCellStyle(style);
				XSSFRichTextString text = new XSSFRichTextString(Tools.formatString(data.get(key)));
				cell.setCellValue(text);
				i++;
			}
		}
		OutputStream os = null;
		try {
			// web浏览通过MIME类型判断文件是excel类型
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			// Content-disposition属性设置成以附件方式进行下载
			response.setHeader("Content-disposition", "attachment;filename=export" + DateUtil.getTimeStamp() + ".xlsx");
			os = response.getOutputStream();
			workbook.write(os);
			os.flush();
		} catch (IOException e) {
			LoggerUtil.error(e);
		} finally {
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
		}
	}

	public static void exportExcelStream(String haveFilePath, List<Map<String, Object>> dataList, int index,
			HttpServletResponse response) {
		// 声明一个工作薄
		File f = new File(haveFilePath);
		XSSFWorkbook workbook = null;
		try {
			FileInputStream fis = new FileInputStream(f);
			workbook = new XSSFWorkbook(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 生成一个表格
		XSSFSheet sheet = workbook.getSheetAt(0);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);

		XSSFCellStyle style = getStyle(workbook);

		// 遍历集合数据，产生数据行
		Iterator<Map<String, Object>> it = dataList.iterator();
		XSSFRow row = null;
		while (it.hasNext()) {
			row = sheet.createRow(index);
			Map<String, Object> data = it.next();
			int i = 0;
			for (String key : data.keySet()) {
				XSSFCell cell = row.createCell(i);
				cell.setCellStyle(style);
				XSSFRichTextString text = new XSSFRichTextString(Tools.formatString(data.get(key)));
				cell.setCellValue(text);
				i++;
			}
			index++;
		}
		OutputStream os = null;
		try {
			// web浏览通过MIME类型判断文件是excel类型
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setCharacterEncoding("utf-8");
			// Content-disposition属性设置成以附件方式进行下载
			response.setHeader("Content-disposition", "attachment;filename=export" + DateUtil.getTimeStamp() + ".xlsx");
			os = response.getOutputStream();
			workbook.write(os);
			os.flush();
		} catch (IOException e) {
			LoggerUtil.error(e);
		} finally {
			if (workbook != null) {
				try {
					workbook.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					LoggerUtil.error(e);
				}
			}
		}
	}
}