package cn.com.ctop.common.module.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
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;
import org.jeecg.common.util.DateUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ExportExcelUtils {


    /**
     * @param workbook
     * @param sheetNum   (sheet的位置，0表示第一个表格中的第一个sheet)
     * @param sheetTitle （sheet的名称）
     * @param headers    （表格的标题）
     * @param result     （表格的数据）
     * @return
     * @throws Exception
     * @Title: exportExcel
     * @Description: 导出Excel的方法
     */

    public void exportExcel(XSSFWorkbook workbook, int sheetNum,
                            String sheetTitle, String[] headers, List<List<Object>> result) {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.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);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);

        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);

        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            cell.setCellStyle(style);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (result != null) {
            int index = 1;
            for (List<Object> m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                for (Object obj : m) {
                    XSSFCell cell = row.createCell((short) cellIndex);
                    if (!Check.isNull(obj)) {
                        if (obj instanceof String) {
                            cell.setCellValue((String) obj);
                        } else if (obj instanceof Double) {
                            cell.setCellValue((Double) obj);
                        } else if (obj instanceof BigDecimal) {
                            BigDecimal bigDecimal = (BigDecimal) obj;
                            cell.setCellValue(bigDecimal.doubleValue());
                        } else if (obj instanceof Long) {
                            cell.setCellValue(String.valueOf(obj));
                        } else {
                            cell.setCellValue(obj.toString());
                        }
                    } else {
                        cell.setCellValue(0);
                    }

                    cellIndex++;
                }
                index++;
            }
        }
    }

    public void exportExcelBudget(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<Object>> result) {
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.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);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);

        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);

        // 绿色字体
        CellStyle greenStyle = workbook.createCellStyle();
        Font greenFont = workbook.createFont();
        greenFont.setColor(IndexedColors.GREEN.getIndex());
        greenStyle.setFont(greenFont);

        // 红色字体
        CellStyle redStyle = workbook.createCellStyle();
        Font redFont = workbook.createFont();
        redFont.setColor(IndexedColors.RED.getIndex());
        redStyle.setFont(redFont);


        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            cell.setCellStyle(style);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (result != null) {
            int index = 1;
            for (List<Object> m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                for (Object obj : m) {
                    XSSFCell cell = row.createCell((short) cellIndex);
                    if (!Check.isNull(obj)) {
                        String s = String.valueOf(obj);
                        if (s.contains("&br;") || s.contains(":red") || s.contains(":green")) {
                            if (s.contains("&br;")) {
                                JSONObject data = getData(s);
                                if (!Check.isNull(data)) {
                                    cell.setCellValue(data.getString("data"));
                                    JSONArray dataArr = data.getJSONArray("array");
                                    if (!Check.isNull(dataArr)) {
                                        for (int i = 0; i < dataArr.size(); i++) {
                                            JSONObject dataJson = dataArr.getJSONObject(i);
                                            if (!Check.isNull(dataJson)) {
                                                String color = dataJson.getString("color");
                                                if (!Check.isNull(color)) {
                                                    if (color.equals("red")) {
                                                        cell.getRichStringCellValue().applyFont(dataJson.getInteger("start"), dataJson.getInteger("end"), redFont);
                                                    } else if (color.equals("green")) {
                                                        cell.getRichStringCellValue().applyFont(dataJson.getInteger("start"), dataJson.getInteger("end"), greenFont);
                                                    }
                                                }

                                            }

                                        }
                                    }
                                } else {
                                    cell.setCellValue(0);
                                }
                            } else {
                                if (s.contains(":red")) {
                                    String value = s.replace(":red", "");
                                    cell.setCellValue(value);
                                    int i = value.lastIndexOf("↑");
                                    cell.getRichStringCellValue().applyFont(i, value.length(), redFont);
                                } else if (s.contains(":green")) {
                                    String value = s.replace(":green", "");
                                    cell.setCellValue(value);
                                    int i = value.lastIndexOf("↓");
                                    cell.getRichStringCellValue().applyFont(i, value.length(), greenFont);
                                } else {
                                    cell.setCellValue(0);
                                }
                            }


                        } else {
                            cell.setCellValue(s);
                        }
                    } else {
                        cell.setCellValue(0);
                    }

                    cellIndex++;
                }
                index++;
            }
        }
    }

    public static void main(String[] args) {

        String a = null;

        System.err.println(a.length());
    }

    private static JSONObject getData(String str) {
        JSONObject returnJson = new JSONObject();
        JSONArray array = new JSONArray();
        String[] split = str.split("&br;");
        if (split.length == 1) {
            String data = split[0];
            JSONObject jsonObject = new JSONObject();
            if (data.contains(":hong")) {
                jsonObject.put("color", "red");
                data = data.replace(":hong", "");
                int startIndex = data.indexOf("↑");
                jsonObject.put("start", startIndex);
                jsonObject.put("end", data.length());
            } else if (data.contains(":lv")) {
                jsonObject.put("color", "green");
                data = data.replace(":lv", "");
                int startIndex = data.indexOf("↓");
                jsonObject.put("start", startIndex);
                jsonObject.put("end", data.length());
            }
            array.add(jsonObject);
            returnJson.put("data", data);
            returnJson.put("array", array);

        } else {
            String data = "";
            Integer dataLength = 0;
            for (int i = 0; i < split.length; i++) {
                String value = split[i];
                JSONObject valJson = new JSONObject();
                if (value.contains(":hong")) {
                    valJson.put("color", "red");
                    String newValue = value.replace(":hong", "");
                    int startIndex = newValue.indexOf("↑");
                    valJson.put("start", dataLength + startIndex);
                    valJson.put("end", dataLength + newValue.length());
                    data += newValue;
                    dataLength += newValue.length();

                } else if (value.contains(":lv")) {
                    valJson.put("color", "green");
                    String newValue = value.replace(":lv", "");
                    int startIndex = newValue.indexOf("↓");
                    valJson.put("start", dataLength + startIndex);
                    valJson.put("end", dataLength + newValue.length());
                    data += newValue;
                    dataLength += newValue.length();
                }
                array.add(valJson);

            }
            returnJson.put("array", array);
            returnJson.put("data", data);

        }

        return returnJson;
    }

    public void exportExcelForAttendance(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<Object>> result) {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        XSSFCellStyle style2 = workbook.createCellStyle();
        XSSFCellStyle style3 = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.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);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);

        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);
        //迟到时间超过15分钟的考勤记录样式(标黄)
        style2.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index);
        style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style2.setBorderBottom(BorderStyle.THIN);
        style2.setBorderLeft(BorderStyle.THIN);
        style2.setBorderRight(BorderStyle.THIN);
        style2.setBorderTop(BorderStyle.THIN);
        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            cell.setCellStyle(style);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (result != null) {
            int index = 1;
            for (List<Object> m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;
                //迟到时间超过15分钟的考勤记录判断
                boolean changeColor = false;
                if (m.contains("迟到")) {
                    String duration = String.valueOf(m.get(m.size() - 2));
                    if (!Check.isNull(duration) && Integer.valueOf(duration.replace(" min", "")) - 10 > 0) {
                        changeColor = true;
                    }
                }
                for (Object obj : m) {
                    XSSFCell cell = row.createCell((short) cellIndex);
                    if (!Check.isNull(obj)) {
                        if (changeColor) {
                            cell.setCellStyle(style2);
                        }
                        if (obj instanceof String) {
                            cell.setCellValue((String) obj);
                        } else if (obj instanceof Double) {
                            cell.setCellValue((Double) obj);
                        } else if (obj instanceof BigDecimal) {
                            BigDecimal bigDecimal = (BigDecimal) obj;
                            cell.setCellValue(bigDecimal.doubleValue());
                        } else if (obj instanceof Long) {
                            cell.setCellValue(String.valueOf(obj));
                        } else {
                            cell.setCellValue(obj.toString());
                        }
                    }
                    cellIndex++;
                }
                index++;
            }
        }
    }

    public void exportExcelForFirstDelivery(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<Object>> result) {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.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);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);
        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);

        //有效率<媒体考核有效率 (标绿)
        XSSFCellStyle style2 = workbook.createCellStyle();
