二 Java之POI導出Excel:多個sheet( 二 )

2.ExcelSheet

查看代碼 package com.***.excel;import lombok.Data;import java.util.List;/** * @description: 導出多個sheet表 * @author: *** * @date: 2022/9/15 */@Datapublic class ExcelSheet {    /*** sheet的名稱*/    private String fileName;    /*** sheet里的標題*/    private String[] handers;    /*** sheet里的數據集*/    private List<String[]> dataset;    public ExcelSheet(String fileName, String[] handers, List<String[]> dataset) {        this.fileName = fileName;        this.handers = handers;        this.dataset = dataset;    }}
三、相關業務代碼1.service層
/*** 導出開票及運單信息*/ExportInvoiceAndBillVo exportInvoiceAndBillInfo(InvoiceReviewListDto dto);2.impl實現類
實現類里的代碼,需要各位根據自己的業務場景進行改動 , 無非就是將需要導出的數據先查出來,帶入模板中,調用工具類的方法導出 。
查看代碼 package com.***.vo.invoicereview;import lombok.Data;import java.io.Serializable;import java.util.List;/** * @description: 導出開票和運單信息Vo * @author: *** * @date: 2022/9/19 */@Datapublic class ExportInvoiceAndBillVo implements Serializable {    /*** 開票信息*/    private List<String[]> invoiceList;    /*** 運單信息*/    private List<String[]> billList;}
查看代碼     @Override    public ExportInvoiceAndBillVo exportInvoiceAndBillInfo(InvoiceReviewListDto dto) {        ExportInvoiceAndBillVo invoiceAndBill = new ExportInvoiceAndBillVo();        // 查詢需要導出的開票信息        PageInfo<InvoiceReviewListVo> pageInfo = queryInvoiceReviewList(dto);        List<InvoiceReviewListVo> invoiceList = pageInfo.getList();        if (invoiceList.size() > 10000) {            throw new ServiceException("開票數據過多,請分批次導出");        }        // 查詢需要導出的車運運單信息        List<Long> invoiceIdList = invoiceList.stream().map(InvoiceReviewListVo::getInvoiceId).collect(Collectors.toList());        List<ExportBillVo> billList = getBillInfo(invoiceIdList);        if (billList.size() > 10000) {            throw new ServiceException("運單數據過多,請分批次導出");        }        // 將表1 表2的數據 放入定義的對象Vo中        invoiceAndBill.setInvoiceList(getInvoiceDataList(invoiceList));        invoiceAndBill.setBillList(getBillDataList(billList));        return invoiceAndBill;    }
3.controller層
controller層的代碼需要注意的是:
1.因為導出Excel一般都是通過瀏覽器進行下載的,所以入參中需要加入HttpServletResponse
2.調用封裝的工具類ExportSheetUtil中的exportManySheetExcel方法就可以了
3.表頭和表名需要各位根據自身的業務場景修改哈
查看代碼     /**     * 導出開票和運單信息     */    @Log    @PostMapping("/exportInvoiceAndBillInfo")    public void exportInvoiceAndBillInfo(@RequestBody InvoiceReviewListDto dto, HttpServletResponse response) {        ExportInvoiceAndBillVo invoiceAndBillVo = invoiceFacadeService.exportInvoiceAndBillInfo(dto);        //設置sheet的表頭與表名        String[] invoiceSheetHead = {"開票編號", "票號", "公司名稱", "收票方名稱", "結算類型", "納稅識別碼", "收票聯系人", "聯系人電話", "運單總金額(元)", "含稅總金額(元)", "開票狀態", "提交開票時間", "運營審核時間", "運營審核人", "財務審核時間", "財務審核人", "開票完成時間", "沖銷操作人", "沖銷時間"};        String[] billSheetHead = {"開票編號", "運單號", "發貨地", "收貨地", "司機", "司機電話", "貨物名稱", "貨物數量", "單位", "貨物重量(噸)", "運單狀態", "運單金額(元)", "含稅金額(元)"};        ExcelSheet invoiceExcel = new ExcelSheet("開票信息", invoiceSheetHead, invoiceAndBillVo.getInvoiceList());        ExcelSheet billExcel = new ExcelSheet("運單信息", billSheetHead, invoiceAndBillVo.getBillList());        List<ExcelSheet> mysheet = new ArrayList<>();        mysheet.add(invoiceExcel);        mysheet.add(billExcel);        ExportSheetUtil.exportManySheetExcel("開票及運單信息", mysheet, response);    }

推薦閱讀