package com.ruoyi.common.importExcel.excel;

import org.apache.poi.hssf.usermodel.*;
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 java.io.IOException;
import java.util.List;
import java.util.Map;

public class ExcelExportUtils {



    /**
     * 创建只有一个表头的excel
     * @param title excel文件名
     * @param columnWithMap 列-宽  map
     * @param tableTitle table表头
     * @param data 数据
     * @return
     * @throws IOException
     */
    public static HSSFWorkbook createExcel(String title, Map<Integer, Integer> columnWithMap, List<String> tableTitle, List<List<String>> data) {
        // 创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 建立新的sheet对象（excel的表单）
        HSSFSheet sheet = workbook.createSheet(title);
        // 设置列宽
        columnWithMap.forEach((key,value)->{
            sheet.setColumnWidth(key,value*256);
        });

        // 正在操作的row
        int rowNum = 0;

        // 航班信息，table名称，总额 的样式
        HSSFCellStyle cellStyleTotal = workbook.createCellStyle();
        //字体
        HSSFFont fontTotal = workbook.createFont();
        //字体颜色
        fontTotal.setColor(Font.COLOR_NORMAL);
        fontTotal.setFontName("宋体");
        //加粗
        fontTotal.setBold(true);
        cellStyleTotal.setFont(fontTotal);
        cellStyleTotal.setAlignment(HorizontalAlignment.CENTER);

        // 普通样式
        HSSFCellStyle cellStyleCommon = workbook.createCellStyle();
        //字体
        HSSFFont fontCommon = workbook.createFont();
        //字体颜色
        fontCommon.setColor(Font.COLOR_NORMAL);
        fontCommon.setFontName("宋体");
        cellStyleCommon.setFont(fontCommon);
        cellStyleCommon.setAlignment(HorizontalAlignment.CENTER);

        // 表头样式
        HSSFCellStyle cellStyleTableTitle = workbook.createCellStyle();
        //字体
        HSSFFont fontTableTitle = workbook.createFont();
        //字体颜色
        fontTableTitle.setColor(Font.COLOR_NORMAL);
        fontTableTitle.setFontName("宋体");
        cellStyleTableTitle.setFont(fontCommon);
        cellStyleTableTitle.setAlignment(HorizontalAlignment.CENTER);
        //背景色
        cellStyleTableTitle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        cellStyleTableTitle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 业务数据标题
        HSSFRow rowTitle = sheet.createRow(rowNum);
        for (int j = 0; j < tableTitle.size(); j++) {
            HSSFCell cell = rowTitle.createCell(j);
            cell.setCellValue(tableTitle.get(j));
            cell.setCellStyle(cellStyleTableTitle);
        }
        rowNum++;

        if (data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                HSSFRow rowData = sheet.createRow(rowNum);
                if (data.get(i).size() > 0) {
                    for (int j = 0; j < data.get(i).size(); j++) {
                        HSSFCell cell = rowData.createCell(j);
                        cell.setCellValue(data.get(i).get(j));
                        cell.setCellStyle(cellStyleCommon);
                    }
                }
                rowNum++;
            }
        }

        return workbook;
    }
}
