我正在尝试生成带有一些验证的excel文件,我已阅读poi dev guides来实现它。在实现过程中,我得到了一个异常(String literals in formulas can't be bigger than 255 characters ASCII
)。 POI将所有下拉选项连接成'0'消除字符串并检查其长度并给我异常。 :(
我正在使用最新版本的POI 3.8 beta 5。
我的代码是:
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 0);
//CellRangeAddressList from org.apache.poi.ss.util package
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(getCountries());
DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
sheet.addValidationData(dataValidation);
FileOutputStream fileOut = new FileOutputStream("c:\\test.xls");
wb.write(fileOut);
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
之后我尝试使用此代码使用XSSFWorkBook:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("new sheet");
DataValidationHelper validationHelper = new XSSFDataValidationHelper(sheet);
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(getCountries());
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DataValidation dataValidation = validationHelper.createValidation(constraint, addressList);
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
dataValidation.setSuppressDropDownArrow(true);
sheet.addValidationData(dataValidation);
FileOutputStream fileOut = new FileOutputStream("c:\\test.xlsx");
不幸的是,在一个单元格中逗号分隔长字符串这样的结果没有成功:
但是在excel手动中,我可以使用这个较长的国家/地区列表创建下拉列表单元格。
是否有任何方法可以使用长字符串生成下拉列表,或者API不支持?
答案 0 :(得分:28)
据我所知,Excel本身不允许输入超过255个字符的验证范围字符串,这不是POI限制。现在我正在使用Named Ranges and Named Cells,它对我来说正常。所以我不得不将我的验证范围标记放在另一张表中(隐藏),并从我的真实表中引用了所需的单元格范围。这是我的工作代码:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet realSheet = workbook.createSheet("Sheet xls");
HSSFSheet hidden = workbook.createSheet("hidden");
for (int i = 0, length= countryName.length; i < length; i++) {
String name = countryName[i];
HSSFRow row = hidden.createRow(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(name);
}
Name namedCell = workbook.createName();
namedCell.setNameName("hidden");
namedCell.setRefersToFormula("hidden!$A$1:$A$" + countryName.length);
DVConstraint constraint = DVConstraint.createFormulaListConstraint("hidden");
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
HSSFDataValidation validation = new HSSFDataValidation(addressList, constraint);
workbook.setSheetHidden(1, true);
realSheet.addValidationData(validation);
FileOutputStream stream = new FileOutputStream("c:\\range.xls");
workbook.write(stream);
stream.close();
答案 1 :(得分:2)
以上代码工作正常。但是,如果我使用XSSF类而不是HSSF,它就无法在线程
中说出异常"main" java.lang.NoSuchMethodError: org.apache.poi.ss.formula.FormulaParser.parse(Ljava/lang/String;Lorg/apache/poi/ss/formula/FormulaParsingWorkbook;II)[Lorg/apache/poi/ss/formula/ptg/Ptg; at org.apache.poi.xssf.usermodel.XSSFName.setRefersToFormula(XSSFName.java:195) at XLDropdown.main(XLDropdown.java:35)
要使用XSSF接口创建下拉列表,我在
处获得了解决方案答案 2 :(得分:1)
Õzbek提供的强大解决方案只需稍加修改就可以完美地使用NPOI(在.NET上使用C#)。
这是我的代码,为C#编码器提供了便利。它将工作表和位置元素作为输入,并且当它们按列排列时也可以处理多个下拉列表。
public static void CreateDropDownListForExcel(this ISheet sheet, IList<string> dropDownValues, int startRow, int lastRow, int column) {
if (sheet == null) {
return;
}
//Create a hidden sheet on the workbook (using the column as an id) with the dropdown values
IWorkbook workbook = sheet.Workbook;
string dropDownName = sheet.SheetName + "DropDownValuesForColumn" + column;
ISheet hiddenSheet = workbook.CreateSheet(dropDownName);
for (int i = 0, length = dropDownValues.Count; i < length; i++) {
string name = dropDownValues[i];
IRow row = hiddenSheet.CreateRow(i);
ICell cell = row.CreateCell(0);
cell.SetCellValue(name);
}
//Create the dropdown using the fields of the hidden sheet
IName namedCell = workbook.CreateName();
namedCell.NameName = dropDownName;
namedCell.RefersToFormula = (dropDownName + "!$A$1:$A$" + dropDownValues.Count);
DVConstraint constraint = DVConstraint.CreateFormulaListConstraint(dropDownName);
CellRangeAddressList addressList = new CellRangeAddressList(startRow, lastRow, column, column);
HSSFDataValidation validation = new HSSFDataValidation(addressList, constraint);
int hiddenSheetIndex = workbook.GetSheetIndex(hiddenSheet);
workbook.SetSheetHidden(hiddenSheetIndex, SheetState.HIDDEN);
//Add the Dropdown to the presenting sheet.
sheet.AddValidationData(validation);
}