gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:14:19 25 4
gpt4 key购买 nike

我尝试使用以下代码在 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.122009-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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com