java - Web開發(fā) - POI導(dǎo)出帶有下拉框的Excel和解決下拉中數(shù)組過多而產(chǎn)生的異常
問題描述
1、如果Excel下拉的數(shù)組較少(大概為0~20個(gè)),可以用如下方式導(dǎo)出:
/** * Excel API */ @SuppressWarnings('resource') HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.createSheet('xxxx'); /** * 初始化參數(shù) */ Map<String, String> map = new HashMap<String, String>(); // 查詢時(shí)用的map List<Object> list = null; String[] strs = null; // 用于下拉的數(shù)組 int startRow = 1; // 下拉的開始行 int endRow = 100; // 下拉的結(jié)束行 CellRangeAddressList regions = null; DVConstraint constraint = null; CellRangeAddressList addressList = null; HSSFDataValidation validation = null; // 數(shù)據(jù)驗(yàn)證 map.put('namespace', 'xxxxxxxxxx.xxxxxxxxxx'); // 查詢數(shù)據(jù) list = commonQueryService.queryList(map); strs = StringUtil.mapListToStrs(list); // list轉(zhuǎn)換為字符串?dāng)?shù)組 cellNum = SpuEnu.CATEGORY_1.getNumber(); // 下拉的列regions = new CellRangeAddressList(startRow, endRow, cellNum, cellNum); // 開始行、結(jié)束行、開始列、結(jié)束列的下拉區(qū)域均被下拉替代 constraint = DVConstraint.createExplicitListConstraint(strs); validation = new HSSFDataValidation(regions, constraint); // 綁定下拉框和作用區(qū)域 sheet.addValidationData(validation);
2、問題是如果下拉的數(shù)組過多,POI會(huì)出現(xiàn)如下異常信息:
String literals in formulas can’t be bigger than 255 characters ASCII
這個(gè)問題的解決辦法網(wǎng)上不好查到,所以我將解決辦法貼在下面
問題解答
回答1:下面是解決辦法:
/** * Excel API */ @SuppressWarnings('resource') HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.createSheet('spu導(dǎo)入模板'); /** * 初始化參數(shù) */ Map<String, String> map = new HashMap<String, String>(); // 查詢時(shí)用的map List<Object> list = null; String[] strs = null; // 用于下拉的數(shù)組 String hiddenSheet = null; int cellNum = 0; int startRow = 1; // 開始行 int endRow = 100; // 結(jié)束行 DVConstraint constraint = null; CellRangeAddressList addressList = null; HSSFDataValidation validation = null; // 數(shù)據(jù)驗(yàn)證 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ù)相對(duì)應(yīng)來獲取>=選中行數(shù)的數(shù)組,將隱藏域加到結(jié)束行之后)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(?)時(shí)。以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);
請(qǐng)注意上面的這倆個(gè)地方:
相關(guān)文章:
1. javascript - ionic1的插件如何遷移到ionic2的項(xiàng)目中2. java - 如何在Fragment中調(diào)用Activity的onNewIntent?3. javascript - h5上的手機(jī)號(hào)默認(rèn)沒有識(shí)別4. mysql里的大表用mycat做水平拆分,是不是要先手動(dòng)分好,再配置mycat5. css - 關(guān)于input標(biāo)簽disabled問題6. python - 獲取到的數(shù)據(jù)生成新的mysql表7. 怎么用css截取字符?8. window下mysql中文亂碼怎么解決??9. javascript - jquery hide()方法無效10. python的文件讀寫問題?
