题目数据下拉选工具类如下:
/** * * excel导出,有码值的数据应用下拉框展现。解决下拉框最多255个字符的问题。 * * 原理为新建一个暗藏状态的sheet页,用来存储下拉框的值。 * * @param wb 工作簿 HSSFWorkbook * * @param col 以后列名 * * @param boxMap 码值汇合 * * @param rows 失常sheet页数据,用来指定哪些行须要增加下拉框 * * @param i 多个码值须要增加下拉,暗藏状态的sheet页名称不能反复,增加i值辨别。 * * @param colToIndex 用来指定哪些列须要增加下拉框 * * @return dataValidation */ public static HSSFDataValidation createBox1(HSSFWorkbook wb, String col, Map<String, String> boxMap, int rows, int i, int colToIndex) { HSSFDataValidation dataValidation = null; String cols = ""; //查问码值汇合,获取当前列的码值。 if (null != boxMap.get(col)) { cols = boxMap.get(col); } //新建暗藏状态的sheet,用来存储码值。 if (cols.length() > 0 && null != cols) { String str[] = cols.split(","); //创立sheet页 HSSFSheet sheet = wb.createSheet("hidden" + i); //向创立的sheet页增加码值数据。 for (int i1 = 0; i1 < str.length; i1++) { HSSFRow row = sheet.createRow(i1); HSSFCell cell = row.createCell((int) 0); cell.setCellValue(str[i1]); } //将码值sheet页做成excel公式 Name namedCell = wb.createName(); namedCell.setNameName("hidden" + i); namedCell.setRefersToFormula("hidden" + i + "!$A$1:$A$" + str.length); //确定要在哪些单元格生成下拉框 DVConstraint dvConstraint = DVConstraint.createFormulaListConstraint("hidden" + i); CellRangeAddressList regions = new CellRangeAddressList(1, rows, colToIndex, colToIndex); dataValidation = new HSSFDataValidation(regions, dvConstraint); //暗藏码值sheet页 int sheetNum = wb.getNumberOfSheets(); for (int n = 1; n < sheetNum; n++) { wb.setSheetHidden(n, true); } } return dataValidation; } /** * * excel导出,有码值的数据应用下拉框展现。 * * @param col 列名 * * @param boxMap 码值汇合 * * @param firstRow 插入下拉框开始行号 * * @param lastRow 插入下拉框完结行号 * * @param firstCol 插入下拉框开始列号 * * @param lastCol 插入下拉框完结行号 * * @return */ public static HSSFDataValidation createBox(String col, Map<String, List<String>> boxMap, int firstRow, int lastRow, int firstCol, int lastCol) { HSSFDataValidation dataValidation = null; //查问码值表 List<String> cols = new ArrayList<>(); if (null != boxMap.get(col)) { cols = boxMap.get(col); } //设置下拉框 if (cols.size() > 0 && null != cols) { //list转数组 String[] str = cols.toArray(new String[cols.size()]); //指定0-9行,0-0列为下拉框 CellRangeAddressList cas = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); //创立下拉数据列 DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(str); //将下拉数据放入下拉框 dataValidation = new HSSFDataValidation(cas, dvConstraint); } return dataValidation; } //设置款式 public static HSSFCellStyle createCellStyle(HSSFCellStyle cellStyle, HSSFFont font, Boolean flag, HSSFRow row) { //设置边框 //下 cellStyle.setBorderBottom(BorderStyle.THIN); //左 cellStyle.setBorderLeft(BorderStyle.THIN); //上 cellStyle.setBorderTop(BorderStyle.THIN); //右 cellStyle.setBorderRight(BorderStyle.THIN); //垂直居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置大小 cellStyle.setFont(font); //背景色填充整个单元格 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充背景色 cellStyle.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); return cellStyle; }
//创立HSSFWorkbook对象 HSSFWorkbook workbook = new HSSFWorkbook(); //创立sheet对象 HSSFSheet sheet = workbook.createSheet(); //列名款式 HSSFFont colFont = workbook.createFont(); colFont.setFontName("宋体"); colFont.setFontHeightInPoints((short) 10);//字体大小 //设置表格列宽 sheet.setDefaultColumnWidth(10); sheet.setColumnWidth(0, 6000); sheet.setColumnWidth(1, 6000); sheet.setColumnWidth(2, 6000); sheet.setColumnWidth(3, 6000); sheet.setColumnWidth(4, 6000); //第一行表头 HSSFRow row = sheet.createRow(0); HSSFCellStyle style = ExcelUtil.createCellStyle(workbook.createCellStyle(), colFont, true, row); sheet.setDefaultColumnStyle(0, style);