修改excel 模板下载功能, 修改下拉框设置 改为隐藏sheet中拉取下拉框内容,避免单元格超255数值上限错误
This commit is contained in:
parent
29aeccb91d
commit
7181d11425
@ -3,10 +3,7 @@ package cn.iocoder.yudao.framework.excel.core.util;
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||
import org.apache.poi.ss.usermodel.DataValidation;
|
||||
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
||||
import org.apache.poi.ss.usermodel.DataValidationHelper;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
||||
|
||||
@ -49,11 +46,40 @@ public class SpinnerWriteHandler implements SheetWriteHandler {
|
||||
Sheet sheet = writeSheetHolder.getSheet();
|
||||
/// 开始设置下拉框
|
||||
DataValidationHelper helper = sheet.getDataValidationHelper();// 设置下拉框
|
||||
|
||||
//定义sheet的名称
|
||||
String hiddenName = "hidden";
|
||||
|
||||
//1.创建一个隐藏的sheet 名称为 hidden
|
||||
Workbook workbook = writeWorkbookHolder.getWorkbook();
|
||||
Sheet hidden = workbook.createSheet(hiddenName);
|
||||
Name category1Name = workbook.createName();
|
||||
category1Name.setNameName(hiddenName);
|
||||
|
||||
for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
|
||||
|
||||
/*** 起始行、终止行、起始列、终止列 **/
|
||||
CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, entry.getKey(), entry.getKey());
|
||||
|
||||
// 获取excel列名
|
||||
String excelLine = getExcelLine(entry.getKey());
|
||||
|
||||
// 循环赋值
|
||||
String[] values = entry.getValue();
|
||||
for (int i = 0, length = values.length; i < length; i++) {
|
||||
// 3:表示你开始的行数 3表示 你开始的列数
|
||||
Row row = hidden.getRow(i);
|
||||
if (row == null) {
|
||||
row = hidden.createRow(i);
|
||||
}
|
||||
row.createCell(entry.getKey()).setCellValue(values[i]);
|
||||
}
|
||||
// hidden!$H:$1:$H$50 sheet为hidden的 H1列开始H50行数据获取下拉数组
|
||||
String refers = hiddenName + "!$"+excelLine+
|
||||
"$1:$"+excelLine +"$"+ (values.length);
|
||||
|
||||
/*** 设置下拉框数据 **/
|
||||
DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
|
||||
DataValidationConstraint constraint = helper.createFormulaListConstraint(refers);
|
||||
DataValidation dataValidation = helper.createValidation(constraint, addressList);
|
||||
/*** 处理Excel兼容性问题 **/
|
||||
if (dataValidation instanceof XSSFDataValidation) {
|
||||
@ -65,5 +91,27 @@ public class SpinnerWriteHandler implements SheetWriteHandler {
|
||||
}
|
||||
sheet.addValidationData(dataValidation);
|
||||
}
|
||||
|
||||
//设置列为隐藏
|
||||
int hiddenIndex = workbook.getSheetIndex("hidden");
|
||||
if (!workbook.isSheetHidden(hiddenIndex)) {
|
||||
workbook.setSheetHidden(hiddenIndex, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回excel列标A-Z-AA-ZZ
|
||||
* @param num 列数
|
||||
* @return java.lang.String
|
||||
*/
|
||||
public static String getExcelLine(int num) {
|
||||
String line = "";
|
||||
int first = num/26;
|
||||
int second = num % 26;
|
||||
if (first>0) {
|
||||
line = (char)('A'+first-1)+"";
|
||||
}
|
||||
line += (char)('A'+second)+"";
|
||||
return line;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user