要求: 根据驾驶员信息,导出相应的排班信息 如下图所示: 分析: jeecg支持的普通导出满足不了我的表头需求,表头后面的站点和发车时间是两个维度,而且数量不确定,所以,使用jeecg的通用下载就不行了。只能自己手动生成excel了。
废话不多说,代码如下:
Controller层:
@Slf4j
@Api(tags="驾驶员")
@RestController
@RequestMapping("/driver/management")
public class BusDriverManagementController extends BaseController<BusDriverManagement, IBusDriverManagementService> {
@Autowired
private IBusDriverManagementService busDriverManagementService;
@RequestMapping(value = "/export-driver-shift-xlsx")
public void exportDriverXls( BusDriverManagement busDriverManagement,HttpServletRequest request, HttpServletResponse response) {
List<BusDriverLineExportVO> driverList = busDriverManagementService.getDriverShiftList(busDriverManagement);
Long siteNum = busDriverManagementService.getSiteNum(busDriverManagement);
try {
busDriverManagementService.exportDriverXls(request,response,driverList,siteNum);
} catch (IOException e) {
throw new HyException("导出失败");
}
}
}
Service层:
@Service
public class
BusDriverManagementServiceImpl extends ServiceImpl<BusDriverManagementMapper, BusDriverManagement> implements IBusDriverManagementService {
@Override
public void exportDriverXls(HttpServletRequest request, HttpServletResponse response, List<BusDriverLineExportVO> driverList, Long siteNum) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
String sheetName = "驾驶员班次";
XSSFSheet sheet = wb.createSheet(sheetName);
sheet.setDefaultRowHeightInPoints(CommonConstant.EXCEL_DEFAULT_ROW_HEIGHT);
sheet.setDefaultColumnWidth(CommonConstant.EXCEL_DEFAULT_COLUMN_WIDTH);
XSSFCellStyle cellStyle = getXssfCellStyle(wb);
createExcelHeader(siteNum, sheet, cellStyle);
createExcelContent(driverList, sheet, cellStyle);
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
wb.write(out);
out.flush();
out.close();
wb.close();
}
private void createExcelContent(List<BusDriverLineExportVO> driverList, XSSFSheet sheet, XSSFCellStyle cellStyle) {
Set<String> driverSet = new HashSet<>();
Set<String> lineSet = new HashSet<>();
XSSFCell cell;
XSSFRow row;
for (int i = 0; i < driverList.size(); i++) {
BusDriverLineExportVO driver = driverList.get(i);
row = sheet.createRow( 2+i*2);
if (!driverSet.contains(driver.getWorkNo())) {
driverSet.add(driver.getWorkNo());
Long count = driverList.stream().filter(dr -> dr.getWorkNo().equals(driver.getWorkNo())).count();
sheet.addMergedRegion(new CellRangeAddress(2 + i*2, (i + count.intValue()) * 2+1, 0, 0));
cell = row.createCell(0);
cell.setCellValue(driver.getDriverName());
cell.setCellStyle(cellStyle);
}
if (!lineSet.contains(driver.getLineId())) {
lineSet.add(driver.getLineId());
Long count = driverList.stream().filter(dr -> dr.getLineId().equals(driver.getLineId())).count();
sheet.addMergedRegion(new CellRangeAddress(2 + i*2, (i + count.intValue()) * 2+1, 1, 1));
cell = row.createCell(1);
cell.setCellValue(driver.getLineName());
cell.setCellStyle(cellStyle);
}
sheet.addMergedRegion(new CellRangeAddress(2 + i*2, i * 2+3, 2, 2));
cell = row.createCell(2);
cell.setCellValue(driver.getShiftName());
cell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(2 + i*2, i * 2+3, 3, 3));
cell = row.createCell(3);
cell.setCellValue(driver.getCarNo());
cell.setCellStyle(cellStyle);
for (int j = 0; j < driver.getSiteList().size(); j++) {
cell = row.createCell(4+j);
cell.setCellValue(driver.getSiteList().get(j));
cell.setCellStyle(cellStyle);
}
row = sheet.createRow( i*2+3);
for (int k = 0; k < driver.getDepartureTimeList().size(); k++) {
cell = row.createCell(4+k);
cell.setCellValue(driver.getDepartureTimeList().get(k));
cell.setCellStyle(cellStyle);
}
}
}
private void createExcelHeader(Long siteNum, XSSFSheet sheet, XSSFCellStyle cellStyle) {
XSSFCell cell;
XSSFRow row;
row = sheet.createRow(0);
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
cell = row.createCell(0);
cell.setCellValue("驾驶员");
cell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
cell = row.createCell(1);
cell.setCellValue("线路名称");
cell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 1, 2, 2));
cell = row.createCell(2);
cell.setCellValue("班次");
cell.setCellStyle(cellStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 1, 3, 3));
cell = row.createCell(3);
cell.setCellValue("车号");
cell.setCellStyle(cellStyle);
if(siteNum !=null) {
for (int i = 1; i <= siteNum.intValue(); i++) {
cell = row.createCell(3 + i);
cell.setCellValue("站点" + i);
cell.setCellStyle(cellStyle);
}
}
row = sheet.createRow(1);
if(siteNum !=null) {
for (int i = 1; i <= siteNum.intValue(); i++) {
cell = row.createCell(3 + i);
cell.setCellValue("发车时间");
cell.setCellStyle(cellStyle);
}
}
}
private XSSFCellStyle getXssfCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font cellFont = wb.createFont();
cellFont.setItalic(false);
cellFont.setColor(Font.COLOR_NORMAL);
cellFont.setFontHeightInPoints((short) 10);
cellFont.setFontName("宋体");
cellStyle.setFont(cellFont);
cellStyle.setWrapText(true);
return cellStyle;
}
}
核心代码:service层中的exportDriverXls方法;
|