工具类
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.util.CellRangeAddressList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @ClassName ExcelOut
* @Description: TODO
* @Author xianyu
* @Date 2022/01/14
* @Version V1.0
**/
public class ExcelOut {
/**
* excel导出,有码值的数据使用下拉框展示。解决下拉框最多255个字符的问题。
* 原理为新建一个隐藏状态的sheet页,用来存储下拉框的值。
*
* @param wb 工作簿 HSSFWorkbook
* @param boxMap 码值集合
* @param rows 正常sheet页数据,用来指定哪些行需要添加下拉框
* @param i 多个码值需要添加下拉,隐藏状态的sheet页名称不能重复,添加i值区分。
* @param colToIndex 用来指定哪些列需要添加下拉框
* @return dataValidation
*/
public static List<HSSFDataValidation> createBox1(HSSFWorkbook wb,Map<String, String> boxMap, int rows, int i, int colToIndex) {
List<HSSFDataValidation> list =new ArrayList<>();
HSSFDataValidation dataValidation = null;
String cols1 = "";
//查询码值集合,获取当前列的码值。
if (null != boxMap.get("ZZLX")) {
cols1 = boxMap.get("ZZLX");
}
//第一列 组织类型
String str1[] = cols1.split(",");
//指定0-9行,0-0列为下拉框
CellRangeAddressList cas = new CellRangeAddressList(1 , 99999 , 0 , 0);
//创建下拉数据列
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(str1);
//将下拉数据放入下拉框
dataValidation = new HSSFDataValidation(cas, dvConstraint);
list.add(dataValidation);
String cols2 = "";
//查询码值集合,获取当前列的码值。
if (null != boxMap.get("GLQY")) {
cols2 = boxMap.get("GLQY");
}
//新建隐藏状态的sheet,用来存储码值。
if (cols2.length() > 0 && null != cols2) {
//管理区域
String str[] = cols2.split(",");
//创建sheet页
HSSFSheet sheet = wb.createSheet("hidden" + i);
//向创建的sheet页添加码值数据。
for (int i1 = 0; i1 < str.length; i1++) {
HSSFRow row = sheet.createRow(i1);
HSSFCell cell = row.createCell((int) 0);
cell.setCellValue(str[i1]);
}
//将码值sheet页做成excel公式
Name namedCell = wb.createName();
namedCell.setNameName("hidden" + i);
namedCell.setRefersToFormula("hidden" + i + "!$A$1:$A$" + str.length);
//确定要在哪些单元格生成下拉框
dvConstraint = DVConstraint.createFormulaListConstraint("hidden" + i);
CellRangeAddressList regions = new CellRangeAddressList(1, rows, colToIndex, colToIndex);
dataValidation = new HSSFDataValidation(regions, dvConstraint);
list.add(dataValidation);
//隐藏码值sheet页
int sheetNum = wb.getNumberOfSheets();
for (int n = 1; n < sheetNum; n++) {
wb.setSheetHidden(n, true);
}
}
String cols3 = "";
//查询码值集合,获取当前列的码值。
if (null != boxMap.get("SJZZ")) {
cols3 = boxMap.get("SJZZ");
}
//上级组织
String str[] = cols3.split(",");
//指定0-9行,0-0列为下拉框
CellRangeAddressList cas2 = new CellRangeAddressList(1 , 99999 , 3 , 3);
//创建下拉数据列
DVConstraint dvConstraint2 = DVConstraint.createExplicitListConstraint(str);
//将下拉数据放入下拉框
dataValidation = new HSSFDataValidation(cas2, dvConstraint2);
list.add(dataValidation);
return list;
}
}
调用
@Override
public void importExcel(HttpServletResponse response) throws UnsupportedEncodingException {
log.info("导入模板下载");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet();
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("组织类型");
row.createCell(2).setCellValue("管理区域");
row.createCell(1).setCellValue("组织名称");
row.createCell(3).setCellValue("上级组织");
Map<String, String> boxMap = new HashMap<>();
boxMap.put("ZZLX", "运营监管,养老机构,养老院,服务中心,服务商,评估机构,志愿者团队");
//查询所有区域
List<String> areaList = sysAreaMapper.findAreaName();
String areaName = StringUtils.join(areaList.toArray(), ",");
boxMap.put("GLQY", areaName);
//查询现有机构
List<String> nameList = organTreeMapper.findNameList();
String organName = StringUtils.join(nameList.toArray(), ",");
boxMap.put("SJZZ", organName);
int i = 0;
List<HSSFDataValidation> dataValidationList = ExcelOut.createBox1(hssfWorkbook, boxMap, 99999, i, 1);
if (!CollectionUtils.isEmpty(dataValidationList)) {
for (HSSFDataValidation hssfDataValidation : dataValidationList) {
sheet.addValidationData(hssfDataValidation);
}
}
String filePath="组织机构导入模板.xls";
//通过浏览器下载
response.reset();//清除buffer缓存
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="+ new String(filePath.getBytes("UTF-8"), "ISO-8859-1"));// 定义文件名
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", " 0");
try {
OutputStream output;
try {
output=response.getOutputStream();
BufferedOutputStream bufferOutput =new BufferedOutputStream(output);
bufferOutput.flush();
hssfWorkbook.write(bufferOutput);
bufferOutput.close();
} catch (Exception e) {
}
} catch (Exception e) {
}
}