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

Java 无法使用JXL API在Excel中查看组合框,java,excel,combobox,jxl,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.s

我试图用以下代码在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);
        }
    }
}
ArrayList arrList=new ArrayList();
arrList.add(“下拉列表1”);
arrList.add(“DropDown2”);
arrList.add(“DropDown3”);
WritableCellFeatures cellFeatures=新的WritableCellFeatures();
cellFeatures.setDataValidationList(arrList);
空白b=null;
标签checkLabel=null;
对于(int x=0;x
我试过“空白”单元格和“标签”。但是Excel仍然没有显示组合框。

如果在没有先调用write()的情况下调用close(),将生成一个完全空的文件。 将工作表和单元格添加到工作簿后,可以对工作簿调用write(),然后关闭文件。最后一步生成可由Excel读取的输出文件(本例中为output.xls)。需要添加:

        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();
WritableCellFeatures-cellFeatures=null;
标签checkLabel=null;
对于(int x=0;x
即使是空白版本也适用,但在这种情况下,单元格没有初始值。 根据我的测试,此代码也可以正常工作:

        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();
WritableCellFeatures-cellFeatures=null;
空白b=null;
对于(int x=0;x
如果使用Excel 2010或Excel 2013打开生成的文件
.xls
,则可能需要另存为
.xlsx
以查看组合。 我在Excel2010/2013年体验了.xls的开场白, 即使单元格实际包含数据验证列表且验证约束有效,数据验证箭头也会丢失;如果希望看到箭头和组合框,则需要以新格式另存为

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



给任何想改进的人,或者用叉子和它一起玩的人

非常感谢您的回复。我的代码是正确的,但您指出的下一个问题很重要。保存为XLSX后,我得到了组合框。这是关于我的项目的主要问题。有没有使用JXL API的解决方案?或者,唯一的解决办法是转到Apache POI?还有其他几个论坛有这个问题,但没有答案。向@Franco致敬,获取详细答案。我要补充的是,使用标签的答案对我有效。空白溶液不起作用;我怀疑是因为我的单元格有一个初始值。我遇到了与OP相同的问题:下拉列表没有出现在.xls上,但它确实出现在.xlsx上。我只是将电子表格读入excel,然后保存为.xlsx。从那以后,下拉菜单起作用了。通过工作,我的意思是当选择单元格时,小箭头出现在单元格的右侧。后来,在使用.xls时,我注意到小箭头出现在单元格A1上。单击单元格A1上的小箭头时,所选单元格上会出现下拉列表。似乎是一个很好的例子,但在Excel中。我正在使用Excel for Mac 2011。版本14.4.7。