package cn.chnmuseum.party.common.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExportExcelUtil {
//
//    public static String exportExcel(List<?> list, String[] titles, String[] keys, String name) {
//        OutputStream outputStream = null;
//        String id = UUIDUtil.getUUID();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
//        Date date = new Date();
//        String saveDir = "/" + simpleDateFormat.format(date);
////        String property = "/home/changfa/app/tomcat-zhny/file" + saveDir;
//        String property = RuoYiConfig.getProfile() + saveDir;
////        String property = "D:\\畅发科技公司";
//        File fileDir = new File(property);
//        if (!fileDir.exists()) {
//            fileDir.mkdir();
//        }
//        String path = property + "/" + name + id + ".xls";
//        try {
//            try {
//                File file = new File(path);
//                outputStream = new FileOutputStream(file);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            getHSSFWorkbook(titles, list, keys).write(outputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (outputStream != null) {
//                try {
//                    outputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return RuoYiConfig.getDownload() + saveDir + "/" + name + id + ".xls";
////        return "192.168.110.86:8081/file" + saveDir + "/" + name + id + ".xls";
//
//    }

    /**
     * 导出Excel
     *
     * @param title   标题
     * @param mapList 内容
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String[] title, List<?> mapList, String[] keys) {

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("sheet1");

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);


        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //存储最大列宽
        Map<Integer, Integer> maxWidth = new HashMap<>();

        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
            maxWidth.put(i, cell.getStringCellValue().getBytes().length * 256 + 512);
        }

        Map<String, Object> map = null;
        Object a = null;
        //创建内容
        for (int i = 0; i < mapList.size(); i++) {
            a = mapList.get(i);
            //将实体类转化为map集合
            map = ObjectToMapUtil.object2Map(a);
            //添加数据行
            row = sheet.createRow(i + 1);
            int k = 0;
            int length = 0;
            for (int j = 0; j < keys.length; j++) {
                //创建单元格
                Cell cells = row.createCell(j);
                //获取单元格内容
                Object o = map.get(keys[j]);
                if (j == 0) {
                    //设置序号
                    cells.setCellValue(i + 1);
                } else {
                    if (o == null) {
                        cells.setCellValue("");
                    } else {
                        cells.setCellValue(o.toString());
                    }
                    //根据内容设置列宽
                    length = cells.getStringCellValue().length() * 521 + 1024;
                    //这里把宽度最大限制到25000
                    if (length > 25000) {
                        length = 25000;
                    }
                    maxWidth.put(k, Math.max(length, maxWidth.get(k)));
                    k++;
                    cells.setCellStyle(style);
                }
            }
        }
        //根据内容设置列宽
        for (int i = 0; i < title.length; i++) {
            //跳过序号列
            sheet.setColumnWidth(i + 1, maxWidth.get(i));
        }
        return wb;
    }
}