问题来源:
导出时如果下拉框中的文字总长度超过一定限制就会导致导出报255错误
解决方案思路:
在创建sheet页时新建一个sheet页, 将下拉的数据写到新建的sheet页中, 然后将该sheet页隐藏
代码参考:
XSSFWorkbook wb = new XSSFWorkbook(); String sheetName = data.getName(); if (null == sheetName) { sheetName = "Sheet1"; } XSSFSheet sheet = wb.createSheet(sheetName); //获取所有sheet页个数 int sheetTotal = wb.getNumberOfSheets(); //处理下拉数据 if (data.getCellRangeMap() != null) { Set<Map.Entry<Integer, String[]>> selectSet = data.getCellRangeMap().entrySet(); Iterator iterator = ((Set) selectSet).iterator(); while (iterator.hasNext()) { Map.Entry<Integer, String[]> entryMap = (Map.Entry<Integer, String[]>) iterator.next(); Integer columnIndex = entryMap.getKey(); //下拉框所在的列 String[] selectList = entryMap.getValue(); //对应列下拉框数据 //新建一个sheet页 String hiddenSheetName = "hiddenSheet" + sheetTotal; XSSFSheet hiddenSheet = wb.createSheet(hiddenSheetName); Row row; //写入下拉数据到新的sheet页中 for (int i = 0; i < selectList.length; i++) { row = hiddenSheet.createRow(i); Cell cell = row.createCell(0); cell.setCellValue(selectList[i]); } //获取新sheet页内容 String strFormula = hiddenSheetName + "!$A$1:$A$65535"; XSSFDataValidationConstraint constraint = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST,strFormula); // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 CellRangeAddressList regions = new CellRangeAddressList(0,65535, columnIndex, columnIndex); // 数据有效性对象 DataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet) sheet); DataValidation validation = help.createValidation(constraint, regions); sheet.addValidationData(validation); //将新建的sheet页隐藏掉 wb.setSheetHidden(sheetTotal, true); sheetTotal++; } }
来源:https://www.cnblogs.com/yifanSJ/p/10863339.html