使用EasyExcel导出数据

1. 添加依赖

      
        
            com.alibaba
            easyexcel
            3.0.5
        

2.实体类

package com.example.diisp.moudle.FinancialRevenueBookkeeping.entity.vo;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

@Data
public class FinancialRevenueBookkeepingVo {

    @ExcelProperty(value = "确收月份",index = 0)
    private String confirmedMonth;

    @ExcelProperty(value = "确收日期",index = 1)
    private String confirmedDate;

    @ExcelProperty(value = "合同编号",index = 2)
    private String contractCode;

    @ExcelProperty(value = "公司名称",index = 3)
    private String companyName;

    @ExcelProperty(value = "项目",index = 4)
    private String projectName;

    @ExcelProperty(value = "业务产品名称",index = 5)
    private String productName;

    @ExcelProperty(value = "数量",index = 6)
    private String productNum;

    @ExcelProperty(value = "开始使用时间",index = 7)
    private String usageStartAt;

    @ExcelProperty(value = "收入产品名称",index = 8)
    private String incomeProdcutName;

    @ExcelProperty(value = "商品名称",index = 9)
    private String goodsName;

    @ExcelProperty(value = "记账应收额",index = 10)
    private String bookkeepingReceivables;

    @ExcelProperty(value = "不含税额",index = 11)
    private String amountNoTax;

    @ExcelProperty(value = "税额",index = 12)
    private String taxAmount;

    @ExcelProperty(value = "需摊销的维保额(含税)",index = 13)
    private String maintAmountToAmortized;

    @ExcelProperty(value = "税率",index = 14)
    private String taxRate;

    @ExcelProperty(value = "需要摊销的维保额(不含税)",index = 15)
    private String maintAmountToAmortizedNoTax;

    @ExcelProperty(value = "税额",index = 16)
    private String taxAmount2;

    @ExcelProperty(value = "实际确收额(含税)",index = 17)
    private String actualAmountReceived;

    @ExcelProperty(value = "税率",index = 18)
    private String taxRate2;

    @ExcelProperty(value = "实际确收额(不含税)",index = 19)
    private String actualAmountReceivedNoTax;

    @ExcelProperty(value = "税额",index = 20)
    private String taxAmount3;

    @ExcelProperty(value = "确收类型",index = 21)
    private String confirmedReceiptType;

    @ExcelProperty(value = "发货情况",index = 22)
    private String sendInfo;

    @ExcelProperty(value = "调整说明",index = 23)
    private String adjustmentComment;

    @ExcelProperty(value = "软/硬件",index = 24)
    private String productAttribute;
}

3. Service接口

   /*导出收入记账所有数据*/
    void getAllBookKeepingList(HttpServletResponse response);

4. Service实现类

 /**
     * 导出所有收入记账数据
     *
     * @param response
     */
    @Override
    public void getAllBookKeepingList(HttpServletResponse response) {

        try {
            String fileName = "收入记账信息";
            URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            // 设置下载信息
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
            // 查询数据库
            List bookkeepingList = baseMapper.getAllBookkeepingList();
            // 创建vo集合
            List financialRevenueBookkeepingVos = new ArrayList<>();
            // 遍历
            for (FinancialRevenueBookkeeping bookkeeping : bookkeepingList) {
                // 创建vo类
                FinancialRevenueBookkeepingVo bookkeepingVo = new FinancialRevenueBookkeepingVo();
                // 拷贝对象
                BeanUtils.copyProperties(bookkeeping, bookkeepingVo, "usageStartAt", "confirmedReceiptType", "productAttribute");
                // 处理数据
                // 税额
                String taxAmount = bookkeeping.getTaxAmount();
                taxAmount = taxAmount + " ";
                bookkeepingVo.setTaxRate(taxAmount);
                // 税率
                String taxRate = bookkeeping.getTaxRate();
                String taxRate2 = bookkeeping.getTaxRate2();
                if (StringUtils.isNotEmpty(taxRate) || StringUtils.isNotEmpty(taxRate2)){
                    taxRate = taxRate + '%';
                    taxRate2 = taxRate2 + '%';
                    bookkeepingVo.setTaxRate(taxRate);
                    bookkeepingVo.setTaxRate2(taxRate2);
                }
                // 确收月份
                String confirmedMonth = bookkeeping.getConfirmedMonth();
                Date date = new SimpleDateFormat("yyyy-MM").parse(confirmedMonth);
                String now = new SimpleDateFormat("yyyy年MM月").format(date);
                bookkeepingVo.setConfirmedMonth(now);
                // 开始使用时间
                Long usageStartAt = bookkeeping.getUsageStartAt();
                ExcelTransDate.transDate(bookkeepingVo, usageStartAt, "usageStartAt");
                // 确收类型
                Integer confirmedReceiptType = bookkeeping.getConfirmedReceiptType();
                switch (confirmedReceiptType) {
                    case 1:
                        bookkeepingVo.setConfirmedReceiptType("即征即退收入额");
                        break;
                    case 2:
                        bookkeepingVo.setConfirmedReceiptType("一般收入额");
                        break;
                    case 3:
                        bookkeepingVo.setConfirmedReceiptType("其他业务收入");
                        break;
                    case 4:
                        bookkeepingVo.setConfirmedReceiptType("营业外收入");
                        break;
                }
                // 软/硬件
                Integer productAttribute = bookkeeping.getProductAttribute();
                switch (productAttribute) {
                    case 1:
                        bookkeepingVo.setProductAttribute("纯软");
                        break;
                    case 2:
                        bookkeepingVo.setProductAttribute("服务费");
                        break;
                    case 3:
                        bookkeepingVo.setProductAttribute("产品维保费");
                        break;
                }
                financialRevenueBookkeepingVos.add(bookkeepingVo);
            }
            // 列宽策略设置
            ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
            //调用方法进行写操作
            EasyExcel.write(response.getOutputStream(), FinancialRevenueBookkeepingVo.class)
                    .registerWriteHandler(CellStyleUtils.getHorizontalCellStyleStrategy())
                    .registerWriteHandler(widthStyleStrategy)
                    .sheet(fileName).doWrite(financialRevenueBookkeepingVos);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

5. 控制层

   /**
     * 导出所有收入记账的信息(不分页)
     *
     * @param response
     */
    @PostMapping("/export")
    public void getAllBookKeepingList(HttpServletResponse response) {
        bookkeepingService.getAllBookKeepingList(response);
    }

6.  前台代码导出

   expostData() {
      this.$axios({
        url: `${this.$api.revenueBookkeeping}/export`,
        method: 'post',
        responseType: 'blob'
      })
        .then(res => {})
        .catch(error => {
          const link = document.createElement('a')
          const blob = new Blob([error.data], { type: 'multipary/form-data' })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          link.setAttribute('download', decodeURI(Date.now() + '导出模板.xlsx'))
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          console.log(error)
        })
    }

你可能感兴趣的:(mybatis,spring,java)