1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.xssf.usermodel.*;
import java.util.List; import java.util.Map; import java.util.Set;
public class ExcelValidationUtils {
private static final int minRow = 1;
private static final int maxRow = 100;
private static final boolean debugHideSheet = true;
public static XSSFWorkbook createOneXLSX() { return new XSSFWorkbook(); }
public static XSSFSheet addOneSheet(XSSFWorkbook wb, String sheetName, String[] headers) { XSSFSheet st = wb.createSheet(sheetName); CellStyle style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); Font fontStyle = wb.createFont(); fontStyle.setFontName("微软雅黑"); fontStyle.setFontHeightInPoints((short) 12); style.setFont(fontStyle); XSSFDataFormat format = wb.createDataFormat(); style.setDataFormat(format.getFormat("@")); XSSFRow row = st.createRow(0); st.createFreezePane(0, 1, 0, 1); for (int i = 0; i < headers.length; i++) { String value = headers[i]; XSSFCell cell = row.createCell(i); st.setColumnWidth(i, value.length() * 1000); cell.setCellStyle(style); st.setDefaultColumnStyle(i, style); cell.setCellValue(value); } return st; }
public static XSSFSheet addLinkageDataValidation(XSSFWorkbook wb, XSSFSheet targetSheet, Map<String, List<String>> linkageData, int parentCol, int childCol, String parentColIdentifier) { XSSFSheet hideSt = wb.createSheet(); wb.setSheetHidden(wb.getSheetIndex(hideSt), !debugHideSheet); int rowId = 0; Set<String> keySet = linkageData.keySet(); for (String parent : keySet) { List<String> sonList = linkageData.get(parent); XSSFRow row = hideSt.createRow(rowId++); row.createCell(0).setCellValue(parent); for (int i = 0; i < sonList.size(); i++) { XSSFCell cell = row.createCell(i + 1); cell.setCellValue(sonList.get(i)); } String range = getRange(1, rowId, sonList.size()); Name name = wb.createName(); name.setNameName(parent); String formula = hideSt.getSheetName() + "!" + range; name.setRefersToFormula(formula); } XSSFDataValidationHelper helper = new XSSFDataValidationHelper(targetSheet);
Name name = wb.createName(); name.setNameName(hideSt.getSheetName()); name.setRefersToFormula(hideSt.getSheetName() + "!$A$1:$A$" + keySet.size()); DataValidation parentValidation = helper.createValidation(helper.createFormulaListConstraint(hideSt.getSheetName()), new CellRangeAddressList(minRow, maxRow, parentCol, parentCol)); parentValidation.createErrorBox("错误", "请选择正确的父级类型"); parentValidation.setShowErrorBox(true); targetSheet.addValidationData(parentValidation);
for (int i = minRow; i < maxRow; i++) { DataValidation childValidation = helper.createValidation(helper.createFormulaListConstraint("INDIRECT(" + parentColIdentifier + "" + (i + 1) + ")"), new CellRangeAddressList(i, i, childCol, childCol)); childValidation.createErrorBox("错误", "请选择正确的子级类型"); childValidation.setShowErrorBox(true); childValidation.setSuppressDropDownArrow(true); targetSheet.addValidationData(childValidation); }
return hideSt; }
public static void addSimpleDropDownListValidation(XSSFSheet st, String[] dropDownList, int firstCol, int lastCol) { XSSFDataValidationHelper helper = new XSSFDataValidationHelper(st); XSSFDataValidationConstraint constraint = (XSSFDataValidationConstraint) helper.createExplicitListConstraint(dropDownList); CellRangeAddressList addressList = new CellRangeAddressList(minRow, maxRow, firstCol, lastCol); XSSFDataValidation validation = (XSSFDataValidation) helper.createValidation(constraint, addressList); validation.setSuppressDropDownArrow(true); validation.setShowErrorBox(true); st.addValidationData(validation); }
public static void addComplexDropDownListValidation(XSSFWorkbook wb, XSSFSheet st, String[] dropDownList, int firstCol, int lastCol) { XSSFSheet hideSt = wb.createSheet(); wb.setSheetHidden(wb.getSheetIndex(hideSt), !debugHideSheet); XSSFDataValidationHelper helper = new XSSFDataValidationHelper(st); for (int i = 0, length = dropDownList.length; i < length; i++) { String value = dropDownList[i]; XSSFRow row = hideSt.createRow(i); XSSFCell cell = row.createCell(0); cell.setCellValue(value); } Name name = wb.createName(); name.setNameName(hideSt.getSheetName()); name.setRefersToFormula(hideSt.getSheetName() + "!$A$1:$A$" + dropDownList.length); DataValidation parentValidation = helper.createValidation(helper.createFormulaListConstraint(hideSt.getSheetName()), new CellRangeAddressList(minRow, maxRow, firstCol, lastCol)); parentValidation.createErrorBox("错误", "请选择正确的类型"); parentValidation.setShowErrorBox(true); st.addValidationData(parentValidation); }
private static String getRange(int offset, int rowId, int colCount) { char start = (char) ('A' + offset); if (colCount <= 25) { char end = (char) (start + colCount - 1); return "$" + start + "$" + rowId + ":$" + end + "$" + rowId; } else { char endPrefix = 'A', endSuffix; if ((colCount - 25) / 26 == 0 || colCount == 51) { if ((colCount - 25) % 26 == 0) { endSuffix = (char) ('A' + 25); } else { endSuffix = (char) ('A' + (colCount - 25) % 26 - 1); } } else { if ((colCount - 25) % 26 == 0) { endSuffix = (char) ('A' + 25); endPrefix = (char) (endPrefix + (colCount - 25) / 26 - 1); } else { endSuffix = (char) ('A' + (colCount - 25) % 26 - 1); endPrefix = (char) (endPrefix + (colCount - 25) / 26); } } return "$" + start + "$" + rowId + ":$" + endPrefix + endSuffix + "$" + rowId; } } }
|