java - 无法使用 JXL API 在 Excel 中查看组合框

标签 java excel combobox jxl

我尝试使用以下代码在 JXL API 中显示 ComboBox:

ArrayList<String> arrList = new ArrayList<String>();
arrList.add("DropDown1");
arrList.add("DropDown2");
arrList.add("DropDown3");
WritableCellFeatures cellFeatures = new WritableCellFeatures();
cellFeatures.setDataValidationList(arrList);

Blank b = null;
Label checkLabel = null;
for (int x = 0; x < xlData.size(); x++) {
    for (int i = 0; i <= 14; i++) {
        System.out.println("X:" + x + "I:" + i);
        if (i > 9) {
            checkLabel = new Label(i, x + xlHeader.size(),(String) arrList.get(0));
            //b = new Blank(i, x + xlHeader.size());
            //b.setCellFeatures(cellFeatures);
            checkLabel.setCellFeatures(cellFeatures);
            writableSheet.addCell(checkLabel);
            System.out.println("Combo Cell : " + x + ":" + i);
        }
    }
}

我尝试过“空白”单元格和“标签”。但 Excel 仍然没有显示 ComboBox。

最佳答案

如果调用 close() 而不先调用 write(),将会生成一个完全空的文件。

将工作表和单元格添加到工作簿后,您可以在工作簿上调用 write(),然后关闭文件。最后一步生成可由 Excel 读取的输出文件(在本例中为 output.xls)。 credits this excellent tutorial需要添加:

        copy.write(); 
        copy.close();

cellFeatures 需要在循环内重新实例化

根据我的测试,这段代码工作正常:

        WritableCellFeatures cellFeatures =  null;
        Label checkLabel = null;
        for (int x = 0; x < xlData.size(); x++) {
            for (int i = 0; i <= 14; i++) {
                System.out.println("X:" + x + "I:" + i);
                if (i > 9) {
                   checkLabel = new Label(i, x + xlHeader.size(), (String) arrList.get(0));
                   cellFeatures = new WritableCellFeatures();
                   cellFeatures.setDataValidationList(arrList);
                   checkLabel.setCellFeatures(cellFeatures);
                   writableSheet.addCell(checkLabel);                           
                }
            }
        }
        // All cells modified/added. Now write out the workbook 
        workbook.write();
        workbook.close();

即使空白版本也可以工作,但在这种情况下单元格没有初始值

根据我的测试,这段代码也可以正常工作:

        WritableCellFeatures cellFeatures =  null;
        Blank b = null;
        for (int x = 0; x < xlData.size(); x++) {
            for (int i = 0; i <= 14; i++) {
                System.out.println("X:" + x + "I:" + i);
                if (i > 9) {
                   b = new Blank(i, x + xlHeader.size());
                   cellFeatures = new WritableCellFeatures();
                   cellFeatures.setDataValidationList(arrList);
                   b.setCellFeatures(cellFeatures);
                   writableSheet.addCell(b);                           
                }
            }
        }
        // All cells modified/added. Now write out the workbook 
        workbook.write();
        workbook.close();

如果您使用 Excel 2010 或 Excel 2013 打开生成的文件 .xls,您可能需要另存为 .xlsx 才能查看组合。

我经历过用Excel2010/2013打开.xls, 即使单元格实际上包含数据验证列表并且验证约束有效,数据验证箭头也会丢失;如果您想看到箭头和组合框,则需要另存为新格式。

此外,这个缺陷似乎是由最新的 Excel 版本而不是 JXL 引起的,在 OpenOffice.org Cal 3.4.1 中打开 .xls 没有任何问题并且组合工作正常这一事实证明了这一点;这可能与当前版本 jxl 2.6.12 2009-10-26 有关。我用于测试生成 Excel 2000 格式的电子表格

<小时/>

The Java code developed for this answer is available任何想要改进或 fork 并使用它的人。

关于java - 无法使用 JXL API 在 Excel 中查看组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18780151/

相关文章:

java - 从Java服务器发送图像到android客户端

c# - LINQ 和 ExcelQueryFactory

c# - 在 Unity 中从 excel 中检索元数据

vb.net - 用一行代码加载组合框?

html - 2个组合框一起工作

Java:将变量设置为类的变量,其中类的名称是字符串?

java - 在倒数计时器中启动另一个计时器

excel - 如何跟踪谁使用我的 Excel 电子表格?

WINAPI - 设置组合框下拉菜单的背景和文本颜色

java - 过滤对象列表中的重复对象