apache poi双值

polhcujo  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(434)

我想要一个包含以下条目的组合框:
{"0,5", "1", "1,5", "2", "2,5"}
我使用数据验证:

  1. DataValidation dataValidation = null;
  2. DataValidationConstraint constraint = null;
  3. DataValidationHelper validationHelper = null;
  4. validationHelper = new XSSFDataValidationHelper(sheet);
  5. CellRangeAddressList addressList = new CellRangeAddressList(row, row, col, col);
  6. constraint = validationHelper.createExplicitListConstraint(list);
  7. dataValidation = validationHelper.createValidation(constraint, addressList);
  8. dataValidation.setSuppressDropDownArrow(true);
  9. sheet.addValidationData(dataValidation);

该列表具有以下结构:
列表=新字符串[]{“0,5”,“1”,“1,5”,“2”,“2,5”}
但是在生成excel文件之后,下拉列表中还有其他内容。
0, 5, 1, 1, 5
为什么?
如果我使用点符号(0.5,1,1.5),下一个问题是,当我从组合框中选择时,excel会自动将其格式化为一个日期,例如1.5->01。可以

zysjyyx4

zysjyyx41#

从你的描述看来 Excel 十进制分隔符是当前区域设置中的逗号。所以这个逗号 {"0,5", "1", "1,5", "2", "2,5"} 与列表约束公式中用作列表分隔符的逗号冲突。这是因为此列表约束公式将 <formula1>"0,5,1,1,5,2,2,5"</formula1> .
使用时 {"0.5", "1", "1.5", "2", "2.5"} ,列表约束公式为 <formula1>"0.5,1,1.5,2,2.5"</formula1> . 但是现在这个公式中的点与您的区域设置冲突,在日期文本中逗号作为十进制分隔符,点作为分隔符。
这是一个众所周知的问题 Excel 问题。当前 Excel 版本通过使用不同类型的存储列表约束来解决此问题: <x12ac:list>"0,5",1,"1,5",2,"2,5"</x12ac:list> 而不是: <formula1>"0,5,1,1,5,2,2,5"</formula1> . 但是 apache poi 不支持此操作。
作为一种解决方法,我建议使用隐藏的工作表来存储列表项。
例子:

  1. import java.io.*;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.apache.poi.xssf.usermodel.*;
  4. import org.apache.poi.ss.util.*;
  5. class CreateExcelDataValidationListName {
  6. public static void main(String[] args) throws Exception{
  7. Workbook workbook = new XSSFWorkbook();
  8. //create sheet for storing the list items:
  9. Sheet sheet = workbook.createSheet("ListSheet");
  10. sheet.createRow(0).createCell(0).setCellValue("SourceList");
  11. int r = 1;
  12. for (double d = 0.5; d < 3; d+=0.5) {
  13. sheet.createRow(r++).createCell(0).setCellValue(d);
  14. }
  15. //unselect that sheet because we will hide it later
  16. sheet.setSelected(false);
  17. //create a named range for the list contraint
  18. Name namedCell = workbook.createName();
  19. namedCell.setNameName("SourceList");
  20. String reference = "ListSheet!$A$2:$A$5";
  21. namedCell.setRefersToFormula(reference);
  22. //create the visible sheet
  23. sheet = workbook.createSheet("Sheet1");
  24. sheet.createRow(0).createCell(0).setCellValue("Take the ListItems from B1:");
  25. sheet.setActiveCell(new CellAddress("B1"));
  26. sheet.autoSizeColumn(0);
  27. //create the data validation
  28. DataValidationHelper dvHelper = sheet.getDataValidationHelper();
  29. DataValidationConstraint dvConstraint = dvHelper.createFormulaListConstraint("SourceList");
  30. CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 1, 1);
  31. DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
  32. sheet.addValidationData(validation);
  33. //hide the ListSheet
  34. workbook.setSheetHidden(0, true);
  35. //set Sheet1 active
  36. workbook.setActiveSheet(1);
  37. FileOutputStream out = new FileOutputStream("CreateExcelDataValidationList.xlsx");
  38. workbook.write(out);
  39. out.close();
  40. workbook.close();
  41. }
  42. }
展开查看全部

相关问题