导出Excel设置某一列下拉框
public static void setSubListExcel(String[] subContent,XSSFSheet sheet,Integer firstRow,Integer lastRow,Integer firstCol,Integer lastCol) {
XSSFDataValidationHelper helper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint constraint = (XSSFDataValidationConstraint)helper.createExplicitListConstraint(subContent);
CellRangeAddressList addressList = new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);
XSSFDataValidation validation = (XSSFDataValidation)helper.createValidation(constraint, addressList);
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);
}
导出Excel添加图片
@Override
public void exportListExcelImages(HttpServletResponse response, String ids, MultipartFile file) {
ExcelWriter writer = ExcelUtil.getWriter(true);
int imagesHeight = 15;
int imageswidth = 6;
try {
LambdaQueryWrapper<StartRun> wrapper = new LambdaQueryWrapper<>();
List<Long> collect = Arrays.stream(ids.split(",")).map(a -> Long.parseLong(a)).collect(Collectors.toList());
wrapper.in(StartRun::getId,collect);
wrapper.orderBy(true,false,StartRun::getStartDate);
List<StartRun> startRuns = startRunMapper.selectList(wrapper);
List<StartRunExportVO> startRunExportVOS = new ArrayList<>();
int seq = 1;
StartRunExportVO startRunExportVO;
for(StartRun startRun : startRuns) {
startRunExportVO = new StartRunExportVO();
BeanUtils.copyProperties(startRun,startRunExportVO);
startRunExportVO.setSeq(seq++);
startRunExportVO.setReconnectionStr(startRun.getReconnectionRate().multiply(new BigDecimal("100"))+"%");
startRunExportVOS.add(startRunExportVO);
}
List<String> headList = CollUtil.newArrayList("序号", "日期", "单租", "重联", "合计", "重联比");
byte[] bytes = file.getBytes();
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition","attachment;filename=passengerFlow.xlsx");
response.setCharacterEncoding("utf-8");
XSSFSheet sheet = (XSSFSheet)writer.getSheet();
XSSFWorkbook workbook = sheet.getWorkbook();
XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = new XSSFClientAnchor(0,0,255,255,(short)0,0,(short)imageswidth,imagesHeight);
anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
drawingPatriarch.createPicture(anchor,workbook.addPicture(bytes,XSSFWorkbook.PICTURE_TYPE_JPEG));
CellStyle headCellStyle = StyleUtil.createHeadCellStyle(workbook);
CellStyle cellStyle = StyleUtil.createCellStyle(workbook);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
for (int i = 0; i < headList.size(); i++) {
Cell cell = writer.getCell(i , imagesHeight+1, true);
cell.setCellStyle(headCellStyle);
cell.setCellValue(headList.get(i));
}
for(int i = 0; i<startRunExportVOS.size(); i++) {
StartRunExportVO param1 = startRunExportVOS.get(i);
Field[] fields = param1.getClass().getDeclaredFields();
for(int j = 0; j<fields.length; j++) {
fields[j].setAccessible(true);
DateTimeFormat annotation = fields[j].getAnnotation(DateTimeFormat.class);
if(annotation != null) {
Cell cell = writer.getCell(j , i+imagesHeight+1+1, true);
cell.setCellStyle(cellStyle);
cell.setCellValue(DateUtils.formatDate((Date) fields[j].get(param1),"yyyy-MM-dd"));
continue;
}
Cell cell = writer.getCell(j , i+imagesHeight+1+1, true);
cell.setCellStyle(cellStyle);
cell.setCellValue(fields[j].get(param1).toString());
}
}
for(int i=0; i<11; i++) {
writer.setColumnWidth(i,18);
}
int height = imagesHeight + startRunExportVOS.size()+1;
for(int i=0; i<height; i++) {
writer.setRowHeight(i,35);
}
}catch (Exception e) {
log.error("开行分析导出列表excecl异常:{}",e.getMessage(),e);
throw new BusinessException("开行分析导出列表excecl异常");
}finally {
ServletOutputStream out= null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
writer.flush(out, true);
writer.close();
IoUtil.close(out);
}
}