Java?導(dǎo)出Excel增加下拉框選項(xiàng)
excel對(duì)于下拉框較多選項(xiàng)的,需要使用隱藏工作簿來(lái)解決,使用函數(shù)取值來(lái)做選項(xiàng)
選項(xiàng)較少(一般少于5個(gè)):
private static DataValidation setFewDataValidation(Sheet sheet, String[] textList, int firstRow, int endRow, int firstCol, int endCol) { DataValidationHelper helper = sheet.getDataValidationHelper(); //加載下拉列表內(nèi)容 DataValidationConstraint constraint = helper.createExplicitListConstraint(textList); constraint.setExplicitListValues(textList); //設(shè)置數(shù)據(jù)有效性加載在哪個(gè)單元格上。四個(gè)參數(shù)分別是:起始行、終止行、起始列、終止列 CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol); //數(shù)據(jù)有效性對(duì)象 return helper.createValidation(constraint, regions); }
選項(xiàng)較多
創(chuàng)建隱藏工作簿:
Sheet sheetHidden = wb.createSheet("Sheet2"); wb.setSheetHidden(1, true);
每一個(gè)列表占用一列
當(dāng)然也可以每個(gè)列表使用一張工作簿,只用第一列。 這里是使用一個(gè)工作簿使用每個(gè)列,先26個(gè)字母,一般夠用了
String[] arr = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
for (int j = 0; j < dataList.size(); j++) { if (index == 0) { //第1個(gè)下拉選項(xiàng),直接創(chuàng)建行、列 row = sheetHidden.createRow(j); //創(chuàng)建數(shù)據(jù)行 // sheetHidden.setColumnWidth(j, 4000); //設(shè)置每列的列寬 row.createCell(0).setCellValue(dataList.get(j)); //設(shè)置對(duì)應(yīng)單元格的值 } else { //非第1個(gè)下拉選項(xiàng) int rowCount = sheetHidden.getLastRowNum(); if (j <= rowCount) { //前面創(chuàng)建過(guò)的行,直接獲取行,創(chuàng)建列 //獲取行,創(chuàng)建列 sheetHidden.getRow(j).createCell(index).setCellValue(dataList.get(j)); //設(shè)置對(duì)應(yīng)單元格的值 } else { //未創(chuàng)建過(guò)的行,直接創(chuàng)建行、創(chuàng)建列 // sheetHidden.setColumnWidth(j, 4000); //設(shè)置每列的列寬 //創(chuàng)建行、創(chuàng)建列 sheetHidden.createRow(j).createCell(index).setCellValue(dataList.get(j)); //設(shè)置對(duì)應(yīng)單元格的值 } } }
index 代表第幾個(gè)下拉框,也就是在隱藏工作簿的第幾列,dataList表示下拉框的內(nèi)容
創(chuàng)建公式:
String strFormula = "Sheet2!$" + arr[index] + "$1:$" + arr[index] + "$" + dataList.size();
Sheet2第A1到A5000作為下拉列表來(lái)源數(shù)據(jù)
xls和xlsx生成下拉框的選項(xiàng)不一樣
private static DataValidation setMoreDataValidation(Workbook wb, Sheet sheet, String strFormula, int startRow, int endRow, int startColumn, int endColumn) { DataValidation dataValidation; // 設(shè)置數(shù)據(jù)有效性加載在哪個(gè)單元格上,四個(gè)參數(shù)分別是:起始行、終止行、起始列、終止列 CellRangeAddressList regions = new CellRangeAddressList(startRow, endRow, startColumn, endColumn); if (wb instanceof XSSFWorkbook) { //獲取新sheet頁(yè)內(nèi)容 XSSFDataValidationConstraint constraint = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST, strFormula); // 設(shè)置數(shù)據(jù)有效性加載在哪個(gè)單元格上,四個(gè)參數(shù)分別是:起始行、終止行、起始列、終止列 // 數(shù)據(jù)有效性對(duì)象 DataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet) sheet); dataValidation = help.createValidation(constraint, regions); dataValidation.setSuppressDropDownArrow(true); dataValidation.setShowErrorBox(true); } else { // 設(shè)置數(shù)據(jù)有效性加載在哪個(gè)單元格上。四個(gè)參數(shù)分別是:起始行、終止行、起始列、終止列 DVConstraint constraint = DVConstraint.createFormulaListConstraint(strFormula); dataValidation = new HSSFDataValidation(regions, constraint); dataValidation.setSuppressDropDownArrow(false); } dataValidation.setEmptyCellAllowed(true); dataValidation.setShowPromptBox(true); dataValidation.createErrorBox("Error", "請(qǐng)選擇下拉框中的數(shù)據(jù)"); dataValidation.createPromptBox("提示", "只能選擇下拉框里面的數(shù)據(jù)"); return dataValidation; }
加入工作簿:
sheet.addValidationData()
完整代碼:
private static void setValidationDate(Workbook wb, Sheet sheet, List<DataValidationCell> dataValidationCellList) { if (dataValidationCellList.isEmpty()) { return; } String[] arr = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; int index = 0; Row row; Sheet sheetHidden = wb.createSheet("Sheet2"); wb.setSheetHidden(1, true); for (DataValidationCell dataValidationCell : dataValidationCellList) { List<String> dataList = dataValidationCell.getDataList(); if (CollectionUtils.isEmpty(dataList)) { continue; } if (dataList.size() <= 5) { sheet.addValidationData(setFewDataValidation(sheet, dataList.toArray(new String[0]), dataValidationCell.getStartRow(), dataValidationCell.getEndRow(), dataValidationCell.getStartColumn(), dataValidationCell.getEndColumn())); //超過(guò)255個(gè)報(bào)錯(cuò) } else { //String strFormula = "Sheet2!$A$1:$A$5000" ; //Sheet2第A1到A5000作為下拉列表來(lái)源數(shù)據(jù) String strFormula = "Sheet2!$" + arr[index] + "$1:$" + arr[index] + "$" + dataList.size(); //Sheet2第A1到A5000作為下拉列表來(lái)源數(shù)據(jù) sheet.addValidationData(setMoreDataValidation(wb, sheet, strFormula, dataValidationCell.getStartRow(), dataValidationCell.getEndRow(), dataValidationCell.getStartColumn(), dataValidationCell.getEndColumn())); //下拉列表元素很多的情況 //2、生成sheet2內(nèi)容 for (int j = 0; j < dataList.size(); j++) { if (index == 0) { //第1個(gè)下拉選項(xiàng),直接創(chuàng)建行、列 row = sheetHidden.createRow(j); //創(chuàng)建數(shù)據(jù)行 // sheetHidden.setColumnWidth(j, 4000); //設(shè)置每列的列寬 row.createCell(0).setCellValue(dataList.get(j)); //設(shè)置對(duì)應(yīng)單元格的值 } else { //非第1個(gè)下拉選項(xiàng) int rowCount = sheetHidden.getLastRowNum(); if (j <= rowCount) { //前面創(chuàng)建過(guò)的行,直接獲取行,創(chuàng)建列 //獲取行,創(chuàng)建列 sheetHidden.getRow(j).createCell(index).setCellValue(dataList.get(j)); //設(shè)置對(duì)應(yīng)單元格的值 } else { //未創(chuàng)建過(guò)的行,直接創(chuàng)建行、創(chuàng)建列 // sheetHidden.setColumnWidth(j, 4000); //設(shè)置每列的列寬 //創(chuàng)建行、創(chuàng)建列 sheetHidden.createRow(j).createCell(index).setCellValue(dataList.get(j)); //設(shè)置對(duì)應(yīng)單元格的值 } } } index++; } } }
public static class DataValidationCell{ private int startRow; private int endRow; private int startColumn; private int endColumn; private List<String> dataList; }
到此這篇關(guān)于Java 導(dǎo)出Excel增加下拉框選項(xiàng)的文章就介紹到這了,更多相關(guān)Java 增加下拉框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java構(gòu)建菜單樹(shù)的實(shí)現(xiàn)示例
本文主要介紹了Java構(gòu)建菜單樹(shù)的實(shí)現(xiàn)示例,像一級(jí)菜單,二級(jí)菜單,三級(jí)菜單甚至更多層級(jí)的菜單,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05使用InputStream的available()能否用來(lái)判斷當(dāng)前流是否讀取到文件
這篇文章主要介紹了使用InputStream的available()能否用來(lái)判斷當(dāng)前流是否讀取到文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06java操作excel導(dǎo)入導(dǎo)出的3種方式
項(xiàng)目需要,要實(shí)現(xiàn)一個(gè)導(dǎo)入導(dǎo)出excel的功能,于是任務(wù)驅(qū)動(dòng)著我學(xué)習(xí)到了POI、easypoi和easyexcel這3個(gè)java操作Excel的工具,下面這篇文章主要給大家介紹了關(guān)于java操作excel導(dǎo)入導(dǎo)出的3種方式,需要的朋友可以參考下2023-05-05Java CountDownLatch計(jì)數(shù)器與CyclicBarrier循環(huán)屏障
CountDownLatch是一種同步輔助,允許一個(gè)或多個(gè)線程等待其他線程中正在執(zhí)行的操作的ASET完成。它允許一組線程同時(shí)等待到達(dá)一個(gè)共同的障礙點(diǎn)2023-04-04詳細(xì)講述Java中的對(duì)象轉(zhuǎn)型
在本篇文章里我們給大家詳細(xì)分享了關(guān)于Java中的對(duì)象轉(zhuǎn)型的知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2018-10-10Spring-cloud 服務(wù)發(fā)現(xiàn)與消費(fèi)(以ribbon為例)
這篇文章主要介紹了Spring-cloud 服務(wù)發(fā)現(xiàn)與消費(fèi)(以ribbon為例),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例
這篇文章主要介紹了java編程隊(duì)列數(shù)據(jù)結(jié)構(gòu)代碼示例,簡(jiǎn)單介紹了隊(duì)列的相關(guān)基礎(chǔ)知識(shí),然后通過(guò)實(shí)例向大家展示其實(shí)現(xiàn)方法,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Java圖片處理開(kāi)源框架Thumbnailator
這篇文章主要為大家詳細(xì)介紹了Java圖片處理開(kāi)源框架Thumbnailator的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05