//        style2.setFillForegroundColor(IndexedColors.GREEN.getIndex()); // 设置前景颜色为绿色
//        style2.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置背景填充样式
        XSSFFont font2 = workbook.createFont();
        font2.setColor(IndexedColors.GREEN.index);// 设置字体颜色为绿色
        font2.setBold(true);
        style2.setFont(font2);

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

        // 遍历集合数据，产生数据行
        if (result != null) {
            int index = 1;
            for (List<Object> m : result) {

                row = sheet.createRow(index);
                int cellIndex = 0;
                for (Object obj : m) {
                    XSSFCell cell = row.createCell((short) cellIndex);
                    if (!Check.isNull(obj)) {
                        if (obj instanceof String) {
                            String parameter = (String) obj;
                            if (parameter.contains("-G")) {
                                String[] val = parameter.split("-");
                                if (parameter.contains("-GREEN")) {
                                    cell.setCellStyle(style2);
                                }
                                cell.setCellValue(val[0]);
                            } else {
                                cell.setCellValue(parameter);
                            }
                        } else if (obj instanceof Double) {
                            cell.setCellValue((Double) obj);
                        } else if (obj instanceof BigDecimal) {
                            BigDecimal bigDecimal = (BigDecimal) obj;
                            cell.setCellValue(bigDecimal.doubleValue());
                        } else if (obj instanceof Long) {
                            cell.setCellValue(String.valueOf(obj));
                        } else {
                            cell.setCellValue(obj.toString());
                        }
                    }
                    cellIndex++;
                }
                index++;
            }
        }
    }

    public void exportExcelForMediaReport(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<Object>> result) {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 20);
        sheet.setColumnWidth(0, 15 * 256);
        sheet.setColumnWidth(1, 40 * 256);
        sheet.setColumnWidth(2, 30 * 256);
        sheet.setColumnWidth(3, 35 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        sheet.setColumnWidth(6, 20 * 256);
        sheet.setColumnWidth(7, 20 * 256);
        sheet.setColumnWidth(8, 15 * 256);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.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);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);

        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);

        //同一行第一个cell
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setWrapText(true);

        // 产生表格标题行
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            cell.setCellStyle(style);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }
        // 遍历集合数据，产生数据行
        if (result != null) {
            int index = 1;
            for (List<Object> m : result) {
                row = sheet.createRow(index);
                int cellIndex = 0;

                for (Object obj : m) {
                    XSSFCell cell = row.createCell((short) cellIndex);
                    if (!Check.isNull(obj)) {
                        if (obj instanceof String) {
                            cell.setCellValue((String) obj);
                        } else if (obj instanceof Double) {
                            cell.setCellValue((Double) obj);
                        } else if (obj instanceof BigDecimal) {
                            BigDecimal bigDecimal = (BigDecimal) obj;
                            cell.setCellValue(bigDecimal.doubleValue());
                        } else if (obj instanceof Long) {
                            cell.setCellValue(String.valueOf(obj));
                        } else {
                            cell.setCellValue(obj.toString());
                        }
                    } else {
                        cell.setCellValue(0);
                    }
                    cell.setCellStyle(cellStyle);

                    cellIndex++;
                }
                index++;
            }
        }
    }

    //发送响应流方法
    public void putResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
