关于后端:Excel文件的标题数据下拉选

10次阅读

共计 3227 个字符,预计需要花费 9 分钟才能阅读完成。

题目数据下拉选工具类如下:

 /**
     * *  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);
正文完
 0