java - Web開發(fā) - POI導出帶有下拉框的Excel和解決下拉中數(shù)組過多而產(chǎn)生的異常
問題描述
1、如果Excel下拉的數(shù)組較少(大概為0~20個),可以用如下方式導出:
/** * Excel API */ @SuppressWarnings('resource') HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.createSheet('xxxx'); /** * 初始化參數(shù) */ Map<String, String> map = new HashMap<String, String>(); // 查詢時用的map List<Object> list = null; String[] strs = null; // 用于下拉的數(shù)組 int startRow = 1; // 下拉的開始行 int endRow = 100; // 下拉的結束行 CellRangeAddressList regions = null; DVConstraint constraint = null; CellRangeAddressList addressList = null; HSSFDataValidation validation = null; // 數(shù)據(jù)驗證 map.put('namespace', 'xxxxxxxxxx.xxxxxxxxxx'); // 查詢數(shù)據(jù) list = commonQueryService.queryList(map); strs = StringUtil.mapListToStrs(list); // list轉換為字符串數(shù)組 cellNum = SpuEnu.CATEGORY_1.getNumber(); // 下拉的列regions = new CellRangeAddressList(startRow, endRow, cellNum, cellNum); // 開始行、結束行、開始列、結束列的下拉區(qū)域均被下拉替代 constraint = DVConstraint.createExplicitListConstraint(strs); validation = new HSSFDataValidation(regions, constraint); // 綁定下拉框和作用區(qū)域 sheet.addValidationData(validation);
2、問題是如果下拉的數(shù)組過多,POI會出現(xiàn)如下異常信息:
String literals in formulas can’t be bigger than 255 characters ASCII
這個問題的解決辦法網(wǎng)上不好查到,所以我將解決辦法貼在下面
問題解答
回答1:下面是解決辦法:
/** * Excel API */ @SuppressWarnings('resource') HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.createSheet('spu導入模板'); /** * 初始化參數(shù) */ Map<String, String> map = new HashMap<String, String>(); // 查詢時用的map List<Object> list = null; String[] strs = null; // 用于下拉的數(shù)組 String hiddenSheet = null; int cellNum = 0; int startRow = 1; // 開始行 int endRow = 100; // 結束行 DVConstraint constraint = null; CellRangeAddressList addressList = null; HSSFDataValidation validation = null; // 數(shù)據(jù)驗證 map.put('namespace', 'xxxxxxx.xxxxx'); // 查詢 list = commonQueryService.queryList(map); strs = StringUtil.mapListToStrs(list); hiddenSheet = 'category1Hidden'; cellNum = SpuEnu.CATEGORY_1.getNumber();HSSFSheet category1Hidden = book.createSheet(hiddenSheet); // 創(chuàng)建隱藏域 for (int i = 0, length = strs.length; i < length; i++) { // 循環(huán)賦值(為了防止下拉框的行數(shù)與隱藏域的行數(shù)相對應來獲取>=選中行數(shù)的數(shù)組,將隱藏域加到結束行之后)category1Hidden.createRow(endRow + i).createCell(cellNum).setCellValue(strs[i]); } Name category1Name = book.createName(); category1Name.setNameName(hiddenSheet); category1Name.setRefersToFormula(hiddenSheet + '!A1:A' + (strs.length + endRow)); // A1:A代表隱藏域創(chuàng)建第?列createCell(?)時。以A1列開始A行數(shù)據(jù)獲取下拉數(shù)組constraint = DVConstraint.createFormulaListConstraint(hiddenSheet); addressList = new CellRangeAddressList(startRow, endRow, cellNum, cellNum); validation = new HSSFDataValidation(addressList, constraint); book.setSheetHidden(1, true); // 1隱藏、0顯示 sheet.addValidationData(validation);
請注意上面的這倆個地方:
相關文章:
1. android - Genymotion 微信閃退 not find plugin.location_google.GoogleProxyUI2. mac里的docker如何命令行開啟呢?3. android webview返回自動刷新4. objective-c - iOS開發(fā)使用什么對html進行代碼高亮5. angular.js - 關于ng-model和ng-bind的疑問6. html - 特殊樣式按鈕 點擊按下去要有凹下和彈起的效果7. angular.js - 在ionic下,利用javascript導入百度地圖,pc端可以顯示,移動端無法顯示8. javascript - 單個控件多個字段搜索9. javascript - npm安裝報錯 系統(tǒng)w7 求大神解答10. html5 - 微信瀏覽器視頻播放失敗
