【问题标题】:Is there a max number items while generating drop down list in Excel using Apache POI?使用 Apache POI 在 Excel 中生成下拉列表时是否有最大数量的项目?
【发布时间】:2014-12-24 01:59:06
【问题描述】:

我正在尝试使用 Apache POI 为一个单元格添加下拉列表。下拉列表包含 302 个字符串。我总是收到这个错误:Excel 在 test.xlsx 中发现了不可读的内容。

然后我做了以下测试。当项目数 88时,打开excel文件时出现错误,没有下拉列表。

谢谢!!!

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.TreeSet;

public class Test {

   public static void main(String[] args) {
    TreeSet<String> temp_rxGroups = new TreeSet<String>();
        for (int i = 0; i < 100; i++) {
            temp_rxGroups.add("" + i);
        }
        String[] countryName = temp_rxGroups.toArray(new String[temp_rxGroups.size()]);

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet realSheet = workbook.createSheet("realSheet");
        XSSFSheet hidden = workbook.createSheet("hidden");
        for (int i = 0, length= countryName.length; i < length; i++) {
            String name = countryName[i];
            XSSFRow row = hidden.createRow(i);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue(name);
        }
        Name namedCell = workbook.createName();
        namedCell.setNameName("hidden");
        namedCell.setRefersToFormula("hidden!$A$1:$A$" + countryName.length);


        DataValidation dataValidation = null;
        DataValidationConstraint constraint = null;
        DataValidationHelper validationHelper = null;
        validationHelper=new XSSFDataValidationHelper(hidden);
        CellRangeAddressList addressList = new  CellRangeAddressList(0,10,0,0);
        //line
        constraint =validationHelper.createExplicitListConstraint(countryName); 
        dataValidation = validationHelper.createValidation(constraint, addressList);
        dataValidation.setSuppressDropDownArrow(true);
        workbook.setSheetHidden(1, true);
        realSheet.addValidationData(dataValidation);
        FileOutputStream stream = new FileOutputStream("c:\\test.xlsx");
        workbook.write(stream);
        stream.close();

    }
}

}

【问题讨论】:

  • Excel 中的限制是什么?您正在尝试使用哪个版本的 Apache POI?
  • 我使用 Apache POI 3.9。就像我说的,当下拉列表包含超过 100 项时,excel 说:excel 发现不可读的内容。 excel中没有下拉列表。我用的是office 2010。
  • 为什么要使用像 3.9 这样的旧版本?如果您升级到最新版本 3.11,会发生什么情况?
  • 我刚刚测试了 3.11 和 3.11-beta3。 Excel 会抛出同样的错误。
  • 你能得到它以便添加 x 项工作,而 x+1 失败吗?理想情况下,我们需要 3 个文件,x worksx+1 failsx loaded into excel + 1 more added by Excel

标签: java excel apache apache-poi


【解决方案1】:

首先,我发现这不是 Apache POI 错误。这是 Excel 的限制。这是link

“数据验证下拉列表中显示的项目数量有限制:

该列表最多可显示工作表上列表中的 32,767 个项目。 如果您在数据验证对话框(分隔列表)中键入项目,则限制为 256 个字符,包括分隔符。"

显然,这一行明确输入了超过 256 个字符。

constraint =validationHelper.createExplicitListConstraint(countryName);

其次,这是我的解决方案。它工作正常。

public class Test {

    public static void main(String[] args) throws IOException{
        TreeSet<String> temp_rxGroups = new TreeSet<String>();
        for (int i = 0; i < 302; i++) {
            temp_rxGroups.add("" + i);
        }
        String[] countryName = temp_rxGroups.toArray(new String[temp_rxGroups.size()]);

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet realSheet = workbook.createSheet("realSheet");
        XSSFSheet hidden = workbook.createSheet("hidden");
        for (int i = 0, length= countryName.length; i < length; i++) {
            String name = countryName[i];
            XSSFRow row = hidden.createRow(i);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue(name);
        }

        DataValidation dataValidation = null;
        DataValidationConstraint constraint = null;
        DataValidationHelper validationHelper = null;
        validationHelper=new XSSFDataValidationHelper(realSheet);
        CellRangeAddressList addressList = new  CellRangeAddressList(0,0,0,0);
        constraint =validationHelper.createFormulaListConstraint("hidden!$A$1:$A$" + countryName.length);
        dataValidation = validationHelper.createValidation(constraint, addressList);
        dataValidation.setSuppressDropDownArrow(true);
        workbook.setSheetHidden(1, true);
        realSheet.addValidationData(dataValidation);
        FileOutputStream stream = new FileOutputStream("c:\\test.xlsx");
        workbook.write(stream);
        stream.close();

    }

}

【讨论】:

  • 非常感谢。有没有办法限制用户只从下拉列表中选择值。因为上面的代码会让用户在下拉值之外输入自己的值。
  • 34 小时后,您的回答让我松了一口气。你救了一条命。非常感谢。
  • 非常感谢!我试图找出为什么从工作表列创建的列表按预期工作,而显式值却没有。现在我知道了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
相关资源
最近更新 更多