package com.post.excel.create;

import com.ruoyi.common.utils.DateUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 原文：https://blog.csdn.net/qq_42612200/article/details/105860179
 * https://www.cnblogs.com/luckyang/p/14637076.html
 * 样式：https://www.cnblogs.com/fqfanqi/p/6172223.html
 */
public class MyExcelUtilV1 {

    public static void main(String[] args) throws Exception {
        List<collectionAndPaymentListBean> getselectTo = new ArrayList<>();

        for (int i = 0; i <= 10; i++) {
            collectionAndPaymentListBean data = new collectionAndPaymentListBean(i + 1);
            getselectTo.add(data);
        }

        run("人力资源部岗位经理检查表-" + DateUtils.dateTimeNow(), "岗位检查表", getselectTo);
    }

    /**
     * 导出 Excel
     *
     * @param fileName    导出时的文件名称
     * @param sheetName   工作簿名字
     * @param getselectTo 需要要导出的数据
     */

    public static void run(String fileName, String sheetName, List<collectionAndPaymentListBean> getselectTo) {

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

        /** 第二步，在Workbook中添加一个sheet,对应Excel文件中的sheet  */
        HSSFSheet sheet = wb.createSheet(sheetName);

        /** 第三步，设置样式以及字体样式*/
        //标题样式
        HSSFCellStyle titleStyleOne = createTitleCellStyleOne(wb);

        HSSFCellStyle titleStyle = createTitleCellStyle(wb);
        //创建表头样式
        HSSFCellStyle headerStyle = createHeadCellStyle(wb);
        //内容样式
        HSSFCellStyle contentStyle = createContentCellStyle(wb);
        //		日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //设置全局 单元格  默认 宽度
        sheet.setDefaultColumnWidth(12);

        /** 第四步，创建标题 ,合并标题单元格 */
        // 行号
        int rowNum = 0;
        // 创建第一页的第一行，索引从0开始
        HSSFRow row0 = sheet.createRow(rowNum++);
        row0.setHeight((short) 800);// 设置行高

        String title = "行政岗位经理岗位责任清单";
        HSSFCell c00 = row0.createCell(0);
        c00.setCellStyle(titleStyleOne);
        c00.setCellValue(title);
        // 合并单元格，参数依次为：起始行，结束行，起始列，结束列 （索引0开始）
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));//标题合并单元格操作，17为总列数


        // 第二行
        HSSFRow row1 = sheet.createRow(rowNum++);
        row1.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCellOne = row1.createCell(i);
            tempCellOne.setCellStyle(headerStyle);
            if (i == 0) {
                tempCellOne.setCellValue("一、基本资料");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));



        // 第三行
        HSSFRow row2 = sheet.createRow(rowNum++);
        row2.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {

            HSSFCell tempCell = row2.createCell(i);
            tempCell.setCellStyle(headerStyle);

            if (i == 0) {
                tempCell.setCellValue("所在单位：");
            }
            if (i == 1) {
                tempCell.setCellValue("data-惠州石化");
            } else if (i == 4) {
                tempCell.setCellValue("所在部门：");
            } else if (i == 5) {
                tempCell.setCellValue("data-办公室");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 3));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 5, 7));



        // 第四行
        HSSFRow row4 = sheet.createRow(rowNum++);
        row4.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCell = row4.createCell(i);
            tempCell.setCellStyle(headerStyle);

            if (i == 0) {
                tempCell.setCellValue("岗位名称：");
            }
            if (i == 1) {
                tempCell.setCellValue("data-行政岗位经理");
            } else if (i == 4) {
                tempCell.setCellValue("岗位编码：");
            } else if (i == 5) {
                tempCell.setCellValue("data-post_0001PM");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 3));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 7));


        // 第五行
        HSSFRow row5 = sheet.createRow(rowNum++);
        row5.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCell = row5.createCell(i);
            tempCell.setCellStyle(headerStyle);

            if (i == 0) {
                tempCell.setCellValue("岗位级别：");
            }
            if (i == 1) {
                tempCell.setCellValue("data-M9");
            } else if (i == 4) {
                tempCell.setCellValue("是否涉密：");
            } else if (i == 5) {
                tempCell.setCellValue("data-否");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(4, 4, 1, 3));
        sheet.addMergedRegion(new CellRangeAddress(4, 4, 5, 7));


        // 第六行
        HSSFRow row6 = sheet.createRow(rowNum++);
        row6.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCell = row6.createCell(i);
            tempCell.setCellStyle(headerStyle);

            if (i == 0) {
                tempCell.setCellValue("直接上级：");
            }
            if (i == 1) {
                tempCell.setCellValue("data-办公室主任");
            } else if (i == 4) {
                tempCell.setCellValue("直接下级：");
            } else if (i == 5) {
                tempCell.setCellValue("data-下级");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 3));
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 5, 7));



        // 第七行
        HSSFRow row7 = sheet.createRow(rowNum++);
        row7.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCell = row7.createCell(i);
            tempCell.setCellStyle(headerStyle);

            if (i == 0) {
                tempCell.setCellValue("管辖人数：");
            }
            if (i == 1) {
                tempCell.setCellValue("data-100");
            } else if (i == 4) {
                tempCell.setCellValue("编制/修订日期：");
            } else if (i == 5) {
                tempCell.setCellValue("data-2023年8月");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(6, 6, 1, 3));
        sheet.addMergedRegion(new CellRangeAddress(6, 6, 5, 7));

        // 第8行：工作职责
        HSSFRow row8 = sheet.createRow(rowNum++);
        row8.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCellOne = row8.createCell(i);
            tempCellOne.setCellStyle(headerStyle);
            if (i == 0) {
                tempCellOne.setCellValue("二、工作职责");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 7));



        // 第9行：工作摘要
        HSSFRow row9 = sheet.createRow(rowNum++);
        row9.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCellOne = row9.createCell(i);
            tempCellOne.setCellStyle(headerStyle);
            if (i == 0) {
                tempCellOne.setCellValue("1. 工作摘要");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(8, 8, 0, 7));


        // 第10行：工作摘要的内容
        HSSFRow row10 = sheet.createRow(rowNum++);
        row10.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCellOne = row10.createCell(i);
            tempCellOne.setCellStyle(headerStyle);
            if (i == 0) {
                tempCellOne.setCellValue("负责公司公务接待管理及重要活动组织安排，负责公司公共关系、相关方、公司会务、办公设备及非生产类低值易耗品等的管理，负责公司信访办公室日常工作。履行岗位职责的同时，必须履行职责范围内的合规管理、保密管理、安全管理职责，并承诺做到廉洁从业。");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(9, 9, 0, 7));


        // 第11行：主要职责
        HSSFRow row11 = sheet.createRow(rowNum++);
        row11.setHeight((short) 500);
        for (int i = 0; i < 7; i++) {
            HSSFCell tempCellOne = row11.createCell(i);
            tempCellOne.setCellStyle(headerStyle);
            if (i == 0) {
                tempCellOne.setCellValue("2. 主要职责");
            }
        }
        // 合并
        sheet.addMergedRegion(new CellRangeAddress(10, 10, 0, 7));





        // 第七行  分割
        rowNum++;

        // 第八行
        HSSFRow rowData = sheet.createRow(rowNum++);
        rowData.setHeight((short) 500);
        for (int i = 0; i < 18; i++) {
            HSSFCell tempCell = rowData.createCell(i);
            tempCell.setCellStyle(headerStyle);
            if (i == 0) {
                tempCell.setCellValue("序号 ");
            } else if (i == 1) {
                tempCell.setCellValue("'收款日期");
            } else if (i == 2) {
                tempCell.setCellValue("发票金额");
            } else if (i == 3) {
                tempCell.setCellValue("业主拨款");
            } else if (i == 4) {
                tempCell.setCellValue("管理费");
            } else if (i == 5) {
                tempCell.setCellValue("税费");
            } else if (i == 6) {
                tempCell.setCellValue("资料保证金");
            } else if (i == 7) {
                tempCell.setCellValue("通讯费");
            } else if (i == 8) {
                tempCell.setCellValue("交通费");
            } else if (i == 9) {
                tempCell.setCellValue("办公费");
            } else if (i == 10) {
                tempCell.setCellValue("保险费");
            } else if (i == 11) {
                tempCell.setCellValue("差旅费");
            } else if (i == 12) {
                tempCell.setCellValue("利息");
            } else if (i == 13) {
                tempCell.setCellValue("履约保证金");
            } else if (i == 14) {
                tempCell.setCellValue("保理手续费");
            } else if (i == 15) {
                tempCell.setCellValue("工资社保公积金");
            } else if (i == 16) {
                tempCell.setCellValue("扣款合计");
            } else if (i == 17) {
                tempCell.setCellValue("实付款");
            }

        }


        for (collectionAndPaymentListBean c : getselectTo) {
            HSSFRow tempRow = sheet.createRow(rowNum++);
            tempRow.setHeight((short) 500);
            // 循环单元格填入数据
            for (int j = 0; j < 18; j++) {
                HSSFCell tempCell = tempRow.createCell(j);
                tempCell.setCellStyle(contentStyle);
                String tempValue = null;
                if (j == 0) {
                    //序号
                    tempValue = c.getIsNewRecord();
                } else if (j == 1) {
                    // 收款日期
                    if (null == c.getReceiveBillDate()) {
                        tempValue = "";
                    } else {
                        tempValue = c.getReceiveBillDate().substring(0, 10);
                    }
                } else if (j == 2) {
                    // 发票金额
                    if (null == c.getAmount()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getAmount() + "";
                    }
                } else if (j == 3) {
                    // 业主拨款
                    if (null == c.getHomeowners()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getHomeowners() + "";
                    }
                } else if (j == 4) {
                    // 管理费
                    if (null == c.getCoffee()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getCoffee() + "";
                    }

                } else if (j == 5) {
                    // 税费
                    if (null == c.getVorhanden()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getVorhanden() + "";
                    }
                } else if (j == 6) {
                    // 资料保证金
                    if (null == c.getSecurity()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getSecurity() + "";
                    }
                } else if (j == 7) {
                    //通讯费
                    if (null == c.getCommunications()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getCommunications() + "";
                    }
                } else if (j == 8) {
                    // 交通费
                    if (null == c.getTransportation()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getTransportation() + "";
                    }
                } else if (j == 9) {
                    // 办公费
                    if (null == c.getWork()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getWork() + "";
                    }
                } else if (j == 10) {
                    // 保险费
                    if (null == c.getSetAInsPremium()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getSetAInsPremium() + "";
                    }
                } else if (j == 11) {
                    // 差旅费
                    if (null == c.getResearchTravel()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getResearchTravel() + "";
                    }
                } else if (j == 12) {
                    // 利息
                    if (null == c.getAccrualAmount()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getAccrualAmount() + "";
                    }
                } else if (j == 13) {
                    // 履约保证金
                    if (null == c.getPerformance()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getPerformance() + "";
                    }
                } else if (j == 14) {
                    // 保理手续费
                    if (null == c.getFactoring()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getFactoring() + "";
                    }
                } else if (j == 15) {
                    // 工资社保公积金
                    if (null == c.getCoffeehous()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getCoffeehous() + "";
                    }
                } else if (j == 16) {
                    // 扣款合计
                    if (null == c.getOfdeductions()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getOfdeductions() + "";
                    }
                } else if (j == 17) {
                    // 实付款
                    if (null == c.getPaymentImpl()) {
                        tempValue = "0.00";
                    } else {
                        tempValue = c.getPaymentImpl() + "";
                    }
                } else if (j == 18) {
                    // 备注
                    tempValue = "0.00";
                }

                tempCell.setCellValue(tempValue);
            }
        }

        try {
            //调用下载     导出
            downloadExcel(wb, fileName + ".xls");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //下载
    public static void downloadExcel(HSSFWorkbook wb, String fileName) throws IOException {
//        response.reset();
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes(), "iso-8859-1"));

        OutputStream out = null;
        try {
            int index = fileName.lastIndexOf(".");

            String fileNamePrefix = "岗位检查表";
            String fileNameSuffix = ".xls";
            if (index > -1) {
                fileNamePrefix = fileName.substring(0, index);
                fileNameSuffix = fileName.substring(index);
            }

            out = new FileOutputStream(new File("C:\\test\\" + fileNamePrefix + fileNameSuffix));
            wb.write(out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }

    }


    /**
     * 创建标题样式
     *
     * @param wb
     * @return
     */
    private static HSSFCellStyle createTitleCellStyleOne(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景颜色

        HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont1.setBold(true); //字体加粗
        headerFont1.setFontName("黑体"); // 设置字体类型
        headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
        cellStyle.setFont(headerFont1); // 为标题样式设置字体样式

        return cellStyle;
    }

    /**
     * 创建标题样式
     *
     * @param wb
     * @return
     */
    private static HSSFCellStyle createTitleCellStyle(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//        cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景颜色

        HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont1.setBold(true); //字体加粗
        headerFont1.setFontName("黑体"); // 设置字体类型
        headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
        cellStyle.setFont(headerFont1); // 为标题样式设置字体样式

        return cellStyle;
    }


    /**
     * 创建表头样式
     *
     * @param wb
     * @return
     */
    private static HSSFCellStyle createHeadCellStyle(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setWrapText(true);// 设置自动换行
//        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景颜色
        cellStyle.setAlignment(HorizontalAlignment.LEFT); //水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直对齐
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
        cellStyle.setBorderRight(BorderStyle.THIN); //右边框
        cellStyle.setBorderTop(BorderStyle.THIN); //上边框

        HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式
        headerFont.setBold(true); //字体加粗
        headerFont.setFontName("黑体"); // 设置字体类型
        headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
        cellStyle.setFont(headerFont); // 为标题样式设置字体样式

        return cellStyle;
    }


    /**
     * 创建内容样式
     *
     * @param wb
     * @return
     */
    private static HSSFCellStyle createContentCellStyle(HSSFWorkbook wb) {
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
        cellStyle.setWrapText(true);// 设置自动换行
        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
        cellStyle.setBorderRight(BorderStyle.THIN); //右边框
        cellStyle.setBorderTop(BorderStyle.THIN); //上边框

        // 生成12号字体
        HSSFFont font = wb.createFont();
        font.setColor((short) 8);
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);

        return cellStyle;
    }


}
