[英]Not able to view ComboBox in Excel using JXL API
我正在嘗試使用以下代碼在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。
將工作表和單元格添加完畢后,請在工作簿上調用write(),然后關閉文件。 最后一步生成輸出文件(在這種情況下為output.xls),可由Excel讀取。 感謝這個出色的教程,它需要添加:
copy.write();
copy.close();
根據我的測試,此代碼可以正常工作:
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();
.xls
,則可能需要另存為.xlsx
才能看到該組合。 我遇到了在Excel2010 / 2013之前打開.xls的情況,即使單元格實際上包含一個數據驗證列表並且驗證約束起作用,數據驗證箭頭也丟失了; 如果要查看箭頭和組合框,則需要另存為新格式。
而且,這個缺點似乎是由最近的Excel版本而不是JXL引起的,這一事實表明,在OpenOffice.org Cal 3.4.1中打開.xls不會有任何問題,並且該組合可以正常工作。 這可能與以下事實有關:我用於測試的當前版本jxl 2.6.12 2009-10-26 生成Excel 2000格式的電子表格
任何想要改進或使用它的人都可以使用為此答案開發的Java代碼 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.