poi 

后端代码:

/**
     * 导出
     */
    @PostMapping("/test")
    public void test(HttpServletResponse response) {
        List<CustomerExcel.CustomerListExcel> list = new ArrayList<>();
        CustomerExcel.CustomerListExcel c1 = new CustomerExcel.CustomerListExcel("1", "1", "1");
        CustomerExcel.CustomerListExcel c2 = new CustomerExcel.CustomerListExcel("2", "2", "2");
        CustomerExcel.CustomerListExcel c3 = new CustomerExcel.CustomerListExcel("3", "3", "3");
        list.add(c1);
        list.add(c2);
        list.add(c3);
        //
        String fileName = "fileName";
        String title = "title";
        String sheetName = "sheetName";
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), CustomerExcel.CustomerListExcel.class, list);
        Sheet sheet = workbook.getSheet(sheetName);
        // 下标0开始插入2行
        sheet.shiftRows(0, sheet.getLastRowNum(), 4, true, false);
        // 第1行, 创建2个单元格
        Row row0 = sheet.getRow(0);
        row0.createCell(0).setCellValue("总成交金额: 100.00");
        row0.createCell(1).setCellValue("总利润金额: 80.00");
        // 第2行, 跨列合并3个单元格(多个单元格有值,取第一个)
        Row row1 = sheet.getRow(1);
        row1.createCell(0).setCellValue("跨列: 100.00");
        row1.createCell(1).setCellValue("总利润金额: 80.00");
        row1.createCell(2).setCellValue("总利润金额: 80.00");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2));
        // 第3行,4行, 跨行合并
        Row row2 = sheet.getRow(2);
        row2.createCell(1).setCellValue("跨行: 100.00");
        Row row3 = sheet.getRow(3);
        row3.createCell(1).setCellValue("跨行: 101.00");
        sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 1));
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

实体类

package net.hp.common.model.DTO.response.profitsharing;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.hp.common.model.DTO.response.charging.ChargingPileReportDTO;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @Description 测试Excel
 * @Date 2022年03月08 11:01:18
 * @Author 郭明华
 */
@Data
public class CustomerExcel implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 交易总金额
     */
    @Excel(name = "交易金额", orderNum = "0", width = 15)
    private String totalMoney;

    /**
     * 交易金额
     */
    @Excel(name = "交易金额", orderNum = "2", width = 15)
    private String totalOrderMoney;

    /**
     * 列表数据
     */
    List<CustomerExcel.CustomerListExcel> list;

    public CustomerExcel() {
    }

    public CustomerExcel(String totalMoney, String totalOrderMoney) {
        this.totalMoney = totalMoney;
        this.totalOrderMoney = totalOrderMoney;
    }

    @Data
    public static class CustomerListExcel {

        public CustomerListExcel() {
        }

        public CustomerListExcel(String orderNo, String money, String orderMoney) {
            this.orderNo = orderNo;
            this.money = money;
            this.orderMoney = orderMoney;
        }

        /**
         * 服务商号
         */
        @Excel(name = "订单号", orderNum = "0", width = 25)
        private String orderNo;

        /**
         * 交易金额
         */
        @Excel(name = "交易金额", orderNum = "1", width = 15)
        private String money;

        /**
         * 实际分润金额
         */
        @Excel(name = "实际分润金额", orderNum = "2", width = 15)
        private String orderMoney;
    }
}

 

posted on 2022-04-22 15:16  1161588342  阅读(319)  评论(0编辑  收藏  举报