java - Web開發(fā) - POI導(dǎo)出帶有下拉框的Excel和解決下拉中數(shù)組過(guò)多而產(chǎn)生的異常
問(wè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、問(wèn)題是如果下拉的數(shù)組過(guò)多,POI會(huì)出現(xiàn)如下異常信息:
String literals in formulas can’t be bigger than 255 characters ASCII
這個(gè)問(wèn)題的解決辦法網(wǎng)上不好查到,所以我將解決辦法貼在下面
問(wèn)題解答
回答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)來(lái)獲取>=選中行數(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. sql語(yǔ)句 - 如何在mysql中批量添加用戶?2. php - 數(shù)據(jù)庫(kù)表如果是null怎么替換為其他字段的值3. 怎么php怎么通過(guò)數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。4. javascript - mysql插入數(shù)據(jù)時(shí)怎樣避免與庫(kù)中的數(shù)據(jù)重復(fù)?5. mysql - JAVA怎么實(shí)現(xiàn)一個(gè)DAO同時(shí)實(shí)現(xiàn)查詢兩個(gè)實(shí)體類的結(jié)果集6. shell - Update query wrong in MySQL7. 事務(wù) - mysql共享鎖lock in share mode的實(shí)際使用場(chǎng)景8. mysql - 數(shù)據(jù)庫(kù)建字段,默認(rèn)值空和empty string有什么區(qū)別 1109. mysql - PHP定時(shí)通知、按時(shí)發(fā)布怎么做?10. mysql建表報(bào)錯(cuò),查手冊(cè)看不懂,求解?
