package com.qmth.themis.business.excel;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Collection;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.qmth.themis.common.contanst.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * excel导出工具
 */
public class ExportUtils {

	private static final Logger log = LoggerFactory.getLogger(ExportUtils.class);
	
    private static final String DEFALUT_CONTENT_TYPE = "application/vnd.ms-excel";

    private static final String DEFALUT_EXT = ".xlsx";

    public static void exportEXCEL(String fileName,Class<?> dataClass,
                             Collection<?> dataset,HttpServletResponse response) throws Exception {
    	log.debug("导出Excel开始...");
        response.setHeader("Content-Disposition","inline;filename="+URLEncoder.encode(fileName, Constants.CHARSET_NAME)+DEFALUT_EXT);
        response.setContentType(DEFALUT_CONTENT_TYPE);
        ServletOutputStream outputStream = response.getOutputStream();
    	
        ExcelWriter excelExporter = new ExcelWriter(dataClass,"sheet1");
        excelExporter.write(dataset,outputStream);
        outputStream.flush();
        outputStream.close();
        log.debug("导出Excel结束");
    }
    
    public static void makeExcel(Class<?> dataClass,Collection<?> dataset,OutputStream outputStream ) throws Exception {
		log.debug("生成Excel开始...");
		ExcelWriter excelExporter = new ExcelWriter(dataClass,"sheet1");
		excelExporter.write(dataset,outputStream);
		log.debug("生成Excel结束");
	}
}
