java用easyexcel按模版导出

首先在项目的resources下面建一个template包,之后在下面创建一个模版,模版格式如下:

名称为 financeReportBillStandardTemplateExcel.xlsx:

{.fee}类型的属性值,是下面实体类的属性,要注意这里面的格式,不能错,还需要注意就是驼峰,例如:{.stockMv},要跟实体类的属性名保持一致,否则在导入的时候就会出现null之类的问题。

要是需要给属性设置格式之类的,例如:我们这里设置千分符并且保留两位小数,可进行如下图所示的操作,先选中所要设置的单元格,然后右键点击,选中设置单元格格式,就会弹出下图的操作,可以进行配置格式

接下来是实体类

package com.citicsc.galaxy.finance.lq;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @ClassName StandardBillFieldsDTO
 * @Description TODO
 * @Author houbing
 * @Date 2023/9/18 10:07
 */

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StandardBillFieldsDTO {

    private String id;

    //利息收入(利息归本)
    private BigDecimal interest;

    //股息红利
    private BigDecimal dividend;

    //资金余额
    private BigDecimal availableCash;

    //资产市值
    private BigDecimal assetMv;

    //非上市股票市值
    private BigDecimal unlistedMv;

    //当日国债逆回购发生额
    private BigDecimal debtReverseRepurchase;

    //出入金净额
    private BigDecimal netCash;

    //其他资金变动
    private BigDecimal otherCash;

    //总资金变动
    private BigDecimal totalCashInout;

    //交易费用
    private BigDecimal fee;

    //利息收入
    private BigDecimal interestIncome;

    //权利金收支
    private BigDecimal netPremium;

    //执行实收资金
    private BigDecimal realReceiveCash;

    //执行实付资金
    private BigDecimal realPaymentCash;

    //现金替代实收资金
    private BigDecimal realOffsetCashIn;

    //现金替代实付资金
    private BigDecimal realOffsetCashOut;

    //执行冻结资金
    private BigDecimal frozenCash;

    //市值权益
    private BigDecimal totalMv;

    //股票市值
    private BigDecimal stockMv;

    //买券金额
    private BigDecimal buySecuritiesAmount;

    //做市商交易经手费优惠
    private BigDecimal marketDiscount;

    //累计平仓盈亏
    private BigDecimal closePnl;

    //累计浮动盈亏
    private BigDecimal floatPnl;

    //其他交易费用
    private BigDecimal otherFee;

}

这里只展示部分字段。

 接下来就直接在controller层中进行导出

@ApiOperation(value = "交易账户基础数据查询表")
    @PostMapping("/exportBill")
    public void exportBill(@RequestBody @Validated TraAccBillReq req ,HttpServletResponse response) throws Exception {
        response.setContentType("application/octet-stream");
        String fileName = URLEncoder.encode("交易账户基础数据查询表" + DateUtils.formatStr(req.getTradingDay()), "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库的数据
        List<StandardBillFieldsDTO> dtos = lqAppService.queryBillFieldList(req);
        if (CollectionUtils.isNotEmpty(dtos)) {
            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
            WriteSheet sheet = EasyExcel.writerSheet(0).build();
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                    .withTemplate(new ClassPathResource("template/financeReportBillStandardTemplateExcel.xlsx").getInputStream())
                    .build();
            excelWriter.fill(dtos, fillConfig, sheet);
            excelWriter.finish();
        } else {
            throw new BizException("未查询到账单信息");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文件时,如何设置单元格样式? A:使用easyexcel模板导出Excel文件时,可以通过设置注解@ExcelProperty的style属性来指定对应单元格的样式。具体操作如下: 1. 定义样式类,继承自com.alibaba.excel.write.style.AbstractCellStyleStrategy,重写setCellStyle方法,对单元格样式进行设置。示例代码如下: ``` public class CustomCellStyleStrategy extends AbstractCellStyleStrategy { private CellStyle cellStyle; public CustomCellStyleStrategy(Workbook workbook) { this.cellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontName("微软雅黑"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); } @Override protected CellStyle setCellStyle(Cell cell, Head head, Integer integer, Integer integer1) { return cellStyle; } } ``` 2. 在需要导出的实体类中,为需要定制样式的属性添加@ExcelProperty注解,设置style属性为对应的样式类。示例代码如下: ``` public class User { @ExcelProperty(value = "姓名", index = 0, style = CustomCellStyleStrategy.class) private String name; @ExcelProperty(value = "年龄", index = 1) private Integer age; // ... } ``` 在注解中,value属性指定了单元格的标题,index属性指定了导出的顺序,style属性指定了样式类。 通过以上步骤,即可设置单元格样式。需要注意的是,样式类需要在导出Excel时传入,如下示例代码: ``` List<User> userList = new ArrayList<>(); // ... 添加用户数据 // 构造导出参数对象 WriteWorkbook writeWorkbook = EasyExcel.write(fileName).build(); WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").head(User.class).registerWriteHandler(new CustomCellStyleStrategy(writeWorkbook.getWorkbook())).build(); // 执行导出 EasyExcel.write(fileName, User.class).sheet("Sheet1").build().write(userList); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值