給自己打個廣告,最近弄了個Excel的工具代兵,支持下拉框尼酿,級聯(lián)下拉框,隱藏Sheet奢人,多線程大數(shù)據(jù)量導(dǎo)出谓媒,生產(chǎn)者消費(fèi)者模式讀取,代碼大部分都有注釋何乎,有興趣的朋友可以看看句惯,覺得不錯可以幫我弄個star什么的:smile:
方式
- 直接給單元格添加下拉框
- 使用隱藏Sheet的方式生成下拉框
直接添加下拉框
/**
* 創(chuàng)建下拉列表選項(xiàng)(單元格下拉框數(shù)據(jù)小于255字節(jié)時使用)
*
* @param sheet 所在Sheet頁面
* @param values 下拉框的選項(xiàng)值
* @param firstRow 起始行(從0開始)
* @param lastRow 終止行(從0開始)
* @param firstCol 起始列(從0開始)
* @param lastCol 終止列(從0開始)
*/
public void createDropDownList(Sheet sheet, String[] values, int firstRow, int lastRow, int firstCol, int lastCol) {
DataValidationHelper helper = sheet.getDataValidationHelper();
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
DataValidationConstraint constraint = helper.createExplicitListConstraint(values);
DataValidation dataValidation = helper.createValidation(constraint, addressList);
if (dataValidation instanceof HSSFDataValidation ) {
dataValidation.setSuppressDropDownArrow(false);
} else {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
}
sheet.addValidationData(dataValidation);
}
需要注意的是,這種方式下拉框數(shù)據(jù)的大小不能超過255個字節(jié)支救,否則會報異常抢野。需要使用隱藏Sheet的方式解決
隱藏Sheet的方式
/**
* 隱藏Sheet方式創(chuàng)建下拉框(單元格下拉框數(shù)據(jù)大于255字節(jié)時使用)
*
* @param sheet 需要添加下拉框的Sheet
* @param firstRow 起始行
* @param firstCol 其實(shí)列
* @param endRow 終止行
* @param endCol 終止列
* @param dataArray 下拉框數(shù)組
* @param wbCreat 所在excel的WorkBook,用于創(chuàng)建隱藏Sheet
* @param hidddenSheetName 隱藏Sheet的名稱
* @return
*/
public void createDropDownListWithHiddenSheet(Sheet sheet, int firstRow,
int firstCol, int endRow,
int endCol, String[] dataArray,
Workbook wbCreat,
String hidddenSheetName) {
Sheet hidden = wbCreat.createSheet(hidddenSheetName);
Cell cell = null;
for (int i = 0, length = dataArray.length; i < length; i++) {
String name = dataArray[i];
Row row = hidden.createRow(i);
cell = row.createCell(0);
cell.setCellValue(name);
}
Name namedCell = wbCreat.createName();
namedCell.setNameName(hidddenSheetName);
namedCell.setRefersToFormula(hidddenSheetName + "!$A$1:$A$" + dataArray.length);
//sheet設(shè)置為隱藏
wbCreat.setSheetHidden(wbCreat.getSheetIndex(hidden), true);
//加載數(shù)據(jù),將名稱為hidden的
DataValidationConstraint constraint = null;
// 設(shè)置數(shù)據(jù)有效性加載在哪個單元格上,四個參數(shù)分別是:起始行各墨、終止行指孤、起始列、終止列
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, endRow, firstCol,
endCol);
// 創(chuàng)建 DataValidation
DataValidation validation = null;
if (sheet instanceof XSSFSheet || sheet instanceof SXSSFSheet) {
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
constraint = dvHelper.createFormulaListConstraint(hidddenSheetName);
validation = dvHelper.createValidation(constraint, addressList);
} else {
constraint = DVConstraint.createFormulaListConstraint(hidddenSheetName);
validation = new HSSFDataValidation(addressList, constraint);
}
if (dataValidation instanceof HSSFDataValidation ) {
dataValidation.setSuppressDropDownArrow(false);
} else {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
}
sheet.addValidationData(validation);
}
在解析Excel數(shù)據(jù)的時候要注意忽略隱藏Sheet的數(shù)據(jù)