1、調用方法
CommonUtils.genearteOtherSheet2(sheet, associatePartyTypeIndex, "股東類型", associatePartyType, mainSheet, false, workbook, 2,"");
CommonUtils.genearteOtherSheet2(sheet, legalIndex, "法人", legal, mainSheet, true, workbook, 3, "C");
CommonUtils.genearteOtherSheet2(sheet, legalIndex, "法人", legal, mainSheet, true, workbook, 3, "C");
2、具體方法
/** * 查詢下拉列表值,寫入到隱藏sheet頁中 * @Title: genearteOtherSheet * @author hegg * @date 2019年12月30日 下午5:03:15 * @return 返回類型 void * @param sheet 為數據字典sheet頁 * @param index 為數據字典對應的值,保存在sheet頁第幾列,同一個sheet頁多次調用本方法(index不可以重復,否則后面的列會覆蓋前面的列) * @param columnDesc 為數據字典sheet頁第幾列保存對應的值的描述 * @param selectList 下拉框內容 * @param mainSheet 為主sheet頁用來實現下拉框作為展示 * @param isjilian * @param workbook 為當前Workbook * @param mainParentIndex 為主sheet頁mainSheet第幾列需要被設置為下拉框 * @param father 當前列為子級下拉框的內容受父級哪一列的影響 */ public static void genearteOtherSheet2(Sheet sheet, int index, String columnDesc, String[] selectList, XSSFSheet mainSheet, boolean isjilian, Workbook workbook, int mainParentIndex, String father) { Row titleRow = null; // 創建下拉列表值存儲工作表 int totalRowNumber = sheet.getLastRowNum();//獲取sheet頁有效行 if(totalRowNumber == 0){ titleRow = sheet.createRow(0); } else { titleRow = sheet.getRow(0); } Cell titleCell = titleRow.createCell(index); titleCell.setCellValue(columnDesc); for (int i = 0; i < selectList.length; i++) { Row row = null; if(totalRowNumber >= i+1){ row = sheet.getRow(i+1); } else { row = sheet.createRow(i+1); } Cell cell = row.createCell(index); cell.setCellValue(selectList[i]); } // 將下標轉換為字母0=A,1=B, String word = getIndexWord(index + 1); //設置名稱為typelist的sheet頁中哪些單元格的內容為下拉框,例如:=Sheet1!$C$2:$C$8意思是將C列第2行至第8行的7個單元格作為下拉框的內容 String formula = "typelist!$" + word + "$2:$" + word + "$" + (selectList.length + 1); if(isjilian){ // 名稱管理器設置name Name naturalName = workbook.createName(); naturalName.setNameName(columnDesc);// setNameName方法中參數必須父級內容一樣 naturalName.setRefersToFormula(formula); // 證件類型和股東類型為級聯關系,這里取mainSheet頁中 股東類型 所在列的下標並轉換為字母,因為 證件類型 取決於 股東類型,最后面的2代表excel中第多少行 //當前列為子級下拉框的內容受父級哪一列的影響 String indirectFormula = "INDIRECT($" + father + "2)";//=INDIRECT($C2) mainSheet.addValidationData(SetDataValidation(workbook, indirectFormula, 1, mainParentIndex, 1, mainParentIndex)); } else { // 設置下拉列表值綁定到主sheet頁具體哪個單元格起作用 mainSheet.addValidationData(SetDataValidation(workbook, formula, 1, mainParentIndex, 1, mainParentIndex)); } }
3、涉及的方法
/** * 設置下拉列表值綁定到主sheet頁具體哪個單元格起作用 * @Title: SetDataValidation * @author hegg * @date 2019年12月30日 下午5:03:37 * @return 返回類型 DataValidation * @param */ public static DataValidation SetDataValidation(Workbook workbook, String strFormula, int firstRow, int firstCol, int endRow, int endCol) { // 表示A列1-59行作為下拉列表來源數據 // String formula = "typelist!$A$1:$A$59" ; // 原順序為 起始行 起始列 終止行 終止列 CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); DataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet)workbook.getSheet("typelist")); DataValidationConstraint formulaListConstraint = dvHelper.createFormulaListConstraint(strFormula); DataValidation dataValidation = dvHelper.createValidation(formulaListConstraint, regions); return dataValidation; }
/** * 將下標轉換為字母,調用該方法時必須+1 * @Title: getIndexWord * @author hegg * @date 2020年1月1日 下午8:59:14 * @return 返回類型 String * @param */ public static String getIndexWord(Integer i){ String[] word = new String[]{"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 d = i/26; int x = i%26; String w = ""; if(x == 0){ if(d>1){ w= word[d-2] + "" + word[25]; } else { w = word[i-1]; } } else { if(d>=1){ w= word[d-1] + "" + word[x-1]; } else { w = word[x-1]; } } Map<Integer, String> a = new LinkedHashMap<Integer, String>(); a.put(i, w); return a.get(i); }