package com.sys.bus.excel.tool;

import com.jfinal.log.Log;
import com.jfinal.render.Render;
import com.jfinal.render.RenderException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;


public class ExcelRender extends Render {

    private final Log LOG = Log.getLog(getClass());
    private final static String CONTENT_TYPE = "application/msexcel;charset=" + getEncoding();
    private List<Object[]> data;
    private String[][] headers;
    private String[] sheetNames = new String[]{"Sheet"};
    private int cellWidth;
    private String[] columns = new String[]{};
    private int[] columnType = new int[]{};
    private String fileName = "excel";
    private int headerRow;

    public ExcelRender(List<Object[]> data,String[] columns,int[] columnType) {
        this.columns =columns;
        this.data = data;
        this.columnType = columnType;
    }

    /**
     *
     * @param data
     * @param columns
     * @param columnType  1字符串 2小数 3百分数
     * @return
     */
    public static ExcelRender me(List<Object[]> data,String[] columns,int[] columnType) {
        return new ExcelRender(data,columns,columnType);
    }

    @Override
    public void render() {
        response.reset();
       // response.setHeader("Content-Disposition", "attachment;Filename=" + this.fileName);
        // response.setContentType(CONTENT_TYPE);
        try {
            fileName = new String(fileName.getBytes("gbk"),"iso8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.addHeader("Content-Disposition", "attachment;filename="+fileName + ".xlsx");

        OutputStream os = null;
        try {
            os = response.getOutputStream();
            new ExportExcel(sheetNames[0],columns,columnType,data,os).export();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage(), e);
            throw new RenderException(e);
        } finally {
            try {
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                LOG.error(e.getMessage(), e);
                throw new RenderException(e);
            }
        }
    }

    public ExcelRender headers(String[]... headers) {
        this.headers = headers;
        return this;
    }

    public ExcelRender headerRow(int headerRow) {
        this.headerRow = headerRow;
        return this;
    }

    public ExcelRender sheetName(String... sheetName) {
        this.sheetNames = sheetName;
        return this;
    }

    public ExcelRender cellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
        return this;
    }

    public ExcelRender fileName(String fileName) {
        this.fileName = fileName;
        return this;
    }
}