EasyExcel 导入计算公式导出展示字符串问题(SUM)

导出自定义表单 结果遇到 SUM函数 时 没有算出结果,直接展示的函数字符串

处理思路 拦截公式字符串 :

将对应的cell 设置 setCellFormula 字符串公式

然后cell类型设置为 CellType.FORMULA

重新配置 这样就解决了问题。

注意先设置CellFormula 然后设置 CellType (我把顺序搞反了,调试了很久)

3这里用的是evaluateFormulaCell 而不是evaluateInCell

原因evaluateInCell 导出的结果没函数计算式

evaluateFormulaCell 导出结果有函数式

结果如下:

这里顺便给出 一个获取列表行求和转化工具

复制代码
/**
 * @author lj
 * @title: CellCode
 * @projectName cloud
 * @date 2024/8/21 002110:46
 */
public class CellCodeUtil {
    /**
     * 行汇总求和   从1  开始
     * @param startIndex  开始的列表下标    从1  开始
     * @param endIndex  开始的结束列下标   从1  开始
     * @param row  行 从 1 开始
     * @return  =SUM(B5:D5)
     */
    public static String getRowSUN(int startIndex,int endIndex,int row){
        StringBuffer data = new StringBuffer("SUM(");
        data.append(excelColIndexToStr(startIndex) + row + ":");
        data.append(excelColIndexToStr(endIndex) + row + ")");
        return data.toString();
    }

    public static void main(String[] args) {
        System.out.println(getRowSUN(2,26,5));
        System.out.println(getColumnSUN(5,16,2));
    }
    /**
     * 列 汇总 求和  从1  开始
     * @param startIndex
     * @param endIndex
     * @param columnIndex  列表
     * @return
     */
    public static String getColumnSUN(int startIndex,int endIndex, int columnIndex){
        StringBuffer data = new StringBuffer("SUM(");
        data.append(excelColIndexToStr(columnIndex) + startIndex + ":");
        data.append(excelColIndexToStr(columnIndex) + endIndex + ")");
        return data.toString();
    }

    /**
     * 下标转列
     * @param columnIndex  从1 开始
     * @return
     */
    public static String excelColIndexToStr(int columnIndex) {
        if (columnIndex <= 0) {
            return null;
        }
        String columnStr = "";
        columnIndex--;
        do {
            if (columnStr.length() > 0) {
                columnIndex--;
            }
            columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
            columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
        } while (columnIndex > 0);
        return columnStr;
    }
}

测试:

相关推荐
熊大如如7 小时前
Java 反射
java·开发语言
猿来入此小猿8 小时前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
goTsHgo8 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder8 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
pjx9879 小时前
微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
java·spring cloud·微服务·eureka
多多*9 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle
爱喝酸奶的桃酥9 小时前
MYSQL数据库集群高可用和数据监控平台
java·数据库·mysql
唐僧洗头爱飘柔952710 小时前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
骑牛小道士10 小时前
Java基础 集合框架 Collection接口和抽象类AbstractCollection
java
alden_ygq10 小时前
当java进程内存使用超过jvm设置大小会发生什么?
java·开发语言·jvm