jxls导出报表单元格设置自适应行高

硅谷探秘者 算法基础 2649 0 0

如何使用jslx请参考之前写的文章:http://www.jiajiajia.club/blog/artical/76vv9kuy36j4/454

添加配置

import org.apache.poi.ss.usermodel.Row;
import org.jxls.area.Area;
import org.jxls.command.AbstractCommand;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
import org.jxls.common.Size;
import org.jxls.transform.poi.PoiTransformer;

/**
 * 自定义指令配置,在批注中添加
 * 例如:
 * jx:autoRowHeight(lastCell ="C3")
 * lastCell参数是指定该批注作用的范围
 */
public class AutoRowHeightCommand extends AbstractCommand {

    /**
     * 返回批注的名称
     * @return
     */
    @Override
    public String getName() {
        return "autoRowHeight";
    }

    /**
     * @param cellRef
     * @param context
     * @return
     */
    @Override
    public Size applyAt(CellRef cellRef, Context context) {
        Area area=getAreaList().get(0);
        Size size = area.applyAt(cellRef, context);

        PoiTransformer transformer = (PoiTransformer) area.getTransformer();
        Row row = transformer.getWorkbook().getSheet(cellRef.getSheetName()).getRow(cellRef.getRow());
        row.setHeight((short) -1);

        return size;
    }
}

在主代码中加如配置

//设置自适应行高
        XlsCommentAreaBuilder.addCommandMapping("autoRowHeight", AutoRowHeightCommand.class);

完整代码如下:

import club.jiajiajia.jxls.config.AutoRowHeightCommand;
import club.jiajiajia.jxls.entity.Entity;
import org.jxls.builder.xls.XlsCommentAreaBuilder;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.core.io.ClassPathResource;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 一般导出(导出一个列表数据)
 */
public class Test2 {
    public static void main(String[] args) throws Exception{
        InputStream templateIn= new ClassPathResource("template/template2.xlsx").getInputStream();

        OutputStream os =new FileOutputStream("D:\\data\\temp2.xlsx");

        Context context = new Context();

        List<Entity> data = new ArrayList<>();
        data.add(new Entity(1,"张三","张三的简介"));
        data.add(new Entity(2,"李四","里斯的简介"));
        data.add(new Entity(3,"王五","王五的简介"));
        data.add(new Entity(4,"赵六","赵六的简介"));

        context.putVar("data", data);

        JxlsHelper jxlsHelper = JxlsHelper.getInstance();
        jxlsHelper.getAreaBuilder().getTransformer();

        //设置自适应行高
        XlsCommentAreaBuilder.addCommandMapping("autoRowHeight", AutoRowHeightCommand.class);

        jxlsHelper.processTemplate(templateIn, os, context);

    }
}

模板中如何添加批注

jx:autoRowHeight(lastCell ="C3")

测试模板以及项目下载地址:https://github.com/18438301593/jxls2.git

在项目中resources下


评论区
请写下您的评论...
暂无评论...
猜你喜欢
算法基础 1934 代码的方式来excel,编码效率很低且不方便维护。jxls是通过预制excel模板,然后通过jxlsAPI将我们用程序的数据结合模板式输到相的excel文件中,从而形成。下面就以
html 1414 iframe度+iframe动态写入内容+iframe图片懒加载
mqtt协议 1610 时,才传文标识符。由MSB+LSB组成的int值示,文标识符增id,每次建立连接后从1开始。5.3.3有效载荷  有效载荷包含将被发布的用消息。数据的内容和式是用特定的。有效载荷的长度这
weblog 1561 layuitable选事件监听以及选中数据获取table.render({ elem:'#test' ,url:'[[@{/smmc/artificial}]]'+'?xsId
weblog 5607 java使用easypoiexcel时进值替换如性别:数据库中储存的是数字,1代男,2代女。的时候需要是汉字。在实体类中使用@Excel注解。 @Excel(name="性别
数据结构与算法 1502 达式:ab+ab-*级语言中采用然语言的中缀达式,但是计算机对中缀达式的处理是非常困难的,而对后缀或前缀达式则显得非常简后缀达式的特点: 1.在后缀达式中,变量(操作数)现的顺序与中
算法基础 3283 Pom依赖dependency groupIdcn.afterturn/groupId artifactIdeasypoi-base/artifactId version4.0.0/version /dependency dependency groupIdcn.afterturn/groupId artifactIdeasypoi-web/artifactId version4.0.0/ver
java基础 3484 统一。但现在回头来看,当年使用永久代来实现方法区的决定并不是一个好主意,这种致了Java用更容易遇到内存溢的问题(永久代有-XX:MaxPermSize的上限,即使不也有默认大小,而J9
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。