package com.ruoyi.growthrate.service.impl;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.ApplicantType;
import com.ruoyi.common.constant.HighValue;
import com.ruoyi.growthrate.service.PatentGrowthrateExcel;
import com.ruoyi.growthrate.vo.PatentGrowthrateDTO;
import com.ruoyi.growthrate.vo.PatentGrowthrateQO;
import com.ruoyi.report.service.impl.AbstractExcelExp;

@Service
public class PatentGrowthrateExcelImpl extends AbstractExcelExp implements PatentGrowthrateExcel {

	private void exportOneContentHvPt(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		SXSSFWorkbook wb = new SXSSFWorkbook(500);
		try {

			// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			// response.setCharacterEncoding("utf-8");
			short headHeight = 30 * 20;
			int minWidth = "四个字宽".getBytes().length * 256;
			String title = "专利增长率报表";

			int dataHeight = 30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();

			Map<String, XSSFCellStyle> styles = createStyles(wb);

			Row titleRow0 = sheet.createRow(0);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(0);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);
			int titleSize = 9;
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(), 0, titleSize));

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeight(headHeight);

			XSSFCellStyle header = styles.get("header");
			createCell(titleRow1, 0, "对比地区", header);
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			CellRangeAddress areaRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 1, 0, 0);
			sheet.addMergedRegion(areaRange);

			createCell(titleRow1, 1, "对比数据", header);
			createCell(titleRow1, 2, "高价值", header);

			createCell(titleRow1, 3, "发明", header);
			CellRangeAddress inventionRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 3,
					titleSize);
			sheet.addMergedRegion(inventionRange);

			createCell(titleRow1, 8, "实用新型", header);
			createCell(titleRow1, 9, "外观设计", header);

			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight(headHeight);
			createCell(titleRow2, 1, "对比周期", header);
			createCell(titleRow2, 2, "总计", header);
			int conumn = 3;
			for (HighValue hv : HighValue.ALL.values()) {
				createCell(titleRow2, conumn++, hv.getTypeName().replace("的有效发明专利", ""), header);
			}
			createCell(titleRow2, conumn++, "/", header);
			createCell(titleRow2, conumn++, "/", header);

			setRegionStyle(sheet, inventionRange, styles.get("header"));

			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);

			int row = 2;
			for (int i = 0; i < list.size(); i++) {
				PatentGrowthrateDTO vo = list.get(i);
				row++;
				Row dataRow = sheet.createRow(row);
				dataRow.setHeightInPoints(dataHeight);

				XSSFCellStyle data = styles.get("data");

				int column = 0;
				createCell(dataRow, column++, vo.getAreaName(), data);
				doCreateCellOneRowPt(dataRow, column, req, vo.getItem1(), data);

				column = 0;
				row++;
				Row dataRow2 = sheet.createRow(row);
				dataRow2.setHeightInPoints(dataHeight);
				createCell(dataRow2, column++, "", data);
				doCreateCellOneRowPt(dataRow2, column, req, vo.getItem2(), data);

				column = 0;
				row++;
				Row dataRow3 = sheet.createRow(row);
				dataRow3.setHeightInPoints(dataHeight);
				createCell(dataRow3, column++, "", data);
				doCreateCellOneRowPt(dataRow3, column, req, vo.getGrowth(), data);

			}

			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth);
		} finally {
			writeToOut(wb, out);
		}
	}

	private void exportOneContentHvAt(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		SXSSFWorkbook wb = new SXSSFWorkbook(500);
		try {
			short headHeight = 30 * 20;
			int minWidth = "五个字的宽".getBytes().length * 256;
			String title = "专利增长率报表";

			int dataHeight = 30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();

			Map<String, XSSFCellStyle> styles = createStyles(wb);

			Row titleRow0 = sheet.createRow(0);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(0);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);
			int titleSize = 32;
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(), 0, titleSize));

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeight(headHeight);

			XSSFCellStyle header = styles.get("header");
			createCell(titleRow1, 0, "对比地区", header);
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			CellRangeAddress areaRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 1, 0, 0);
			sheet.addMergedRegion(areaRange);

			createCell(titleRow1, 1, "对比数据", header);
			createCell(titleRow1, 2, "高价值", header);
			
			int column=3;
			for(ApplicantType at:ApplicantType.ALL.values()) {
				createCell(titleRow1, column, at.getTypeName(), header);
				CellRangeAddress inventionRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), column, column+5);
				sheet.addMergedRegion(inventionRange);
				column=column+5+1;
				setRegionStyle(sheet, inventionRange, styles.get("header"));
			}

			
			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight((short)(headHeight/2*3));
			
			createCell(titleRow2, 1, "对比周期", header);
			createCell(titleRow2, 2, "总计", header);
			column=3;
			XSSFCellStyle header2 = styles.get("header2");
			for(ApplicantType at:ApplicantType.ALL.values()) {
				createCell(titleRow2, column++, "合计", header);
				for(HighValue hv:HighValue.ALL.values()) {
					createCell(titleRow2, column++, hv.getTypeName().replace("的有效发明专利", ""), header2);
				}
			}
			
			
			setRegionStyle(sheet, areaRange, styles.get("header"));
			
			

			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);

			int row = 2;
			for (int i = 0; i < list.size(); i++) {
				PatentGrowthrateDTO vo = list.get(i);
				row++;
				Row dataRow = sheet.createRow(row);
				dataRow.setHeightInPoints(dataHeight);

				XSSFCellStyle data = styles.get("data");

				column = 0;
				createCell(dataRow, column++, vo.getAreaName(), data);
				doCreateCellOneRowAt(dataRow, column, req, vo.getItem1(), data);

				column = 0;
				row++;
				Row dataRow2 = sheet.createRow(row);
				dataRow2.setHeightInPoints(dataHeight);
				createCell(dataRow2, column++, "", data);
				doCreateCellOneRowAt(dataRow2, column, req, vo.getItem2(), data);

				column = 0;
				row++;
				Row dataRow3 = sheet.createRow(row);
				dataRow3.setHeightInPoints(dataHeight);
				createCell(dataRow3, column++, "", data);
				doCreateCellOneRowAt(dataRow3, column, req, vo.getGrowth(), data);

			}
			
			autoColumnWidthForChineseChar(sheet, 0, titleSize+1, minWidth,minWidth);
			sheet.setColumnWidth(1, minWidth/2*3);
		} finally {
			writeToOut(wb, out);
		}
	}

	private void exportOneContentHv(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		if ("1".equals(req.getCpContents())) {// 专利类型
			exportOneContentHvPt(out, req, list);
		} else {
			exportOneContentHvAt(out, req, list);
		}

	}

	private void exportAllContentHv(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		SXSSFWorkbook wb = new SXSSFWorkbook(500);
		try {

			// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			// response.setCharacterEncoding("utf-8");
			short headHeight = 30 * 20;
			int minWidth = "四个字宽".getBytes().length * 256;
			String title = "专利增长率报表";

			int dataHeight = 30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();

			Map<String, XSSFCellStyle> styles = createStyles(wb);

			Row titleRow0 = sheet.createRow(0);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(0);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);

			int titleSize = 31;

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(),
					titleRow0.getRowNum(), titleSize - 2));

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeight(headHeight);
			XSSFCellStyle header = styles.get("header");
			createCell(titleRow1, 0, "对比地区", header);
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			CellRangeAddress areaRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 2, 0, 0);
			sheet.addMergedRegion(areaRange);

			createCell(titleRow1, 1, "对比数据", header);
			createCell(titleRow1, 2, "高价值", header);
			createCell(titleRow1, 3, "发明", header);
			CellRangeAddress inventionRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 3, 27);
			sheet.addMergedRegion(inventionRange);
			createCell(titleRow1, 28, "实用新型", header);
			createCell(titleRow1, 29, "外观设计", header);

			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight(headHeight);
			createCell(titleRow2, 1, "对比周期", header);
			CellRangeAddress tRange1 = new CellRangeAddress(titleRow2.getRowNum(), titleRow2.getRowNum() + 1, 1, 1);
			sheet.addMergedRegion(tRange1);

			CellRangeAddress tRange2 = new CellRangeAddress(titleRow2.getRowNum(), titleRow2.getRowNum() + 1, 2, 2);
			sheet.addMergedRegion(tRange2);

			createCell(titleRow2, 2, "总计", header);

			int column = 2;
			for (HighValue hv : HighValue.ALL.values()) {
				column++;
				createCell(titleRow2, column, hv.getTypeName().replace("的有效发明专利", ""), header);
				CellRangeAddress tRange = new CellRangeAddress(titleRow2.getRowNum(), titleRow2.getRowNum(), column,
						column + 4);
				sheet.addMergedRegion(tRange);
				setRegionStyle(sheet, tRange, styles.get("header"));
				column = column + 4;
			}
			createCell(titleRow2, 28, "/", header);
			createCell(titleRow2, 29, "/", header);

			Row titleRow3 = sheet.createRow(3);
			titleRow3.setHeight(headHeight);
			for (int j = 0; j < 5; j++) {
//				int c = 3 + j * 6;
				int c = 3 + j * 5;
//				createCell(titleRow3, c++, "合计", header);
				createCell(titleRow3, c++, "大专院校", header);
				createCell(titleRow3, c++, "科研机构", header);
				createCell(titleRow3, c++, "企业", header);
				createCell(titleRow3, c++, "机关团体", header);
				createCell(titleRow3, c++, "个人", header);
			}

			createCell(titleRow3, 28, "/", header);
			createCell(titleRow3, 29, "/", header);

			setRegionStyle(sheet, tRange1, styles.get("header"));
			setRegionStyle(sheet, tRange2, styles.get("header"));
			setRegionStyle(sheet, areaRange, styles.get("header"));

			// 冻结
			sheet.createFreezePane(0, 4, 0, 4);
			int row = 3;
			for (int i = 0; i < list.size(); i++) {
				PatentGrowthrateDTO vo = list.get(i);
				row++;
				Row dataRow = sheet.createRow(row);
				dataRow.setHeightInPoints(dataHeight);

				XSSFCellStyle data = styles.get("data");

				column = 0;
				createCell(dataRow, column++, vo.getAreaName(), data);
				doCreateCellOneRowHv(dataRow, column, vo.getItem1(), data);

				column = 0;
				row++;
				Row dataRow2 = sheet.createRow(row);
				dataRow2.setHeightInPoints(dataHeight);
				createCell(dataRow2, column++, "", data);
				doCreateCellOneRowHv(dataRow2, column, vo.getItem2(), data);

				column = 0;
				row++;
				Row dataRow3 = sheet.createRow(row);
				dataRow3.setHeightInPoints(dataHeight);
				createCell(dataRow3, column++, "", data);
				doCreateCellOneRowHv(dataRow3, column, vo.getGrowth(), data);
			}

			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth);
		} finally {
			writeToOut(wb, out);
		}
	}

	private void exportOneContentActOrAuth(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		String cpData = req.getCpData();
		String cpDataStr = cpData;
		if ("act".equals(cpData)) {
			cpDataStr = "有效";
		} else if ("auth".equals(cpData)) {
			cpDataStr = "授权";
		}

		SXSSFWorkbook wb = new SXSSFWorkbook(500);
		try {

			// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			// response.setCharacterEncoding("utf-8");
			short headHeight = 30 * 20;
			int minWidth = "四个字宽".getBytes().length * 256;
			String title = "专利增长率报表";

			int dataHeight = 30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();

			Map<String, XSSFCellStyle> styles = createStyles(wb);

			Row titleRow0 = sheet.createRow(0);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(0);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);
			int titleSize = 5;
			if ("1".equals(req.getCpContents())) {// 专利类型
				titleSize = 5;
			} else {
				titleSize = 7;
			}

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(), 0, titleSize));

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeight(headHeight);

			XSSFCellStyle header = styles.get("header");
			createCell(titleRow1, 0, "对比地区", header);
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			CellRangeAddress areaRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 1, 0, 0);
			sheet.addMergedRegion(areaRange);

			createCell(titleRow1, 1, "对比数据", header);

			createCell(titleRow1, 2, cpDataStr, header);

			createCell(titleRow1, 3, "", header);
			CellRangeAddress inventionRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 3,
					titleSize);
			sheet.addMergedRegion(inventionRange);

			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight(headHeight);
			createCell(titleRow2, 1, "对比周期", header);
			createCell(titleRow2, 2, "总计", header);
			if ("1".equals(req.getCpContents())) {// 专利类型
				createCell(titleRow2, 3, "发明", header);
				createCell(titleRow2, 4, "实用新型", header);
				createCell(titleRow2, 5, "外观设计", header);
			} else {
				createCell(titleRow2, 3, "大专院校", header);
				createCell(titleRow2, 4, "科研机构", header);
				createCell(titleRow2, 5, "企业", header);
				createCell(titleRow2, 6, "机关团体", header);
				createCell(titleRow2, 7, "个人", header);
			}

			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);

			int row = 2;
			for (int i = 0; i < list.size(); i++) {
				PatentGrowthrateDTO vo = list.get(i);
				row++;
				Row dataRow = sheet.createRow(row);
				dataRow.setHeightInPoints(dataHeight);

				XSSFCellStyle data = styles.get("data");

				int column = 0;
				createCell(dataRow, column++, vo.getAreaName(), data);
				doCreateCellOneRow1(dataRow, column, req, vo.getItem1(), data);

				column = 0;
				row++;
				Row dataRow2 = sheet.createRow(row);
				dataRow2.setHeightInPoints(dataHeight);
				createCell(dataRow2, column++, "", data);
				doCreateCellOneRow1(dataRow2, column, req, vo.getItem2(), data);

				column = 0;
				row++;
				Row dataRow3 = sheet.createRow(row);
				dataRow3.setHeightInPoints(dataHeight);
				createCell(dataRow3, column++, "", data);
				doCreateCellOneRow1(dataRow3, column, req, vo.getGrowth(), data);

			}

			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth);
		} finally {
			writeToOut(wb, out);
		}
	}
	
	
	private void doCreateCellOneRowPt(Row dataRow, int column, PatentGrowthrateQO req, Map<String, String> item,
			XSSFCellStyle data) {
		createCell(dataRow, column++, item.get("timeReg"), data);
		createCell(dataRow, column++, item.get("total_cnt"), data);
		createCell(dataRow, column++, item.get("hv_seis_press"), data);
		createCell(dataRow, column++, item.get("hv_keep10_press"), data);
		createCell(dataRow, column++, item.get("hv_have_overseas_press"), data);
		createCell(dataRow, column++, item.get("hv_high_pledge_press"), data);
		createCell(dataRow, column++, item.get("hv_technology_award_press"), data);
		createCell(dataRow, column++, "/", data);
		createCell(dataRow, column++, "/", data);
		
	}
	
	private void doCreateCellOneRowAt(Row dataRow, int column, PatentGrowthrateQO req, Map<String, String> item,
			XSSFCellStyle data) {
		createCell(dataRow, column++, item.get("timeReg"), data);
		createCell(dataRow, column++, item.get("all_total_cnt"), data);
		

		createCell(dataRow, column++, item.get("UC_total_cnt"), data);
		createCell(dataRow, column++, item.get("UC_hv_seis_press"), data);
		createCell(dataRow, column++, item.get("UC_hv_keep10_press"), data);
		createCell(dataRow, column++, item.get("UC_hv_have_overseas_press"), data);
		createCell(dataRow, column++, item.get("UC_hv_high_pledge_press"), data);
		createCell(dataRow, column++, item.get("UC_hv_technology_award_press"), data);
		
		
		createCell(dataRow, column++, item.get("SRI_total_cnt"), data);
		createCell(dataRow, column++, item.get("SRI_hv_seis_press"), data);
		createCell(dataRow, column++, item.get("SRI_hv_keep10_press"), data);
		createCell(dataRow, column++, item.get("SRI_hv_have_overseas_press"), data);
		createCell(dataRow, column++, item.get("SRI_hv_high_pledge_press"), data);
		createCell(dataRow, column++, item.get("SRI_hv_technology_award_press"), data);
		
		createCell(dataRow, column++, item.get("IME_total_cnt"), data);
		createCell(dataRow, column++, item.get("IME_hv_seis_press"), data);
		createCell(dataRow, column++, item.get("IME_hv_keep10_press"), data);
		createCell(dataRow, column++, item.get("IME_hv_have_overseas_press"), data);
		createCell(dataRow, column++, item.get("IME_hv_high_pledge_press"), data);
		createCell(dataRow, column++, item.get("IME_hv_technology_award_press"), data);
		
		createCell(dataRow, column++, item.get("OT_total_cnt"), data);
		createCell(dataRow, column++, item.get("OT_hv_seis_press"), data);
		createCell(dataRow, column++, item.get("OT_hv_keep10_press"), data);
		createCell(dataRow, column++, item.get("OT_hv_have_overseas_press"), data);
		createCell(dataRow, column++, item.get("OT_hv_high_pledge_press"), data);
		createCell(dataRow, column++, item.get("OT_hv_technology_award_press"), data);
		
		createCell(dataRow, column++, item.get("PE_total_cnt"), data);
		createCell(dataRow, column++, item.get("PE_hv_seis_press"), data);
		createCell(dataRow, column++, item.get("PE_hv_keep10_press"), data);
		createCell(dataRow, column++, item.get("PE_hv_have_overseas_press"), data);
		createCell(dataRow, column++, item.get("PE_hv_high_pledge_press"), data);
		createCell(dataRow, column++, item.get("PE_hv_technology_award_press"), data);
		
		
	}
	private void doCreateCellOneRow1(Row dataRow, int column, PatentGrowthrateQO req, Map<String, String> item,
			XSSFCellStyle data) {
		createCell(dataRow, column++, item.get("timeReg"), data);
		createCell(dataRow, column++, item.get("total_cnt"), data);
		if ("1".equals(req.getCpContents())) {// 专利类型
			createCell(dataRow, column++, item.get("invention_cnt"), data);
			createCell(dataRow, column++, item.get("modelUtility_cnt"), data);
			createCell(dataRow, column++, item.get("appearance_cnt"), data);

		} else {
			createCell(dataRow, column++, item.get("higher_school"), data);
			createCell(dataRow, column++, item.get("research_institute"), data);
			createCell(dataRow, column++, item.get("enterprise"), data);
			createCell(dataRow, column++, item.get("institutions"), data);
			createCell(dataRow, column++, item.get("individual"), data);
		}
	}

	private void exportAllContentActOrAuth(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		String cpData = req.getCpData();
		String cpDataStr = cpData;
		if ("act".equals(cpData)) {
			cpDataStr = "有效";
		} else if ("auth".equals(cpData)) {
			cpDataStr = "授权";
		}

		SXSSFWorkbook wb = new SXSSFWorkbook(500);
		try {

			// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			// response.setCharacterEncoding("utf-8");
			short headHeight = 30 * 20;
			int minWidth = "四个字宽".getBytes().length * 256;
			String title = "专利增长率报表";

			int dataHeight = 30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();

			Map<String, XSSFCellStyle> styles = createStyles(wb);

			Row titleRow0 = sheet.createRow(0);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(0);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);
			int titleSize = 22;
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(), 0, titleSize - 2));

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeight(headHeight);

			XSSFCellStyle header = styles.get("header");
			createCell(titleRow1, 0, "对比地区", header);
			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			CellRangeAddress areaRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 1, 0, 0);
			sheet.addMergedRegion(areaRange);

			createCell(titleRow1, 1, "对比数据", header);

			createCell(titleRow1, 2, cpDataStr, header);

			createCell(titleRow1, 3, "发明", header);
			CellRangeAddress inventionRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 3, 8);
			sheet.addMergedRegion(inventionRange);

			createCell(titleRow1, 9, "实用新型", header);
			CellRangeAddress modelUtilityRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 9,
					14);
			sheet.addMergedRegion(modelUtilityRange);

			createCell(titleRow1, 15, "外观设计", header);
			CellRangeAddress appearanceRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 15,
					20);
			sheet.addMergedRegion(appearanceRange);

			setRegionStyle(sheet, areaRange, styles.get("header"));
			setRegionStyle(sheet, inventionRange, styles.get("header"));
			setRegionStyle(sheet, modelUtilityRange, styles.get("header"));
			setRegionStyle(sheet, appearanceRange, styles.get("header"));

			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight(headHeight);
			createCell(titleRow2, 1, "对比周期", header);
			createCell(titleRow2, 2, "总计", header);
			for (int j = 0; j < 3; j++) {
				int c = 3 + j * 6;
				createCell(titleRow2, c++, "合计", header);
				createCell(titleRow2, c++, "大专院校", header);
				createCell(titleRow2, c++, "科研机构", header);
				createCell(titleRow2, c++, "企业", header);
				createCell(titleRow2, c++, "机关团体", header);
				createCell(titleRow2, c++, "个人", header);
			}

