package xiaoqu.home.open.utils;

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

import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddressList;

import com.alibaba.fastjson.JSON;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import xiaoqu.home.open.controller.input.grid.GridReadFileModel;

public final class ExcelSelectListUtil {

	/**
	 * firstRow 開始行號 根据此项目，默认为2(下标0开始) lastRow 根据此项目，默认为最大65535 firstCol
	 * 区域中第一个单元格的列号 (下标0开始) lastCol 区域中最后一个单元格的列号 strings 下拉内容
	 */
	public static void selectList(Workbook workbook, int firstCol, int lastCol, String[] strings) {

		Sheet sheet = workbook.getSheetAt(0);
		// 生成下拉列表
		// 只对(x，x)单元格有效
		CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 65535, firstCol, lastCol);
		// 生成下拉框内容
		DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(strings);
		HSSFDataValidation dataValidation = new HSSFDataValidation(cellRangeAddressList, dvConstraint);
		// 对sheet页生效
		sheet.addValidationData(dataValidation);

	}

	public static void main(String[] args) throws Exception {
		ImportParams params = new ImportParams();
//		params.setStartRows(2);
		params.setTitleRows(2);
		List<GridReadFileModel> result = ExcelImportUtil.importExcel(new File("C:\\eclipse\\xiaoqu-intellectual-web\\logs\\模板.xlsx"), GridReadFileModel.class, params);
		for (GridReadFileModel key : result) {
			System.out.println(JSON.toJSONString(key));
		}
	}

	// ***********excel导出测试
//	public static void main(String[] args) throws IOException {
//		ExportParams params = new ExportParams("导入模板", "测试");
//		List<GridBatchInsertInput> list = new ArrayList<GridBatchInsertInput>();
//		for (int i = 0; i < 10; i++) {
//			GridBatchInsertInput GridBatchInsertInput = new GridBatchInsertInput();
//			GridBatchInsertInput.setDistrictId(1L);
//			GridBatchInsertInput.setGridLevel("1");
//			GridBatchInsertInput.setName("测试");
//			GridBatchInsertInput.setSuperiorName("测试");
//			list.add(GridBatchInsertInput);
//		}
//		Workbook workbook = ExcelExportUtil.exportExcel(params, GridBatchInsertInput.class, list);
//		// 这里是自己加的 带下拉框的代码
//		ExcelSelectListUtil.selectList(workbook, 1, 1, new String[] { "斤", "两", "个", "袋", "份" });
////			ExcelSelectListUtil.selectList(workbook, 0, 0, new String[]{"蔬菜","水果","主食","熟食","调料"});
//		File savefile = new File("c:/excel/");
//		if (!savefile.exists()) {
//			savefile.mkdirs();
//		}
//		FileOutputStream fos = new FileOutputStream("c:/excel/ExcelExportBigData.bigDataExport.xlsx");
//		workbook.write(fos);
//		fos.close();
//	}

}
