Apache POI Double Values in ComboBox

前端 未结 1 1072
半阙折子戏
半阙折子戏 2021-01-28 10:01

I want to have a ComboBox with the following entrys:

{\"0,5\", \"1\", \"1,5\", \"2\", \"2,5\"}

I use DataValidation:



        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 10:20

    From your description it seems that in your Excel the decimal delimiter is comma from your current locale settings. So this comma in {"0,5", "1", "1,5", "2", "2,5"} is in conflict with the comma used as list delimiter in list constraint formula. This is because this list constraint formula will be "0,5,1,1,5,2,2,5".

    When used {"0.5", "1", "1.5", "2", "2.5"}, the list constraint formula will be "0.5,1,1.5,2,2.5". But now the dot in this formula is in conflict with your locale settings having comma as decimal delimiter and dot as delimiter in date literals.

    This is a well known Excel problem. Current Excel versions are solving this by using a different kind of storing the list constraint: "0,5",1,"1,5",2,"2,5" instead of: "0,5,1,1,5,2,2,5". But apache poi does not support this.

    As a workaround I suggest using a hidden sheet to store the list items.

    Example:

    import java.io.*;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.*;
    
    import org.apache.poi.ss.util.*;
    
    class CreateExcelDataValidationListName {
    
     public static void main(String[] args) throws Exception{
    
       Workbook workbook = new XSSFWorkbook();  
    
       //create sheet for storing the list items:
       Sheet sheet = workbook.createSheet("ListSheet");
       sheet.createRow(0).createCell(0).setCellValue("SourceList");
       int r = 1;
       for (double d = 0.5; d < 3; d+=0.5) {
        sheet.createRow(r++).createCell(0).setCellValue(d);
       }
       //unselect that sheet because we will hide it later
       sheet.setSelected(false);
       //create a named range for the list contraint
       Name namedCell = workbook.createName();
       namedCell.setNameName("SourceList");
       String reference = "ListSheet!$A$2:$A$5";
       namedCell.setRefersToFormula(reference);
    
       //create the visible sheet
       sheet = workbook.createSheet("Sheet1");
    
       sheet.createRow(0).createCell(0).setCellValue("Take the ListItems from B1:");
       sheet.setActiveCell(new CellAddress("B1"));
    
       sheet.autoSizeColumn(0);
    
       //create the data validation
       DataValidationHelper dvHelper = sheet.getDataValidationHelper();
       DataValidationConstraint dvConstraint = dvHelper.createFormulaListConstraint("SourceList");
       CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 1, 1);            
       DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
    
       sheet.addValidationData(validation);
    
       //hide the ListSheet
       workbook.setSheetHidden(0, true);
       //set Sheet1 active
       workbook.setActiveSheet(1);
    
       FileOutputStream out = new FileOutputStream("CreateExcelDataValidationList.xlsx");
       workbook.write(out);
       out.close();
       workbook.close();
    
     }
    }
    

    0 讨论(0)
提交回复
热议问题