1. Wenn in Excel nur wenige Dropdown-Arrays vorhanden sind (ca. 0–20), k?nnen Sie diese auf folgende Weise exportieren:
/**
* Excel API
*/
@SuppressWarnings("resource")
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet("xxxx");
/**
* 初始化參數(shù)
*/
Map<String, String> map = new HashMap<String, String>(); // 查詢時用的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ù)驗證
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. Das Problem besteht darin, dass bei zu vielen Dropdown-Arrays die folgende Ausnahmemeldung im POI angezeigt wird:
String literals in formulas can't be bigger than 255 characters ASCII
Die L?sung für dieses Problem ist online nicht leicht zu finden, daher werde ich die L?sung unten ver?ffentlichen
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
下面是解決辦法:
/**
* Excel API
*/
@SuppressWarnings("resource")
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet("spu導(dǎo)入模板");
/**
* 初始化參數(shù)
*/
Map<String, String> map = new HashMap<String, String>(); // 查詢時用的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ù)驗證
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ù)相對應(yīng)來獲取>=選中行數(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(?)時。以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);
請注意上面的這倆個地方: