package com.ruoyi.common.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.ExcelExp;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;

/**
 * 多sheet 导出
 */
public class ExcelExportUtil {
    public static void exportManySheetExcel(String fileName, List<ExcelExp> mysheets, HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();//创建工作薄
        List<ExcelExp> sheets = mysheets;
        //表头样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
        style.setAlignment(HorizontalAlignment.CENTER);// 水平
        //字体样式
        HSSFFont fontStyle = wb.createFont();
        fontStyle.setFontName("微软雅黑");
        fontStyle.setFontHeightInPoints((short) 12);
        style.setFont(fontStyle);
        for (ExcelExp excel : sheets) {
            //新建一个sheet
            HSSFSheet sheet = wb.createSheet(excel.getFileName());//获取该sheet名称
            String[] handers = excel.getHanders();//获取sheet的标题名
            String[] header = new String[handers.length];
            String[] code = new String[handers.length];
            for (int i = 0; i < handers.length; i++) {
                String[] temp = handers[i].split("#");//由两部分组成
                header[i] = temp[0];//#号之前的是标题汉字
                if (temp.length > 1) {
                    code[i] = temp[1];  //#号之后的是查询出得映射字段
                }
            }
            HSSFRow rowFirst = sheet.createRow(0);//第一个sheet的第一行为标题
            //写标题
            for (int i = 0; i < header.length; i++) {
                //获取第一行的每个单元格
                HSSFCell cell = rowFirst.createCell(i);
                //往单元格里写数据
                cell.setCellValue(header[i]);
                cell.setCellStyle(style); //加样式
                sheet.setColumnWidth(i, 10000); //设置每列的列宽
            }
            //写数据集
            List clazz = Collections.singletonList(excel.getClazz());
            clazz = excel.getDataset();
            for (int i = 0; i < clazz.size(); i++) {  //获取多少行
                HSSFRow row = sheet.createRow(i + 1); //设置行
                String json = JSONUtil.parse(clazz.get(i)).toString();
                JSONObject jsonObject = JSONObject.parseObject(json);  //转换成json
                for (int j = 0; j < header.length; j++) {
                    row.createCell(j).setCellValue((String) jsonObject.get(code[j]));  //填充指定的单元格
                }
            }
        }

        //下载
        try {
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "utf-8"));
            response.setHeader("Cache-Control", "No-cache");
            response.flushBuffer();
            wb.write(response.getOutputStream());
            wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}