【POI-EXCEL-下拉框】POI导出excel下拉框数据太多导致下拉框不显示BUG修复

RT

最近在线上遇到一个很难受的BUG,我一度以为是我代码逻辑出了问题,用了Arthas定位分析之后,开始坚定了信心:大概率是POI的API有问题,比如写入数据过多。
在这里插入图片描述
PS:上图为正常的下拉框。但是,当下拉选项过多时(跟多少无关,而是跟字节数有关),会导致下拉框内容显示失败

解决办法

功夫不负有心人,根据百度找到了这篇文章《Java POI 利用隐藏sheet实现导出下拉数据太多为空》。

解决思路是:

  1. 使用隐藏sheet来实现下拉框,抛弃旧有的方法

翻译成我这边的代码之后,如下:

创建下拉框单例:

import cn.hutool.core.collection.CollectionUtil;
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.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint;
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author feng.zhang
 * @since 2023-08-17 12:28
 */
public class CommonExcelUtil {

    /**
     * 给列加下拉选项
     *
     * @param sheet            sheet页
     * @param hiddentSheetName 隐藏sheet名
     * @param colName          需要绑定的列名
     * @param textList         具体下拉框内容,如   String[] textList = {"男","女};
     * @param firstRow         起始行(0起算第一行)
     * @param endRow           结束行(0起算第一行)
     * @param firstCol         起始列(0起算第一列)
     * @param endCol           结束列(0起算第一列)
     **/
    public static void createBox(Sheet sheet, String hiddentSheetName, String colName, String[] textList, int firstRow, int endRow, int firstCol, int endCol) {
        List<String> phaseNames = new ArrayList<>(Arrays.asList(textList));
        int row = CollectionUtil.isEmpty(phaseNames) ? 1 : phaseNames.size();
        String strFormula = hiddentSheetName + "!$" + colName + "$1:$" + colName + "$" + row;
        XSSFDataValidationConstraint hiddentConstraint = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST, strFormula);

        // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
        CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);

        // 数据有效性对象
        DataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet) sheet);
        DataValidation validation = help.createValidation(hiddentConstraint, regions);
        sheet.addValidationData(validation);
    }
}

调用处代码:

private void setTopLevel(Workbook workbook) {
        String[] array = getDataArray();

        // 创建隐藏sheet
        final String hiddenSheetName = "hiddenSheetA";
        final String colName = "A";
        if (array.length > 0) {
            workbook.createSheet(hiddenSheetName);
            workbook.setSheetHidden(workbook.getSheetIndex(workbook.getSheet(hiddenSheetName)), true);
            Sheet sheet = workbook.getSheet(hiddenSheetName);

            //sheet.getLastRowNum无法区分 有一行和没有 所以这里先建一行
            sheet.createRow(0);

            int colNum = Integer.valueOf(colName.charAt(0)) - 65;
            Row row; //创建数据行
            sheet.setColumnWidth(colNum, 4000); //设置每列的列宽
            for (int j = 0; j < array.length; j++) {
                if (sheet.getLastRowNum() < j) {
                    row = sheet.createRow(j); //创建数据行
                } else {
                    row = sheet.getRow(j);
                }
                //设置对应单元格的值
                row.createCell(colNum).setCellValue(array[j]);
            }
        }

        CommonExcelUtil.createBox(workbook.getSheetAt(0), hiddenSheetName, colName, array, 2, topLevelArr.size() + 1000, 6, 6);
    }

感谢

感谢【博客园】大佬【作者:二次元的程序猿】的文章《ava POI 利用隐藏sheet实现导出下拉数据太多为空

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Excel 三级 联动 下拉框 宏代码 实例 代码注释 先在第一个下拉框加入一个valiation, 内容是 =$A$2:$A$5 Private Sub Worksheet_Change(ByVal Target As Range) ' Call back function which defined within according worksheet Dim i As Integer Dim tempStr As String Dim firstDrawBoxRowCount As Integer Dim firstDrawBoxColumn As Integer firstDrawBoxRowCount = 4 'Define the row number of first draw box firstDrawBoxColumn = 1 'Define the column number of ifrst draw box Dim secondDrawBoxRowCount As Integer Dim secondDrawBoxColumn As Integer secondDrawBoxRowCount = 33 'Define the row number of second draw box secondDrawBoxColumn = 4 'Define the column number of second draw box If Target.Column = 1 Then 'This defines the first column of draw box list, you can also define the row number of draw box list Cells(Target.Row, Target.Column + 1) = "" ' Do the clean first Cells(Target.Row, Target.Column + 1).Validation.Delete Cells(Target.Row, Target.Column + 2) = "" Cells(Target.Row, Target.Column + 2).Validation.Delete For i = 2 To firstDrawBoxRowCount + 1 'Enter the cycle to find out the content for column 2 If Trim(Cells(Target.Row, Target.Column)) = Trim(Cells(i, firstDrawBoxColumn)) Then tempStr = Trim(Cells(i, firstDrawBoxColumn + 1)) 'Find out the options for second draw box, it is seperated by , Cells(Target.Row, Target.Column + 1).Select ' Fill the validation to second draw box With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:=tempStr .IgnoreBlank = True .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True
使用POI导出Excel设置下拉框可以通过以下步骤实现: 1. 创建下拉列表数据源。可以使用org.apache.poi.ss.usermodel.DataValidationHelper类中的createExplicitListConstraint方法创建下拉列表数据源。 例如: DataValidationHelper dvHelper = sheet.getDataValidationHelper(); CellRangeAddressList addressList = new CellRangeAddressList(1, 10, 0, 0); String[] strings = {"选项1", "选项2", "选项3"}; DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(strings); DataValidation validation = dvHelper.createValidation(dvConstraint, addressList); sheet.addValidationData(validation); 上面的代码创建了一个下拉列表数据源,该数据源包含三个选项:选项1、选项2和选项3。该数据源将应用于第1行到第10行的第1列单元格。 2. 设置单元格格式为下拉列表格式。可以使用org.apache.poi.ss.usermodel.Cell类中的setCellValue方法将单元格的值设置为下拉列表数据源中的一个选项。 例如: Cell firstCell = sheet.getRow(1).createCell(0); firstCell.setCellValue("选项1"); 3. 保存Excel文件。最后,使用org.apache.poi.ss.usermodel.Workbook类中的write方法将Excel文件保存到磁盘。 例如: FileOutputStream fileOut = new FileOutputStream("workbook.xls"); workbook.write(fileOut); fileOut.close(); 上面的代码将Excel文件保存到名为“workbook.xls”的文件中。 注意:上面的代码仅供示例参考,具体实现应根据具体需求进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

验证码有毒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值