package com.shining.cloud.util;

import com.njcn.pojo.excelreport.FpyByType;
import com.njcn.pojo.excelreport.FpyRateData;
import com.njcn.pojo.excelreport.OverLimitInfoData;
import com.njcn.pojo.excelreport.ReportTargetData;
import com.njcn.utils.PubUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {
    private static final Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
    private XSSFWorkbook workbook;
    private List<XSSFSheet> listSheet = new ArrayList<>();
    private String tableName;
    private int rowId;
    private XSSFCellStyle style1;
    private XSSFCellStyle style2;
    private XSSFCellStyle style3;
    private XSSFCellStyle style4;
    private XSSFCellStyle style5;
    private XSSFCellStyle style6;
    private XSSFCellStyle style7;
    private XSSFCellStyle defaultStyle;
    private OverLimitInfoData overLimitInfo; //新增标识相别名称

    public void setOverLimitInfo(OverLimitInfoData overLimitInfo) {
        this.overLimitInfo = overLimitInfo;
    }

    //新建空白工作薄
    public void createWorkBook() {
        workbook = new XSSFWorkbook();
    }

    //新建空白表单
    public void createSheet(String sheetName) {
        XSSFSheet sheet = workbook.createSheet(sheetName);
        listSheet.add(sheet);

        for (int i = 0; i < 8; i++) {
            setStyle(i);
        }
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public XSSFWorkbook getWorkbook() {
        return workbook;
    }

    public List<XSSFSheet> getListSheet() {
        return listSheet;
    }

    //导出表格
    public void exportFile(XSSFWorkbook workbook, HttpServletResponse response, HttpServletRequest request) {
        try {
            response.setContentType("application/force-download");
            response.addHeader("Content-Disposition", "attachment;fileName=" + new String(tableName.getBytes("GB2312"), "iso8859-1") + "_" + PubUtils.getNow() + ".xlsx");// 设置文件名
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException e) {
            logger.error("导出表格发生异常，异常是"+e.getMessage());
        }
    }

    private XSSFCellStyle getStyle(int styleType) {
        switch (styleType) {
            case 0:
                return style1;
            case 1:
                return style2;
            case 3:
                return style3;
            case 4:
                return style4;
            case 5:
                return style5;
            case 6:
                return style6;
            case 7:
                return style7;
            default:
                return defaultStyle;
        }
    }

    private void setStyle(int styleType) {
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 8);

        switch (styleType) {
            case 0:
                style1 = workbook.createCellStyle();
                font.setFontHeightInPoints((short) 20);
                font.setBold(true);
                style1.setVerticalAlignment(VerticalAlignment.CENTER);
                style1.setAlignment(HorizontalAlignment.CENTER_SELECTION);
                style1.setFont(font);
                break;
            case 1:
                style2 = workbook.createCellStyle();
                style2.setVerticalAlignment(VerticalAlignment.CENTER);
                font.setBold(true);
                style2.setFont(font);
                break;
            case 3:
                style3 = workbook.createCellStyle();
                font.setFontHeightInPoints((short) 14);
                font.setBold(true);
                style3.setVerticalAlignment(VerticalAlignment.CENTER);
                style3.setAlignment(HorizontalAlignment.CENTER_SELECTION);
                style3.setFont(font);
                break;
            case 4:
                style4 = workbook.createCellStyle();
                font.setBold(true);
                style4.setVerticalAlignment(VerticalAlignment.CENTER);
                style4.setAlignment(HorizontalAlignment.CENTER_SELECTION);
                style4.setFont(font);
                break;
            case 5:
                style5 = workbook.createCellStyle();
                font.setColor(HSSFColor.RED.index);
                style5.setVerticalAlignment(VerticalAlignment.CENTER);
                style5.setAlignment(HorizontalAlignment.CENTER_SELECTION);
                style5.setFont(font);
                break;
            case 6:
                style6 = workbook.createCellStyle();
                style6.setVerticalAlignment(VerticalAlignment.CENTER);
                style6.setAlignment(HorizontalAlignment.CENTER_SELECTION);
                style6.setFont(font);
                break;
            case 7:
                style7 = workbook.createCellStyle();
                font.setBold(true);
                style7.setRotation((short) 255);
                style7.setVerticalAlignment(VerticalAlignment.CENTER);
                style7.setAlignment(HorizontalAlignment.CENTER_SELECTION);
                style7.setFont(font);
                break;
            default:
                defaultStyle = workbook.createCellStyle();
                defaultStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                defaultStyle.setFont(font);
                break;
        }
    }

    //填充表头部分
    public void createHeader(JSONObject jsonObject, JSONObject json, XSSFSheet sheet) {
        String name = sheet.getSheetName();
        int length = name.equals("合格率") ? 4 : 0;  //如果表单是合格率，则长度偏差设为4
        int len = length / 2;

        rowId = 0;
        //第一行  表名
        XSSFRow row = sheet.createRow(rowId++);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(jsonObject.get("bdName") + "  " + jsonObject.get("pName") + name + "统计报表");
        cell.setCellStyle(getStyle(0));
        sheet.createRow(rowId++);
        sheet.addMergedRegion(new CellRangeAddress(rowId - 2, rowId - 1, 0, 15 + length));
        //第二行 统计时间
        row = sheet.createRow(rowId++);
        cell = row.createCell(0);
        cell.setCellValue("统计报表时间:");
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 0, 1));
        cell.setCellStyle(getStyle(1));
        cell = row.createCell(2);
        cell.setCellValue(jsonObject.get("time").toString());
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 2, 15 + length));
        cell.setCellStyle(getStyle(2));
        //第三行 检修时间
        row = sheet.createRow(rowId++);
        cell = row.createCell(0);
        cell.setCellValue("检修时间:");
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 0, 1));
        cell.setCellStyle(getStyle(1));
        cell = row.createCell(2);
        cell.setCellValue(jsonObject.get("exceptTime").toString());
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 2, 15 + length));
        cell.setCellStyle(getStyle(2));
        //第四行 监测点名称 电压等级
        row = sheet.createRow(rowId++);
        cell = row.createCell(0);
        cell.setCellValue("监测线路:");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 0, 1));
        cell = row.createCell(2);
        cell.setCellValue(jsonObject.get("Name").toString());
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 2, 10 + length));
        cell = row.createCell(11 + length);
        cell.setCellValue("电压等级(kV):");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 11 + length, 13 + length));
        cell = row.createCell(14 + length);
        cell.setCellValue(json.get("voltageClasses").toString());
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 14 + length, 15 + length));
        //第五行 PT/CT变比 基准短路容量
        row = sheet.createRow(rowId++);
        cell = row.createCell(0);
        cell.setCellValue("PT变比:");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 0, 1));
        cell = row.createCell(2);
        cell.setCellValue(json.get("pt1") + "/" + json.get("pt2"));
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 2, 4 + len));
        cell = row.createCell(5 + len);
        cell.setCellValue("CT变比:");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 5 + len, 7 + len));
        cell = row.createCell(8 + len);
        cell.setCellValue(json.get("ct1") + "/" + json.get("ct2"));
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 8 + len, 10 + len * 2));
        cell = row.createCell(11 + len * 2);
        cell.setCellValue("基准短路容量(MVA):");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 11 + len * 2, 13 + len * 2));
        cell = row.createCell(14 + len * 2);
        cell.setCellValue(json.get("jzcmp") + "MVA");
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 14 + len * 2, 15 + len * 2));
        //第六行 母线最小短路容量 用户协议用电容量  供电设备容量
        row = sheet.createRow(rowId++);
        cell = row.createCell(0);
        cell.setCellValue("母线最小短路容量(MVA):");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 0, 1));
        cell = row.createCell(2);
        cell.setCellValue(json.get("dlcmp") + "MVA");
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 2, 4 + len));
        cell = row.createCell(5 + len);
        cell.setCellValue("用户协议用电容量(MVA):");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 5 + len, 7 + len));
        cell = row.createCell(8 + len);
        cell.setCellValue(json.get("xycmp") + "MVA");
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 8 + len, 10 + len * 2));
        cell = row.createCell(11 + len * 2);
        cell.setCellValue("供电设备容量(MVA):");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 11 + len * 2, 13 + len * 2));
        cell = row.createCell(14 + len * 2);
        cell.setCellValue(json.get("devcmp") + "MVA");
        cell.setCellStyle(getStyle(2));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 14 + len * 2, 15 + len * 2));
        //第七行 工况
        row = sheet.createRow(rowId++);
        cell = row.createCell(0);
        cell.setCellValue("工况:");
        cell.setCellStyle(getStyle(1));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 0, 1));
        cell = row.createCell(2);
        cell.setCellValue("");
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, 2, 15 + length));
    }

    public void writeNonHarmonics(XSSFSheet sheet, List<ReportTargetData>... lists) {
        JSONObject jsonObject = judge("有效值", true);
        int end = writeHeader(sheet, jsonObject, this.rowId);

        end = writeBody(lists[0], jsonObject, JSONObject.fromObject("{'data':[{'name':'电压','unit':'kV'},{'name':'电流','unit':'A'},{'name':'线电压','unit':'kV'}]}"), end, sheet);

        jsonObject = judge("功率", true);
        end = writeHeader(sheet, jsonObject, end);
        end = writeBody(lists[1], jsonObject, JSONObject.fromObject("{'data':[{'name':'有功','unit':'kW'},{'name':'无功','unit':'kVar'},{'name':'视在','unit':'kVA'}]}"), end, sheet);

        jsonObject = judge("电压闪变", true);
        end = writeHeader(sheet, jsonObject, end);
        end = writeBody(lists[2], jsonObject, JSONObject.fromObject("{'data':[{'name':'Pst','unit':'/'},{'name':'Plt','unit':'/'}]}"), end, sheet);

        jsonObject = judge("畸变率", true);
        end = writeHeader(sheet, jsonObject, end);
        end = writeBody(lists[3], jsonObject, JSONObject.fromObject("{'data':[{'name':'电压畸变率','unit':'%'},{'name':'电流畸变率','unit':'%'}]}"), end, sheet);

        jsonObject = judge("电压偏差", true);
        end = writeHeader(sheet, jsonObject, end);
        end = writeBody(lists[4], jsonObject, JSONObject.fromObject("{'data':[{'name':'电压偏差','unit':'%'}]}"), end, sheet);

        jsonObject = judge("频率", false);
        end = writeHeader(sheet, jsonObject, end);
        end = writeBody(lists[5], jsonObject, JSONObject.fromObject("{'data':[{'name':'频率','unit':'Hz'},{'name':'频率偏差','unit':'Hz'}]}"), end, sheet);

        jsonObject = judge("三相电压不平衡度", false);
        end = writeHeader(sheet, jsonObject, end);
        writeBody(lists[6], jsonObject, JSONObject.fromObject("{'data':[{'name':'电压不平衡','unit':'%'},{'name':'正序电压','unit':'kV'},{'name':'负序电压','unit':'V'}" +
                ",{'name':'零序电压','unit':'V'},{'name':'电流不平衡','unit':'%'},{'name':'正序电流','unit':'A'},{'name':'负序电流','unit':'A'},{'name':'零序电流','unit':'A'}]}"), end, sheet);
    }

    /*
     * b为true标识谐波电压含有率
     * b为false标识谐波电流幅值
     */
    public void writeHarmonics(XSSFSheet sheet, List<ReportTargetData> list, Boolean b) {
        int end;

        if (b) {
            JSONObject jsonObject = judge("谐波电压含有率", true);
            end = writeHeader(sheet, jsonObject, this.rowId);
            writeBody(list, jsonObject, JSONObject.fromObject(constructorVoltage(list)), end, sheet);
        } else {
            JSONObject jsonObject = judge("谐波电流幅值", true);
            end = writeHeader(sheet, jsonObject, this.rowId);
            writeBody(list, jsonObject, JSONObject.fromObject(constructorICurrent(list)), end, sheet);
        }
    }

    public void writeFpyData(XSSFSheet sheet, List<FpyByType> list) {
        int end = this.rowId;
        JSONObject jsonObject = judgeFpy();

        if (jsonObject == null) { //如果没有选择该项指标，直接返回
            return;
        }

        int length = string2Int(jsonObject, "dyhyl") + string2Int(jsonObject, "dlhyl");
        int len = string2Int(jsonObject, "plpc") + string2Int(jsonObject, "sxdybphd");

        if (length > 0) {
            end = writeFpyHeader(sheet, jsonObject, this.rowId, 1, length, 0);//表头部分
            //填充数据
            end = writeFpyBody(sheet, list, jsonObject, end, length);
        }

        length = string2Int(jsonObject, "dypc") + string2Int(jsonObject, "dycssb") + string2Int(jsonObject, "dyxbjbl");

        if (length + len > 0) {
            end = writeFpyHeader(sheet, jsonObject, end, 2, length, len);//表头部分
            //填充数据
            end = writeNoFpyBody(sheet, list, jsonObject, end, length, len);
        }
    }

    public int writeFpyBody(XSSFSheet sheet, List<FpyByType> list, JSONObject jsonObject, int rowId, int length) {
        XSSFRow row = null;
        XSSFCell cell;

        for (int i = 0; i < 24; i++) {
            int cellId = 0;
            row = sheet.createRow(rowId++);

            for (int j = 0; j < length; j++) {
                j = (string2Int(jsonObject, "dyhyl") == 0 ? 1 : j);
                cell = row.createCell(cellId);

                if (string2Int(jsonObject, "dyhyl") == 1 && i == 0 && j < 1) {
                    sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId + 22, cellId, cellId));
                    cell.setCellValue("2至25次谐波电压含有率");
                    cell.setCellStyle(getStyle(7));
                } else if (string2Int(jsonObject, "dlhyl") == 1 && i == 0) {
                    cell.setCellValue("2至25次谐波电流含有率");
                    cell.setCellStyle(getStyle(7));
                    sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId + 22, cellId, cellId));
                }

                cellId++;
                //谐波次数
                cell = row.createCell(cellId);
                cell.setCellValue(i + 2);
                cell.setCellStyle(getStyle(4));
                cellId++;

                //总的合格率
                cellId = drawPhase(row, cell, cellId, list.get(j).getListS(), list.get(j).getFpy(), "T", i, sheet, rowId);
                //A相指标
                cellId = drawPhase(row, cell, cellId, list.get(j).getListA(), list.get(j).getFpy(), "A", i, sheet, rowId);
                //B相指标
                cellId = drawPhase(row, cell, cellId, list.get(j).getListB(), list.get(j).getFpy(), "B", i, sheet, rowId);
                //C相指标
                if (!(overLimitInfo.getMode() == 2) || string2Int(jsonObject, "dlhyl") == 1 && j == 1) {
                    cellId = drawPhase(row, cell, cellId, list.get(j).getListC(), list.get(j).getFpy(), "C", i, sheet, rowId);
                } else {
                    cellId = drawPhase(row, cell, cellId, list.get(j).getListC(), null, "C", i, sheet, rowId);
                }
            }
        }

        return rowId;
    }

    public int drawPhaseByName(XSSFSheet sheet, int rowId, String name, XSSFRow row, XSSFCell cell, int cellId, List<FpyRateData> list, Integer fpy, String phase, int index) {
        if (name != null) {
            cell = row.createCell(cellId);
            cell.setCellValue(name);
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
            cellId += 2;
        }

        return drawPhase(row, cell, cellId, list, fpy, phase, index, sheet, rowId);
    }

    public int drawPhase(XSSFRow row, XSSFCell cell, int cellId, List<FpyRateData> list, Integer fpy, String phase, int index, XSSFSheet sheet, int rowId) {
        cell = row.createCell(cellId);
        int length = (phase.equals("T")) ? 1 : 2;

        if (phase.equals("T")) {
            cell.setCellValue(fpy);
            cell.setCellStyle(getStyle(6));
            if (overLimitInfo.getList().size() == 2) {
                sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
            }
            cellId += (overLimitInfo.getList().size() == 2 ? 2 : 1);
        }
        if (fpy == null) {
            for (int i = 0; i < length; i++) {
                cell = row.createCell(cellId);
                cell.setCellValue("-");
                cell.setCellStyle(getStyle(6));
                cellId++;
            }
        } else if (list != null) {
            if (!phase.equals("T")) {
                cell = row.createCell(cellId);
                cell.setCellValue(list.get(index).getCount());
                cell.setCellStyle(getStyle(6));
                cellId++;
            }

            cell = row.createCell(cellId);
            double cellValue = Double.parseDouble(String.valueOf(list.get(index).getRate()));  //add by sw
            cell.setCellValue(cellValue);

            if (list != null && fpy.doubleValue() > list.get(index).getRate()) {
                cell.setCellStyle(getStyle(5));
            } else {
                cell.setCellStyle(getStyle(6));
            }

            if (overLimitInfo.getList().size() == 2 && phase.equals("T")) {
                sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
            }

            cellId += ((overLimitInfo.getList().size() == 2 && phase.equals("T")) ? 2 : 1);
        } else {
            for (int i = 0; i < length; i++) {
                cell = row.createCell(cellId);
                cell.setCellValue("/");
                cell.setCellStyle(getStyle(6));

                if (overLimitInfo.getList().size() == 2 && phase.equals("T")) {
                    sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
                }

                cellId += ((overLimitInfo.getList().size() == 2 && phase.equals("T")) ? 2 : 1);
            }
        }

        return cellId;
    }

    public int writeNoFpyBody(XSSFSheet sheet, List<FpyByType> list, JSONObject jsonObject, int rowId, int length, int len) {
        XSSFRow row;
        XSSFCell cell = null;
        int cellId = 0;

        if (length > 0) {
            //没有后半部分指标
            if (len == 0) {
                if (jsonObject.get("dypc").equals("1")) {
                    row = sheet.createRow(rowId++);
                    cellId = drawPhaseByName(sheet, rowId, "电压偏差", row, cell, 0, list.get(2).getListS(), list.get(2).getFpy(), "T", 0);
                    cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListA(), list.get(2).getFpy(), "A", 0);
                    cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListB(), list.get(2).getFpy(), "B", 0);
                    if (!(overLimitInfo.getMode() == 2)) {
                        drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListC(), list.get(2).getFpy(), "C", 0);
                    } else {
                        drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListC(), null, "C", 0);
                    }
                }

                if (jsonObject.get("dyxbjbl").equals("1")) {
                    row = sheet.createRow(rowId++);
                    cellId = drawPhaseByName(sheet, rowId, "电压谐波畸变率", row, cell, 0, list.get(4).getListS(), list.get(4).getFpy(), "T", 0);
                    cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListA(), list.get(4).getFpy(), "A", 0);
                    cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListB(), list.get(4).getFpy(), "B", 0);
                    if (!(overLimitInfo.getMode() == 2)) {
                        drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListC(), list.get(4).getFpy(), "C", 0);
                    } else {
                        drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListC(), null, "C", 0);
                    }
                }

                if (jsonObject.get("dycssb").equals("1")) {
                    row = sheet.createRow(rowId++);
                    cellId = drawPhaseByName(sheet, rowId, "电压长时闪变", row, cell, 0, list.get(6).getListS(), list.get(6).getFpy(), "T", 0);
                    cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListA(), list.get(6).getFpy(), "A", 0);
                    cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListB(), list.get(6).getFpy(), "B", 0);
                    if (!(overLimitInfo.getMode() == 2)) {
                        drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListC(), list.get(6).getFpy(), "C", 0);
                    } else {
                        drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListC(), null, "C", 0);
                    }
                }
            } else {
                int offset = (length < len) ? len : length; //确定有几行数据

                for (int k = 0; k < offset; k++) {
                    row = sheet.createRow(rowId++);
                    //前半部分
                    if (jsonObject.get("dypc").equals("1")) {
                        cellId = drawPhaseByName(sheet, rowId, "电压偏差", row, cell, 0, list.get(2).getListS(), list.get(2).getFpy(), "T", 0);
                        cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListA(), list.get(2).getFpy(), "A", 0);
                        cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListB(), list.get(2).getFpy(), "B", 0);
                        if (!(overLimitInfo.getMode() == 2)) {
                            cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListC(), list.get(2).getFpy(), "C", 0);
                        } else {
                            cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(2).getListC(), null, "C", 0);
                        }
                        jsonObject.put("dypc", "0"); //画过以后下次这个指标不画了
                    } else if (jsonObject.get("dyxbjbl").equals("1")) {
                        cellId = drawPhaseByName(sheet, rowId, "电压谐波畸变率", row, cell, 0, list.get(4).getListS(), list.get(4).getFpy(), "T", 0);
                        cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListA(), list.get(4).getFpy(), "A", 0);
                        cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListB(), list.get(4).getFpy(), "B", 0);
                        if (!(overLimitInfo.getMode() == 2)) {
                            cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListC(), list.get(4).getFpy(), "C", 0);
                        } else {
                            cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(4).getListC(), null, "C", 0);
                        }
                        jsonObject.put("dyxbjbl", "0"); //画过以后下次这个指标不画了
                    } else if (jsonObject.get("dycssb").equals("1")) {
                        cellId = drawPhaseByName(sheet, rowId, "电压长时闪变", row, cell, 0, list.get(6).getListS(), list.get(6).getFpy(), "T", 0);
                        cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListA(), list.get(6).getFpy(), "A", 0);
                        cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListB(), list.get(6).getFpy(), "B", 0);
                        if (!(overLimitInfo.getMode() == 2)) {
                            cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListC(), list.get(6).getFpy(), "C", 0);
                        } else {
                            cellId = drawPhaseByName(sheet, rowId, null, row, cell, cellId, list.get(6).getListC(), null, "C", 0);
                        }
                        jsonObject.put("dycssb", "0"); //画过以后下次这个指标不画了
                    }

                    //后半部分
                    if (jsonObject.get("sxdybphd").equals("1")) {
                        writeNoPhase(sheet, row, cell, rowId, cellId, list.get(3).getListT(), list.get(3).getFpy(), "三相电压不平衡度");
                        jsonObject.put("sxdybphd", "0"); //画过以后下次这个指标不画了
                    } else if (jsonObject.get("plpc").equals("1")) {
                        writeNoPhase(sheet, row, cell, rowId, cellId, list.get(5).getListT(), list.get(5).getFpy(), "频率偏差");
                        jsonObject.put("plpc", "0"); //画过以后下次这个指标不画了
                    }

                }
            }
        } else { //没有前半部指标
            if (jsonObject.get("sxdybphd").equals("1")) {
                row = sheet.createRow(rowId++); //考虑到在外面创建行是因为前后指标都有的时候，后面指标不需要另外创建行
                writeNoPhase(sheet, row, cell, rowId, cellId, list.get(3).getListT(), list.get(3).getFpy(), "三相电压不平衡度");
            }

            if (jsonObject.get("plpc").equals("1")) {
                row = sheet.createRow(rowId++);
                writeNoPhase(sheet, row, cell, rowId, cellId, list.get(5).getListT(), list.get(5).getFpy(), "频率偏差");
            }
        }

        return rowId;
    }

    public void writeNoPhase(XSSFSheet sheet, XSSFRow row, XSSFCell cell, int rowId, int cellId, List<FpyRateData> list, Integer fpy, String name) {
        //名称
        cell = row.createCell(cellId);
        cell.setCellValue(name);
        cell.setCellStyle(getStyle(4));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
        cellId += 2;
        //合格率限值
        cell = row.createCell(cellId);
        cell.setCellValue(fpy);
        cell.setCellStyle(getStyle(4));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
        cellId += 2;
        //采样点数
        cell = row.createCell(cellId);

        if (list == null) {
            cell.setCellValue("/");
        } else {
            cell.setCellValue(list.get(0).getCount());
        }

        cell.setCellStyle(getStyle(6));
        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 2));
        cellId += 3;
        //合格率
        cell = row.createCell(cellId);

        if (list == null) {
            cell.setCellValue("/");
        } else {
            cell.setCellValue(list.get(0).getRate());
        }

        if (list != null && fpy.doubleValue() > list.get(0).getRate()) {
            cell.setCellStyle(getStyle(5));
        } else {
            cell.setCellStyle(getStyle(6));
        }

        sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 2));
    }

    private int string2Int(JSONObject jsonObject, String value) {
        Integer integer = Integer.parseInt(jsonObject.get(value).toString());
        return integer.intValue();
    }

    public int writeFpyHeader(XSSFSheet sheet, JSONObject jsonObject, int rowId, int type, int length, int len) {
        XSSFRow row = sheet.createRow(rowId++);
        int cellId = 0;
        XSSFCell cell;
        length = ((type == 2 && length > 0) ? 1 : length);

        for (int i = 0; i < length; i++) { //第一行
            //参数
            cell = row.createCell(cellId);
            cell.setCellValue("参数");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + 1));
            cellId += 2;

            //合格率限值
            cell = row.createCell(cellId);
            cell.setCellValue("限值(%)");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + (overLimitInfo.getList().size() == 2 ? 1 : 0)));
            cellId += (overLimitInfo.getList().size() == 2 ? 2 : 1);

            //合格率
            cell = row.createCell(cellId);
            cell.setCellValue("合格率(%)");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + (overLimitInfo.getList().size() == 2 ? 1 : 0)));
            cellId += (overLimitInfo.getList().size() == 2 ? 2 : 1);

            //A相
            cell = row.createCell(cellId);
            cell.setCellValue(((type == 1) ? ((string2Int(jsonObject, "dyhyl") == 1 && i == 0) ? overLimitInfo.getList().get(0) : "A") : overLimitInfo.getList().get(0)) + "相");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
            cellId += 2;

            //B相
            cell = row.createCell(cellId);
            cell.setCellValue(((type == 1) ? ((string2Int(jsonObject, "dyhyl") == 1 && i == 0) ? overLimitInfo.getList().get(1) : "B") : overLimitInfo.getList().get(1)) + "相");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
            cellId += 2;

            //C相
            cell = row.createCell(cellId);
            cell.setCellValue((type == 1) ? (string2Int(jsonObject, "dyhyl") == 1 && i == 0 ? (overLimitInfo.getList().get(2) + ((overLimitInfo.getMode() == 2) ? "" : "相")) : "C相") : (overLimitInfo.getList().get(2) + ((overLimitInfo.getMode() == 2) ? "" : "相")));
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + 1));
            cellId += 2;
        }

        if (type == 2 && len > 0) {
            //参数
            cell = row.createCell(cellId);
            cell.setCellValue("参数");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + 1));
            cellId += 2;

            //合格率限值
            cell = row.createCell(cellId);
            cell.setCellValue("限值(%)");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + 1));
            cellId += 2;

            //采样点数
            cell = row.createCell(cellId);
            cell.setCellValue("采样点数");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + 2));
            cellId += 3;

            //合格率
            cell = row.createCell(cellId);
            cell.setCellValue("合格率(%)");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId, cellId, cellId + 2));
        }

        int newCellId = 0;
        row = sheet.createRow(rowId++);

        for (int i = 0; i < length; i++) { //第二行
            newCellId += 4;

            for (int k = 0; k < 3; k++) {
                if ((string2Int(jsonObject, "dyhyl") == 1 && i == 0 || type == 2) && k == 2 && (overLimitInfo.getMode() == 2)) {
                    //采样点数
                    cell = row.createCell(newCellId);
                    cell.setCellValue("-");
                    cell.setCellStyle(getStyle(4));
                    newCellId++;
                    //合格率
                    cell = row.createCell(newCellId);
                    cell.setCellValue("-");
                    cell.setCellStyle(getStyle(4));
                    newCellId++;
                } else {
                    //采样点数
                    cell = row.createCell(newCellId);
                    cell.setCellValue("采样点数");
                    cell.setCellStyle(getStyle(4));
                    newCellId++;
                    //合格率
                    cell = row.createCell(newCellId);
                    cell.setCellValue("合格率(%)");
                    cell.setCellStyle(getStyle(4));
                    newCellId++;
                }
            }
        }

        return rowId;
    }

    private JSONObject judgeFpy() {
        return JSONObject.fromObject("{'dyhyl':'1','dlhyl':'1','dypc':'1','dyxbjbl':'1','dycssb':'1','sxdybphd':'1','plpc':'1'}");

    }

    private JSONObject judge(String value, Boolean b) {
        return JSONObject.fromObject("{'header':'" + value + "','phase':" + b + ",'max':1,'min':1,'avg':1,'cp95':1,'length':15,'showPass':true}");
    }

    private boolean showPass(List<Integer> list, int v) {
        if (v != -1) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).intValue() == v) {
                    return true;
                }
            }
        }

        return false;
    }

    //构建谐波电压指标的json字符串
    private String constructorVoltage(List<ReportTargetData> list) {
        StringBuilder str = new StringBuilder("{'data':[{'name':'基波','unit':'kV'},");

        for (int i = 0; i < list.size() - 2; i++) {
            /*if (null != list.get(i).getOverLimit()) {*/
            str.append("{'name':'" + (i + 2) + "次','unit':'%'},");
           /* } else {
                str.append("{'name':'" + (i + 2) + "次','unit':'%'},");
            }*/
        }

        str.append("{'name':'电压畸变率','unit':'%'},");
        str.deleteCharAt(str.lastIndexOf(","));
        str.append("]}");

        return str.toString();
    }

    //构建谐波电流指标的json字符串
    private String constructorICurrent(List<ReportTargetData> list) {
        StringBuilder str = new StringBuilder("{'data':[{'name':'基波','unit':'A'},");

        for (int i = 0; i < list.size() - 1; i++) {
            /*if (null != list.get(i).getOverLimit()) {*/
            str.append("{'name':'" + (i + 2) + "次','unit':'A'},");
            /*} else {
                str.append("{'name':'" + (i + 1) + "次','unit':'A'},");
            }*/
        }

        str.deleteCharAt(str.lastIndexOf(","));
        str.append("]}");

        return str.toString();
    }

    public int writeHeader(XSSFSheet sheet, JSONObject jsonObject, int rowId) {
        if (jsonObject == null) {
            return rowId;
        }

        int len = Integer.parseInt(jsonObject.get("length").toString());
        XSSFRow row = sheet.createRow(rowId++);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue(jsonObject.get("header").toString());
        cell.setCellStyle(getStyle(3));
        sheet.createRow(rowId++);
        sheet.addMergedRegion(new CellRangeAddress(rowId - 2, rowId - 1, 0, len));

        int cellId = 0;

        row = sheet.createRow(rowId++);
        cell = row.createCell(cellId++);
        cell.setCellValue("名称");
        cell.setCellStyle(getStyle(4));
        cell = row.createCell(cellId++);
        cell.setCellValue("单位");
        cell.setCellStyle(getStyle(4));

        //根据模板设置导出指标
        int l = overLimitInfo.getList().size();
        Boolean b = jsonObject.get("phase").toString().equals("true");
        b = (jsonObject.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) ? false : b;

        if (jsonObject.get("max").toString().equals("1")) {
            cell = row.createCell(cellId);
            cell.setCellValue("最大值");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + (b ? (l - 1) : 2)));
            cellId += b ? l : 3;
        }

        if (jsonObject.get("min").toString().equals("1")) {
            cell = row.createCell(cellId);
            cell.setCellValue("最小值");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + (b ? (l - 1) : 2)));
            cellId += b ? l : 3;
        }

        if (jsonObject.get("avg").toString().equals("1")) {
            cell = row.createCell(cellId);
            cell.setCellValue("平均值");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + (b ? (l - 1) : 2)));
            cellId += b ? l : 3;
        }

        if (jsonObject.get("cp95").toString().equals("1")) {
            cell = row.createCell(cellId);
            cell.setCellValue("CP95概率大值");
            cell.setCellStyle(getStyle(4));
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId, cellId + (b ? (l - 1) : 2)));
            cellId += b ? l : 3;
        }

        cell = row.createCell(cellId);
        cell.setCellValue("结论");
        cell.setCellStyle(getStyle(4));
        cellId += (b ? (l == 2 ? 3 : 1) : 1);

        cell = row.createCell(cellId);
        cell.setCellValue("国标限值");
        cell.setCellStyle(getStyle(4));

        if (jsonObject.get("phase").toString().equals("true")) {
            row = sheet.createRow(rowId++);

            for (int i = 0; i < 2; i++) {
                cell = row.createCell(i);
                sheet.addMergedRegion(new CellRangeAddress(rowId - 2, rowId - 1, i, i));
            }

            if (jsonObject.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) {
                for (int i = 2; i < len - 1; i += 3) {
                    cell = row.createCell(i);
                    cell.setCellValue("ABC总");
                    cell.setCellStyle(getStyle(4));
                    sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, i, i + 2));
                }
            } else if (jsonObject.get("header").toString().equals("有效值") || jsonObject.get("header").toString().equals("谐波电流幅值")) {
                for (int i = 2; i < len - 1; i += 3) {
                    cell = row.createCell(i);
                    cell.setCellValue("A");
                    cell.setCellStyle(getStyle(4));

                    cell = row.createCell(i + 1);
                    cell.setCellValue("B");
                    cell.setCellStyle(getStyle(4));

                    cell = row.createCell(i + 2);
                    cell.setCellValue("C");
                    cell.setCellStyle(getStyle(4));
                }
            } else {
                for (int i = 2; i < len - (l == 2 ? 5 : 1); i += l) {
                    for (int k = 0; k < overLimitInfo.getList().size(); k++) {
                        cell = row.createCell(i + k);
                        cell.setCellValue(overLimitInfo.getList().get(k));
                        cell.setCellStyle(getStyle(4));
                    }
                }
            }

            for (int i = len - (jsonObject.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0") ? 1 : (l == 2 ? 5 : 1)); i < len + 1; i += (jsonObject.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0") ? 1 : (b ? (l == 2 ? 3 : 1) : 1))) {
                for (int k = 0; k < (jsonObject.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0") ? 1 : (b ? (l == 2 ? 3 : 1) : 1)); k++) {
                    cell = row.createCell(i + k);
                }

                sheet.addMergedRegion(new CellRangeAddress(rowId - 2, rowId - 1, i, i + (jsonObject.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0") ? 0 : (b ? (l == 2 ? 2 : 0) : 0))));
            }
        }

        return rowId;
    }

    private int writeheader(XSSFSheet sheet, JSONObject jsonObject, int rowId,int num) {
        XSSFRow row = sheet.createRow(rowId++);
        int len = Integer.parseInt(jsonObject.get("length").toString());
        XSSFCell cell = row.createCell(0);
        cell.setCellStyle(getStyle(4));
        cell = row.createCell(1);
        cell.setCellStyle(getStyle(4));

        if (jsonObject.get("phase").toString().equals("true")) {
            int l = overLimitInfo.getList().size();

            for (int i = 2; i < len - (l == 2 ? 5 : 1); i += l) {
                cell = row.createCell(i);
                cell.setCellValue(num == 1 ? "AB" : "A");
                cell.setCellStyle(getStyle(4));
                cell = row.createCell(i + 1);
                cell.setCellValue(num == 1 ? "BC" : "B");
                cell.setCellStyle(getStyle(4));

                if (!((overLimitInfo.getMode() == 2)) && num == 1) {
                    cell = row.createCell(i + 2);
                    cell.setCellValue("CA");
                    cell.setCellStyle(getStyle(4));
                } else if (num == 0) {
                    cell = row.createCell(i + 2);
                    cell.setCellValue("C");
                    cell.setCellStyle(getStyle(4));
                } else {
                    cell = row.createCell(i + 2);
                    cell.setCellValue("-");
                    cell.setCellStyle(getStyle(4));
                }
            }
        }

        return rowId;
    }

    private int getCellId(int rowId, XSSFSheet sheet, XSSFRow row, Boolean b, int cellId) {
        if (!b && cellId > 2) {
            row.createCell(cellId++);
            row.createCell(cellId++);
            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId - 3, cellId - 1));
        }

        return cellId;
    }

    private int writeBody(List<ReportTargetData> list, JSONObject jsonPloy, JSONObject jsonObject,
                          int rowId, XSSFSheet sheet) {
        if (jsonPloy == null) {
            return rowId;
        }

        XSSFRow row;
        XSSFCell cell;
        JSONArray jsonArray = (JSONArray) jsonObject.get("data");
        Boolean b = jsonPloy.get("phase").toString().equals("true");
        int kk = (jsonPloy.get("header").toString().equals("三相电压不平衡度") && !overLimitInfo.getMode().equals("0")) ? 4 : 0;
        kk = (jsonPloy.get("header").toString().equals("有效值") && !overLimitInfo.getMode().equals("0")) ? 1 : kk;

        for (int i = kk; i < jsonArray.size(); i++) {
            String str = ((JSONObject) jsonArray.get(i)).get("name").toString();

            if (str.equals("线电压")) {
                rowId = writeheader(sheet, jsonPloy, rowId, 1);//add by sw
            } else if (str.equals("电流畸变率") && !overLimitInfo.getMode().equals("0")) {
                rowId = writeheader(sheet, jsonPloy, rowId, 0);//add by sw
            }

            row = sheet.createRow(rowId++);
            int cellId = 0;
            Double value = null;

            if (null != list.get(i).getOverLimit()) {
                value = new Double(String.valueOf(list.get(i).getOverLimit()));
            }

            cell = row.createCell(cellId++);

            cell.setCellValue(str);
            Boolean b1 = (str.equals("电压偏差") || str.equals("频率偏差"));
            cell.setCellStyle(getStyle(4));
            cell = row.createCell(cellId++);
            cell.setCellValue(((JSONObject) jsonArray.get(i)).get("unit").toString());
            cell.setCellStyle(getStyle(4));

            int length = b ? overLimitInfo.getList().size() : list.get(i).getList().size();

            if (jsonPloy.get("max").toString().equals("1")) {
                for (int k = 0; k < length; k++) {
                    k = ((jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) ? 3 : k);

                    if ((overLimitInfo.getMode() == 2) && k == overLimitInfo.getList().size() - 1 && str.indexOf("电流") == -1 && !jsonPloy.get("header").toString().equals("谐波电流幅值")) {
                        cell = row.createCell(cellId++);
                        cell.setCellValue("-");
                        cell.setCellStyle(getStyle(6));
                    } else {
                        cell = row.createCell(cellId++);
                        Float v = list.get(i).getList().get(k).getFmaxValue();

                        cellId = JudgeData(cell, value, v, cellId, b, rowId, sheet, row, b1);

                        if (jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) {
                            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId - 1, cellId + 1));
                            cellId += 2;
                        }
                    }
                }
            }

            if (jsonPloy.get("min").toString().equals("1")) {
                for (int k = 0; k < length; k++) {
                    k = ((jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) ? 3 : k);

                    if ((overLimitInfo.getMode() == 2) && k == overLimitInfo.getList().size() - 1 && str.indexOf("电流") == -1 && !jsonPloy.get("header").toString().equals("谐波电流幅值")) {
                        cell = row.createCell(cellId++);
                        cell.setCellValue("-");
                        cell.setCellStyle(getStyle(6));
                    } else {
                        cell = row.createCell(cellId++);
                        Float v = list.get(i).getList().get(k).getMinValue();

                        cellId = JudgeData(cell, value, v, cellId, b, rowId, sheet, row, b1);

                        if (jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) {
                            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId - 1, cellId + 1));
                            cellId += 2;
                        }
                    }
                }
            }

            if (jsonPloy.get("avg").toString().equals("1")) {
                for (int k = 0; k < length; k++) {
                    k = ((jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) ? 3 : k);

                    if ((overLimitInfo.getMode() == 2) && k == overLimitInfo.getList().size() - 1 && str.indexOf("电流") == -1 && !jsonPloy.get("header").toString().equals("谐波电流幅值")) {
                        cell = row.createCell(cellId++);
                        cell.setCellValue("-");
                        cell.setCellStyle(getStyle(6));
                    } else {
                        cell = row.createCell(cellId++);
                        Float v = list.get(i).getList().get(k).getMeanValue();

                        cellId = JudgeData(cell, value, v, cellId, b, rowId, sheet, row, b1);

                        if (jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) {
                            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId - 1, cellId + 1));
                            cellId += 2;
                        }
                    }
                }
            }

            if (jsonPloy.get("cp95").toString().equals("1")) {
                for (int k = 0; k < length; k++) {
                    k = ((jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) ? 3 : k);

                    if ((overLimitInfo.getMode() == 2) && k == overLimitInfo.getList().size() - 1 && str.indexOf("电流") == -1 && !jsonPloy.get("header").toString().equals("谐波电流幅值")) {
                        cell = row.createCell(cellId++);
                        cell.setCellValue("-");
                        cell.setCellStyle(getStyle(6));
                    } else {
                        cell = row.createCell(cellId++);
                        Float v = list.get(i).getList().get(k).getCp95Value();

                        cellId = JudgeData(cell, value, v, cellId, b, rowId, sheet, row, b1);

                        if (jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) {
                            sheet.addMergedRegion(new CellRangeAddress(rowId - 1, rowId - 1, cellId - 1, cellId + 1));
                            cellId += 2;
                        }
                    }
                }
            }

            cell = row.createCell(cellId);
            int l = overLimitInfo.getList().size();
            cellId += (jsonPloy.get("header").toString().equals("功率") && !overLimitInfo.getMode().equals("0")) ? 1 : (b ? (l == 2 ? 3 : 1) : 1);

            if (null == list.get(i).getPass() || jsonPloy.get("showPass").toString().equals("false")) {
                cell.setCellValue("/");
                cell.setCellStyle(getStyle(6));
            } else if (-1 == list.get(i).getPass()) {
                cell.setCellValue("不合格");
                cell.setCellStyle(getStyle(5));
            } else {
                cell.setCellValue("合格");
                cell.setCellStyle(getStyle(6));
            }

            cell = row.createCell(cellId++);

            if (null == list.get(i).getOverLimit()) {
                cell.setCellValue("/");
            } else {
                if (b1) {
                    cell.setCellValue("±" + value.toString());
                } else {
                    cell.setCellValue(value.doubleValue());
                }
            }

            cell.setCellStyle(getStyle(4));
        }

        return rowId;
    }

    private int JudgeData(XSSFCell cell, Double value, Float v, int cellId, boolean b, int rowId, XSSFSheet
            sheet, XSSFRow row, Boolean b1) {
        if (null == v) {
            cell.setCellValue("/");
            cellId = getCellId(rowId, sheet, row, b, cellId);
            cell.setCellStyle(getStyle(6));
        } else {
            double cellValue = Double.parseDouble(String.valueOf(v));
            cell.setCellValue(cellValue);
            cellId = getCellId(rowId, sheet, row, b, cellId);

            if (null != value && value.doubleValue() < (b1 ? Math.abs(cellValue) : cellValue)) {
                cell.setCellStyle(getStyle(5));
            } else {
                cell.setCellStyle(getStyle(6));
            }
        }

        return cellId;
    }
}
