/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.excel;

import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.util.CellRangeAddressList;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;

/**
 * OD解析配置导出模板，设置下拉框选项
 * @author 张波 create 2020年10月12日上午9:46:27
 */
public class ExcelSheetWriteHandler implements SheetWriteHandler {
	@Override
	public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

	}

	@Override
	public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
		selectList(writeWorkbookHolder,writeSheetHolder,2,2,new String[] { "主产品", "附属产品" });
		selectList(writeWorkbookHolder,writeSheetHolder,6,6,new String[] { "横表", "纵表" });
		selectList(writeWorkbookHolder,writeSheetHolder,14,14,new String[] { "有效", "无效" });
	}
	
	/**
	 * 设置Excel下拉框选项
	 * @param writeWorkbookHolder
	 * @param writeSheetHolder
	 * @param firstCol
	 * @param lastCol
	 * @param strings
	 */
	public static void selectList(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder,int firstCol,int lastCol,String[] strings ){
		// 设置下拉
		CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 1000, firstCol, lastCol);
		DataValidationHelper helper = writeSheetHolder.getSheet().getDataValidationHelper();
		DataValidationConstraint constraint = helper.createExplicitListConstraint(strings);
		DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
		writeSheetHolder.getSheet().addValidationData(dataValidation);	
	}
	
}
