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;
    }
}

测试:

相关推荐
llwszx3 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野3 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person4 小时前
Java SE--方法的使用
java·开发语言·算法
小阳拱白菜5 小时前
java异常学习
java
FrankYoou6 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*6 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了6 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
天河归来7 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
weixin_478689767 小时前
十大排序算法汇总
java·算法·排序算法
码荼7 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud