// poi導出苗沧,生成Workbook
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("生活老師導入","Sheet0", ExcelType.XSSF), ExportDormDTO.class, list);
// 設置下拉預選值
String[] dropDownValue = new String[]{"A", "B", "C", "D"};
String? dropDownSheetName= "生活評價";
XSSFDataValidationHelper dropDownValidationHelper = createXSSFDataValidationHelper(workbook, dropDownSheetName, dropDownValue);
DataValidationConstraint dropDOwnValidationConstraint = dropDownValidationHelper.createFormulaListConstraint(dropDownSheetName + "!$A$1:$A$" + dropDownValue.length);
Sheet firstSheet = workbook.getSheet("Sheet0");
CellRangeAddressList drowDownValueCoveringRowsAndCloumns = new CellRangeAddressList(2, firstSheet.getLastRowNum(), 5, 9);
XSSFDataValidation dropDownValidation =(XSSFDataValidation)dropDownValidationHelper.createValidation(dropDOwnValidationConstraint, drowDownValueCoveringRowsAndCloumns);
firstSheet.addValidationData(dropDownValidation);
downLoadExcel("生活老師評價導入.xlsx", response, workbook);
-------------------------------------------------------------------------------
下面是固定寫法财边,是上面代碼中使用到的局部方法
// 流導出
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
? ? response.reset();
? ? response.setContentType("application/x-download");
? ? response.setHeader("Content-Disposition",
? ? ? ? ? ? "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
? ? workbook.write(response.getOutputStream());
}
// 生成預選值的sheet頁
private static XSSFDataValidationHelper createXSSFDataValidationHelper(Workbook workbook, String name, String[] strList) {
? ? Sheet sheet = workbook.createSheet(name);
? ? // 循環(huán)往該sheet中設置添加下拉列表的值
? ? for (int i = 0; i < strList.length; i++) {
? ? ? ? Row row = sheet.createRow(i);
? ? ? ? Cell cell = row.createCell((int) 0);
? ? ? ? cell.setCellValue(strList[i]);
? ? }
? ? workbook.setSheetHidden(workbook.getSheetIndex(name), true);
? ? XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet) workbook.getSheet(name));
? ? dvHelper.createFormulaListConstraint(name + "!$A$1:$A$" + strList.length);
? ? return dvHelper;
}
亞馬遜測評 www.yisuping.com