Java?導出Excel增加下拉框選項
excel對于下拉框較多選項的,需要使用隱藏工作簿來解決,使用函數(shù)取值來做選項
選項較少(一般少于5個):
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ù)據(jù)有效性加載在哪個單元格上。四個參數(shù)分別是:起始行、終止行、起始列、終止列 CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol); //數(shù)據(jù)有效性對象 return helper.createValidation(constraint, regions); }
選項較多
創(chuàng)建隱藏工作簿:
Sheet sheetHidden = wb.createSheet("Sheet2"); wb.setSheetHidden(1, true);
每一個列表占用一列
當然也可以每個列表使用一張工作簿,只用第一列。 這里是使用一個工作簿使用每個列,先26個字母,一般夠用了
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個下拉選項,直接創(chuàng)建行、列 row = sheetHidden.createRow(j); //創(chuàng)建數(shù)據(jù)行 // sheetHidden.setColumnWidth(j, 4000); //設置每列的列寬 row.createCell(0).setCellValue(dataList.get(j)); //設置對應單元格的值 } else { //非第1個下拉選項 int rowCount = sheetHidden.getLastRowNum(); if (j <= rowCount) { //前面創(chuàng)建過的行,直接獲取行,創(chuàng)建列 //獲取行,創(chuàng)建列 sheetHidden.getRow(j).createCell(index).setCellValue(dataList.get(j)); //設置對應單元格的值 } else { //未創(chuàng)建過的行,直接創(chuàng)建行、創(chuàng)建列 // sheetHidden.setColumnWidth(j, 4000); //設置每列的列寬 //創(chuàng)建行、創(chuàng)建列 sheetHidden.createRow(j).createCell(index).setCellValue(dataList.get(j)); //設置對應單元格的值 } } }
index 代表第幾個下拉框,也就是在隱藏工作簿的第幾列,dataList表示下拉框的內(nèi)容
創(chuàng)建公式:
String strFormula = "Sheet2!$" + arr[index] + "$1:$" + arr[index] + "$" + dataList.size();
Sheet2第A1到A5000作為下拉列表來源數(shù)據(jù)
xls和xlsx生成下拉框的選項不一樣
private static DataValidation setMoreDataValidation(Workbook wb, Sheet sheet, String strFormula, int startRow, int endRow, int startColumn, int endColumn) { DataValidation dataValidation; // 設置數(shù)據(jù)有效性加載在哪個單元格上,四個參數(shù)分別是:起始行、終止行、起始列、終止列 CellRangeAddressList regions = new CellRangeAddressList(startRow, endRow, startColumn, endColumn); if (wb instanceof XSSFWorkbook) { //獲取新sheet頁內(nèi)容 XSSFDataValidationConstraint constraint = new XSSFDataValidationConstraint(DataValidationConstraint.ValidationType.LIST, strFormula); // 設置數(shù)據(jù)有效性加載在哪個單元格上,四個參數(shù)分別是:起始行、終止行、起始列、終止列 // 數(shù)據(jù)有效性對象 DataValidationHelper help = new XSSFDataValidationHelper((XSSFSheet) sheet); dataValidation = help.createValidation(constraint, regions); dataValidation.setSuppressDropDownArrow(true); dataValidation.setShowErrorBox(true); } else { // 設置數(shù)據(jù)有效性加載在哪個單元格上。四個參數(shù)分別是:起始行、終止行、起始列、終止列 DVConstraint constraint = DVConstraint.createFormulaListConstraint(strFormula); dataValidation = new HSSFDataValidation(regions, constraint); dataValidation.setSuppressDropDownArrow(false); } dataValidation.setEmptyCellAllowed(true); dataValidation.setShowPromptBox(true); dataValidation.createErrorBox("Error", "請選擇下拉框中的數(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())); //超過255個報錯 } else { //String strFormula = "Sheet2!$A$1:$A$5000" ; //Sheet2第A1到A5000作為下拉列表來源數(shù)據(jù) String strFormula = "Sheet2!$" + arr[index] + "$1:$" + arr[index] + "$" + dataList.size(); //Sheet2第A1到A5000作為下拉列表來源數(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個下拉選項,直接創(chuàng)建行、列 row = sheetHidden.createRow(j); //創(chuàng)建數(shù)據(jù)行 // sheetHidden.setColumnWidth(j, 4000); //設置每列的列寬 row.createCell(0).setCellValue(dataList.get(j)); //設置對應單元格的值 } else { //非第1個下拉選項 int rowCount = sheetHidden.getLastRowNum(); if (j <= rowCount) { //前面創(chuàng)建過的行,直接獲取行,創(chuàng)建列 //獲取行,創(chuàng)建列 sheetHidden.getRow(j).createCell(index).setCellValue(dataList.get(j)); //設置對應單元格的值 } else { //未創(chuàng)建過的行,直接創(chuàng)建行、創(chuàng)建列 // sheetHidden.setColumnWidth(j, 4000); //設置每列的列寬 //創(chuàng)建行、創(chuàng)建列 sheetHidden.createRow(j).createCell(index).setCellValue(dataList.get(j)); //設置對應單元格的值 } } } index++; } } }
public static class DataValidationCell{ private int startRow; private int endRow; private int startColumn; private int endColumn; private List<String> dataList; }
到此這篇關于Java 導出Excel增加下拉框選項的文章就介紹到這了,更多相關Java 增加下拉框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用InputStream的available()能否用來判斷當前流是否讀取到文件
這篇文章主要介紹了使用InputStream的available()能否用來判斷當前流是否讀取到文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Java CountDownLatch計數(shù)器與CyclicBarrier循環(huán)屏障
CountDownLatch是一種同步輔助,允許一個或多個線程等待其他線程中正在執(zhí)行的操作的ASET完成。它允許一組線程同時等待到達一個共同的障礙點2023-04-04Spring-cloud 服務發(fā)現(xiàn)與消費(以ribbon為例)
這篇文章主要介紹了Spring-cloud 服務發(fā)現(xiàn)與消費(以ribbon為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04