//			TODO	
			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);
			int row = 2;
			for (int i = 0; i < list.size(); i++) {
				PatentGrowthrateDTO vo = list.get(i);
				row++;
				Row dataRow = sheet.createRow(row);
				dataRow.setHeightInPoints(dataHeight);

				XSSFCellStyle data = styles.get("data");

				int column = 0;
				createCell(dataRow, column++, vo.getAreaName(), data);
				doCreateCellOneRow(dataRow, column, vo.getItem1(), data);

				column = 0;
				row++;
				Row dataRow2 = sheet.createRow(row);
				dataRow2.setHeightInPoints(dataHeight);
				createCell(dataRow2, column++, "", data);
				doCreateCellOneRow(dataRow2, column, vo.getItem2(), data);

				column = 0;
				row++;
				Row dataRow3 = sheet.createRow(row);
				dataRow3.setHeightInPoints(dataHeight);
				createCell(dataRow3, column++, "", data);
				doCreateCellOneRow(dataRow3, column, vo.getGrowth(), data);
			}

			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth);

		} finally {
			writeToOut(wb, out);
		}

	}

	private void doCreateCellOneRowHv(Row dataRow, int column, Map<String, String> item, XSSFCellStyle sty) {
		createCell(dataRow, column++, item.get("timeReg"), sty);
		createCell(dataRow, column++, item.get("all_total_cnt"), sty);

		createCell(dataRow, column++, item.get("hv_seis_press_UC"), sty);
		createCell(dataRow, column++, item.get("hv_seis_press_SRI"), sty);
		createCell(dataRow, column++, item.get("hv_seis_press_IME"), sty);
		createCell(dataRow, column++, item.get("hv_seis_press_OT"), sty);
		createCell(dataRow, column++, item.get("hv_seis_press_PE"), sty);

		createCell(dataRow, column++, item.get("hv_keep10_press_UC"), sty);
		createCell(dataRow, column++, item.get("hv_keep10_press_SRI"), sty);
		createCell(dataRow, column++, item.get("hv_keep10_press_IME"), sty);
		createCell(dataRow, column++, item.get("hv_keep10_press_OT"), sty);
		createCell(dataRow, column++, item.get("hv_keep10_press_PE"), sty);

		createCell(dataRow, column++, item.get("hv_have_overseas_press_UC"), sty);
		createCell(dataRow, column++, item.get("hv_have_overseas_press_SRI"), sty);
		createCell(dataRow, column++, item.get("hv_have_overseas_press_IME"), sty);
		createCell(dataRow, column++, item.get("hv_have_overseas_press_OT"), sty);
		createCell(dataRow, column++, item.get("hv_have_overseas_press_PE"), sty);

		createCell(dataRow, column++, item.get("hv_high_pledge_press_UC"), sty);
		createCell(dataRow, column++, item.get("hv_high_pledge_press_SRI"), sty);
		createCell(dataRow, column++, item.get("hv_high_pledge_press_IME"), sty);
		createCell(dataRow, column++, item.get("hv_high_pledge_press_OT"), sty);
		createCell(dataRow, column++, item.get("hv_high_pledge_press_PE"), sty);

		createCell(dataRow, column++, item.get("hv_technology_award_press_UC"), sty);
		createCell(dataRow, column++, item.get("hv_technology_award_press_SRI"), sty);
		createCell(dataRow, column++, item.get("hv_technology_award_press_IME"), sty);
		createCell(dataRow, column++, item.get("hv_technology_award_press_OT"), sty);
		createCell(dataRow, column++, item.get("hv_technology_award_press_PE"), sty);

		createCell(dataRow, column++, "/", sty);
		createCell(dataRow, column++, "/", sty);

	}

	private void doCreateCellOneRow(Row dataRow, int column, Map<String, String> item, XSSFCellStyle sty) {
		createCell(dataRow, column++, item.get("timeReg"), sty);
		createCell(dataRow, column++, item.get("all_total_cnt"), sty);
		createCell(dataRow, column++, item.get("invention_total_cnt"), sty);
		createCell(dataRow, column++, item.get("invention_higher_school"), sty);
		createCell(dataRow, column++, item.get("invention_research_institute"), sty);
		createCell(dataRow, column++, item.get("invention_enterprise"), sty);
		createCell(dataRow, column++, item.get("invention_institutions"), sty);
		createCell(dataRow, column++, item.get("invention_individual"), sty);

		createCell(dataRow, column++, item.get("modelUtility_total_cnt"), sty);
		createCell(dataRow, column++, item.get("modelUtility_higher_school"), sty);
		createCell(dataRow, column++, item.get("modelUtility_research_institute"), sty);
		createCell(dataRow, column++, item.get("modelUtility_enterprise"), sty);
		createCell(dataRow, column++, item.get("modelUtility_institutions"), sty);
		createCell(dataRow, column++, item.get("modelUtility_individual"), sty);

		createCell(dataRow, column++, item.get("appearance_total_cnt"), sty);
		createCell(dataRow, column++, item.get("appearance_higher_school"), sty);
		createCell(dataRow, column++, item.get("appearance_research_institute"), sty);
		createCell(dataRow, column++, item.get("appearance_enterprise"), sty);
		createCell(dataRow, column++, item.get("appearance_institutions"), sty);
		createCell(dataRow, column++, item.get("appearance_individual"), sty);
	}

	private void exportAllContent(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		String cpData = req.getCpData();
		if ("hv".equals(cpData)) {
			exportAllContentHv(out, req, list);
			return;
		} else {
			exportAllContentActOrAuth(out, req, list);
			return;
		}
	}

	private void exportOneContent(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {
		String cpData = req.getCpData();
		if ("hv".equals(cpData)) {
			exportOneContentHv(out, req, list);
			return;
		} else {
			exportOneContentActOrAuth(out, req, list);
			return;
		}
	}

	@Override
	public void doExport(OutputStream out, PatentGrowthrateQO req, List<PatentGrowthrateDTO> list) {

		String[] cpContents = req.getCpContents().split(",");
		if (cpContents.length == 2) {//对比内容同时选择了 专利类型 和  申请人类型
			exportAllContent(out, req, list);
			return;
		} else {
			exportOneContent(out, req, list);
			return;

		}

	}

}
