2017-07-14 75 views
0

的Apache POI Double值我想有以下entrys一个ComboBox:在组合框

{ “0,5”, “1”, “1.5”, “2”,“2, 5" }

我使用资料验证:

DataValidation dataValidation = null; 
    DataValidationConstraint constraint = null; 
    DataValidationHelper validationHelper = null; 

    validationHelper = new XSSFDataValidationHelper(sheet); 
    CellRangeAddressList addressList = new CellRangeAddressList(row, row, col, col); 
    constraint = validationHelper.createExplicitListConstraint(list); 
    dataValidation = validationHelper.createValidation(constraint, addressList); 
    dataValidation.setSuppressDropDownArrow(true); 
    sheet.addValidationData(dataValidation); 

列表是如下结构的:

list = new String [] {“0,5”,“1”,“1,5”,“2”,“2,5”}

但是生成excel文件后,在下拉列表中。

0,5,1,1,5

为什么呢?

如果我使用一个点符号(0.5,1,1.5)的下一个问题是,当我从组合框选择,EXCEL自动套用其日期,如1.5 - > 01月

回答

2

从你的描述似乎在您的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不支持这一点。

作为解决方法,我建议使用隐藏工作表来存储列表项。

例子:

import java.io.*; 

import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.xssf.usermodel.*; 

import org.apache.poi.ss.util.*; 

class CreateExcelDataValidationList { 

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); 

    workbook.write(new FileOutputStream("CreateExcelDataValidationList.xlsx")); 
    workbook.close(); 

} 
} 
+0

谢谢你,为我工作:) – sHarivaRi