excel下拉框
@RequestMapping("xiala")
public void xiala(HttpServletResponse response){
String fileName = "僵尸表";
try{
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition","attachment;filename=" +
URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20") + ".xlsx");
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
String[] dropdownOptions = {"Option 1", "Option 2", "Option 3"};
CellRangeAddressList addressList = new CellRangeAddressList(1, 100, 0, 0);
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(dropdownOptions);
DataValidation dataValidation = validationHelper.createValidation(constraint, addressList);
sheet.addValidationData(dataValidation);
String[] publishType = {"个人", "企业", "33"};
CellRangeAddressList publishTypeList = new CellRangeAddressList(1, 100, 1, 1);
DataValidationConstraint publishTypeConstraint = validationHelper.createExplicitListConstraint(publishType);
DataValidation publishValidation = validationHelper.createValidation(publishTypeConstraint, publishTypeList);
sheet.addValidationData(publishValidation);
OutputStream os = new BufferedOutputStream(response.getOutputStream());
workbook.write(os);
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
System.out.println("Excel文件创建成功,包含下拉框!");
}
excel sheet的复制
@RequestMapping("list")
public void list(HttpServletResponse response) throws Exception{
System.out.println("------ 开始下载模板 ------");
String fileName = URLEncoder.encode("名单导入模板","UTF-8");
response.setHeader("Content-Disposition","attachment;filename=" + fileName + ".xlsx");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/template.xlsx");
Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
Workbook destWorkbook = new XSSFWorkbook();
Sheet sourceSheet = sourceWorkbook.getSheetAt(0);
Sheet destSheet = destWorkbook.createSheet(sourceSheet.getSheetName());
for (int rowIndex = 0; rowIndex <= sourceSheet.getLastRowNum(); rowIndex++) {
Row sourceRow = sourceSheet.getRow(rowIndex);
Row destRow = destSheet.createRow(rowIndex);
if (sourceRow != null) {
for (int colIndex = 0; colIndex < sourceRow.getLastCellNum(); colIndex++) {
Cell sourceCell = sourceRow.getCell(colIndex);
Cell destCell = destRow.createCell(colIndex);
if (sourceCell != null) {
CellStyle newCellStyle = destWorkbook.createCellStyle();
newCellStyle.cloneStyleFrom(sourceCell.getCellStyle());
destCell.setCellStyle(newCellStyle);
switch (sourceCell.getCellType()) {
case STRING:
destCell.setCellValue(sourceCell.getStringCellValue());
break;
case NUMERIC:
destCell.setCellValue(sourceCell.getNumericCellValue());
break;
case BOOLEAN:
destCell.setCellValue(sourceCell.getBooleanCellValue());
break;
case FORMULA:
destCell.setCellFormula(sourceCell.getCellFormula());
break;
case BLANK:
destCell.setBlank();
break;
default:
break;
}
}
}
}
}
OutputStream os = new BufferedOutputStream(response.getOutputStream());
destWorkbook.write(os);
os.flush();
os.close();
}
excel列之间的级联