Java 导出excel

1.导出excel 带合计

如:在这里插入图片描述

public void export(DriverAndGuestMealQueryVO vo) {
        DriverAndGuestMealListDTO riceLiquidationPage = page(vo);
        // 创建一个Excel工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表sheet
        Sheet sheet = workbook.createSheet("司机补贴表");

        // 创建表头行
        Row headerRow = sheet.createRow(0);
        String[] headers = {
                "日期", "餐厅","餐段", "餐别", "消费金额", "客数", "调整金额", "结算金额"
        };
        int defaultColumnWidth = 20 * 256;
        for (int i = 0; i < headers.length; i++) {
            sheet.setColumnWidth(i, defaultColumnWidth);
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }
        try {
            List<DriverAndGuestMealQueryDTO> data = riceLiquidationPage.getPageInfo().getList();
            // 填充数据
            int rowNum = 1;
            if (data.size() > 0) {
                for (DriverAndGuestMealQueryDTO dto : data) {
                    Row dataRow = sheet.createRow(rowNum++);
                    int colNum = 0;
                    dataRow.createCell(colNum++).setCellValue(dto.getDriverDate());
                    dataRow.createCell(colNum++).setCellValue(dto.getRestaurant());
                    dataRow.createCell(colNum++).setCellValue(dto.getMealPeriod());
                    dataRow.createCell(colNum++).setCellValue(dto.getMealType());
                    dataRow.createCell(colNum++).setCellValue(String.valueOf(dto.getAmountSpent()));
                    dataRow.createCell(colNum++).setCellValue(dto.getNumberOfGuests());
                    dataRow.createCell(colNum++).setCellValue(String.valueOf(dto.getAdjustmentAmount()));
                    dataRow.createCell(colNum++).setCellValue(String.valueOf(dto.getSettlementAmount()));
                    // 注意:原始数据中可能没有“日期”字段,这里假设你有一个方法来获取它,或者你可以省略这一列
                    // dataRow.createCell(colNum++).setCellValue(dto.getDate()); // 假设有一个getDate()方法
                }
            }
            Row summaryRow = sheet.createRow(rowNum);
            Cell totalCells[] = new Cell[headers.length];
            totalCells[0] = summaryRow.createCell(0);
            totalCells[0].setCellValue("合计 ");

            totalCells[headers.length - 4] = summaryRow.createCell(headers.length - 4);
            totalCells[headers.length - 4].setCellValue(String.valueOf(riceLiquidationPage.getAmountSpentSum()));
            totalCells[headers.length - 3] = summaryRow.createCell(headers.length - 3);
            totalCells[headers.length - 3].setCellValue(String.valueOf(riceLiquidationPage.getNumberOfGuestsSum()));
            totalCells[headers.length - 2] = summaryRow.createCell(headers.length - 2);
            totalCells[headers.length - 2].setCellValue(String.valueOf(riceLiquidationPage.getAdjustmentAmountSum()));
        totalCells[headers.length-1] = summaryRow.createCell(headers.length-1);
        totalCells[headers.length-1].setCellValue(String.valueOf(riceLiquidationPage.getSettlementAmountSum()));
        // 设置响应内容类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            // 设置响应头,告诉浏览器下载的文件名
            response.setHeader("Content-Disposition", "attachment; filename=司机补贴表.xlsx");

        } catch (Exception e) {
            e.getMessage();
        }

        // 将工作簿写入响应输出流
        try {
            workbook.write(response.getOutputStream());
            // 关闭工作簿释放资源
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭工作簿释放资源
            try {
                workbook.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值