package com.nantian.cloud.dbcapp.tool;


import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.nantian.cloud.dbcapp.bean.EnterpriseDataInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ExcleTool {
    public void writeExcle(List<EnterpriseDataInfo> list) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("data.xls");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        HSSFWorkbook sheets = new HSSFWorkbook();
        HSSFSheet sheet = sheets.createSheet("8月");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("name");
        HSSFCell cell1 = row.createCell(1);
        cell1.setCellValue("code");
        HSSFCell cell2 = row.createCell(2);
        cell2.setCellValue("value");
        HSSFCell cell3 = row.createCell(3);
        cell3.setCellValue("statType");
        HSSFCell cell4 = row.createCell(4);
        cell4.setCellValue("convertRation");
        //写入数据
        for (int i = 0; i < list.size(); i++) {
            EnterpriseDataInfo enterpriseDataInfo = list.get(i);
            ArrayList<String> strings = new ArrayList<>();
            strings.add(enterpriseDataInfo.getName());
            strings.add(enterpriseDataInfo.getCode());
            strings.add(enterpriseDataInfo.getDataValue());
            strings.add(enterpriseDataInfo.getStatType());
            strings.add(enterpriseDataInfo.getConvertRation());
            HSSFRow row1 = sheet.createRow(i + 1);
            for (int j = 0; j < strings.size(); j++) {
                row1.createCell(j).setCellValue(strings.get(j));
            }
        }
        sheets.write(bufferedOutputStream);
        bufferedOutputStream.close();
    }

    public static HorizontalCellStyleStrategy myHorizontalCellStyleStrategy(){
        //表头样式策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //表头前景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        WriteFont headWriteFont = new WriteFont();
        //headWriteFont.setBold(true);
        headWriteFont.setFontName("宋体");
        headWriteFont.setFontHeightInPoints((short)11);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontName("宋体");
        contentWriteFont.setFontHeightInPoints((short)11);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //设置自动换行
        contentWriteCellStyle.setWrapped(true);
        //设置垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置边框样式
        setBorderStyle(contentWriteCellStyle);
        //内容风格可以定义多个。
        List<WriteCellStyle> listCntWritCellSty =  new ArrayList<>();
        listCntWritCellSty.add(contentWriteCellStyle);
        WriteCellStyle contentWriteCellStyle2 = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色。
        // 头默认了 FillPatternType所以可以不指定。
        contentWriteCellStyle2.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
        //contentWriteCellStyle2.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        //设置垂直居中
        contentWriteCellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置水平居中
        contentWriteCellStyle2.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置边框样式
        setBorderStyle(contentWriteCellStyle2);
        listCntWritCellSty.add(contentWriteCellStyle2);
        // 水平单元格风格综合策略(表头 + 内容)
        // return  new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        return  new HorizontalCellStyleStrategy(headWriteCellStyle, listCntWritCellSty);
    }

    //设置边框样式
    public static void setBorderStyle(WriteCellStyle contentWriteCellStyle){
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        // contentWriteCellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex()); //颜色
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
    }

}