//            response.setContentType("multipart/form-data;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String getCellValue(Cell cell) {
        String value = "";
        if (cell != null) {
            // 以下是判断数据的类型
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                    value = cell.getNumericCellValue() + "";
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        Date date = cell.getDateCellValue();
                        if (date != null) {
                            value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                        } else {
                            value = "";
                        }
                    } else {
                        value = new DecimalFormat("0").format(cell.getNumericCellValue());
                    }
                    break;
                case HSSFCell.CELL_TYPE_STRING: // 字符串
                    value = cell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                    value = cell.getBooleanCellValue() + "";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA: // 公式
                    value = cell.getCellFormula() + "";
                    break;
                case HSSFCell.CELL_TYPE_BLANK: // 空值
                    value = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR: // 故障
                    value = "非法字符";
                    break;
                default:
                    value = "未知类型";
                    break;
            }
        }
        return value.trim();
    }

    public String getCellValue2(Cell cell) {
        String value = "";
        if (cell != null) {
            // 以下是判断数据的类型
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                    value = cell.getNumericCellValue() + "";
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        Date date = cell.getDateCellValue();
                        if (date != null) {
                            if (date.getHours() > 0 || date.getMinutes() > 0) {
                                value = new SimpleDateFormat(DateUtils.NEW_FORMAT2).format(date);
                            } else {
                                value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                            }
                        } else {
                            value = "";
                        }
                    } else {
                        value = new DecimalFormat("0.0000").format(cell.getNumericCellValue()).replace(".0000", "");
                    }
                    break;
                case HSSFCell.CELL_TYPE_STRING: // 字符串
                    value = cell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                    value = cell.getBooleanCellValue() + "";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA: // 公式
                    value = cell.getCellFormula() + "";
                    break;
                case HSSFCell.CELL_TYPE_BLANK: // 空值
                    value = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR: // 故障
                    value = "非法字符";
                    break;
                default:
                    value = "未知类型";
                    break;
            }
        }
        return value.trim();
    }

    public void putHeaders(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers) {
        // 生成一个表格
        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        // 设置表格默认列宽度为20个字节
        sheet.setDefaultColumnWidth((short) 30);
        // 生成一个样式
        XSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(IndexedColors.PALE_BLUE.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);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        // 生成一个字体
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        //加粗
        font.setBold(true);
        // 把字体应用到当前的样式
        style.setFont(font);

        // 指定当单元格内容显示不下时自动换行
        style.setWrapText(true);
        //记录样式(标黄)
//        style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index);

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

}
