package cn.com.qmth.examcloud.core.questions.base.excel;

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

import cn.com.qmth.examcloud.core.questions.base.excel.ExcelWriter;

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


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

    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) {
        try {

            response.setHeader("Content-Disposition", "inline; filename="
                    + URLEncoder.encode(fileName, "UTF-8") + DEFALUT_EXT);
            response.setContentType(DEFALUT_CONTENT_TYPE);
            ServletOutputStream outputStream = response.getOutputStream();
            ExcelWriter excelExporter = new ExcelWriter(dataClass);
            excelExporter.write("sheet1", dataset, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
