gpt4 book ai didi

java - 组合框中的 Apache POI 双值

转载 作者:行者123 更新时间:2023-12-01 20:19:47 25 4
gpt4 key购买 nike

我想要一个包含以下条目的组合框:

{"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. May

最佳答案

从你的描述看来,在你的 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 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();

}
}

关于java - 组合框中的 Apache POI 双值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108735/

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