测试表达式工具分类

这里测试了几种方式,MS excel,Spring SEPL,MVEL,Google aviator

import com.googlecode.aviator.AviatorEvaluator;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.mvel2.MVEL;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import java.io.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map; public class ExcelTest {
@Test
public void testExcelFormula() throws IOException {
// 加载 Excel 文件
InputStream fis = this.getClass().getResourceAsStream("/formula.xlsx");
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0); // 读取公式计算后的值
Cell formulaCell1 = sheet.getRow(1).getCell(1); // 假设公式在第2行第2列
System.out.println("公式计算前的值: " + formulaCell1.getNumericCellValue()); // 修改单元格值
Row row = sheet.getRow(1); // 假设修改第2行
Cell cell = row.getCell(0); // 假设修改第1列
cell.setCellValue(15); // 设置新值 // 重新计算公式
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
for (Row r : sheet) {
for (Cell c : r) {
if (c.getCellType() == CellType.FORMULA) {
evaluator.evaluateFormulaCell(c); // 重新计算公式
}
}
} // 读取公式计算后的值
Cell formulaCell = sheet.getRow(1).getCell(1); // 假设公式在第2行第2列
System.out.println("公式计算后的值: " + formulaCell.getNumericCellValue()); // 保存修改后的文件(可选)
FileOutputStream fos = new FileOutputStream("example_updated.xlsx");
workbook.write(fos);
fos.close(); // 关闭资源
fis.close();
workbook.close();
} @Test
public void testSELFormula() throws IOException { ExpressionParser parser = new SpelExpressionParser();
// 使用Spring EL计算订单项的总价(虽然这里可以直接调用方法,但为了展示EL,我们依然使用它)
StandardEvaluationContext context = new StandardEvaluationContext();
Map<String, Object> cartItem = new HashMap<>();
cartItem.put("price", new BigDecimal("20.56"));
cartItem.put("quantity", 2);
context.setVariable("cartItem", cartItem);
// BigDecimal totalPrice = parser.parseExpression("#cartItem['price'] = 0; #cartItem['quantity'] = 0; #cartItem['price'] * #cartItem['quantity']")
// .getValue(context, BigDecimal.class);
BigDecimal totalPrice = parser.parseExpression("#cartItem['price'] * #cartItem['quantity']")
.getValue(context, BigDecimal.class); // 这里只是为了演示EL,实际上可以直接使用orderItem.getTotalPrice()
System.out.println("Order item total price calculated by Spring EL: " + totalPrice);
} /*
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.2</version>
*/
@Test
public void testMvELFormula() throws IOException {
Map<String, Object> context = new HashMap<>();
Map<String, Object> cartItem = new HashMap<>();
// cartItem.put("price", new BigDecimal("20.56"));
// cartItem.put("quantity", 2);
context.put("cartItem", cartItem); // String expression = "cartItem.price=cartItem.price==null?15:cartItem.price; cartItem.quantity=cartItem.quantity==null?1:cartItem.quantity; cartItem.price*cartItem.quantity";
// Corrected expression using bracket notation
String expressions = "cartItem['price'] = cartItem['price'] == null ? new java.math.BigDecimal('15') : cartItem['price']; " +
"cartItem['quantity'] = cartItem['quantity'] == null ? 2 : cartItem['quantity']; " +
"cartItem['price'] * cartItem['quantity']";
Object res = MVEL.eval(expressions, context);
System.out.println(res.getClass());
System.out.println("res: " + res); // 输出: John } /*
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.3.1</version>
*/
@Test
public void testAviator() {
// Create a context with variables
// Map<String, Object> context = new HashMap<>();
// context.put("price", 20.5);
// context.put("quantity", 3); // Define multiple expressions
// String expressions = "total = price * quantity; discount = total > 50 ? 5 : 0; finalPrice = total - discount; finalPrice"; Map<String, Object> context = new HashMap<>();
Map<String, Object> cartItem = new HashMap<>();
// cartItem.put("price", new BigDecimal("20.56"));
// cartItem.put("quantity", 2);
context.put("cartItem", cartItem);
String expression = "cartItem.price = cartItem.price == nil ? 15 : cartItem.price;" +
"cartItem.quantity = cartItem.quantity == nil ? 2 : cartItem.quantity;" +
"cartItem.price * cartItem.quantity"; Object result = AviatorEvaluator.execute(expression, context); System.out.println(result.getClass()); // Print the result
System.out.println("Final price: " + result); // Output: Final price: 56.5
}
}

测试结果

使用推荐

推荐使用MVEL,易于java开发理解,功能强大,性能优秀

几种JAVA表达式语言计算工具的更多相关文章

  1. 四种java代码静态检查工具

    [转载]常用 Java 静态代码分析工具的分析与比较 转载自 开源中国社区 http://www.oschina.net/question/129540_23043       1月16日厦门 OSC ...

  2. Java 高精度浮点数计算工具

    说起编程中的高精度数值,我第一反应就是double类型了.的确,double阶码11位,尾数52位,几乎能应对任何苛刻的要求......然而,当我天真地尝试用double来算泰勒展开式的函数值,离散代 ...

  3. JAVA简单精确计算工具类

    1 public class ArithUtil { 2 3 // 默认除法运算精度 4 private static final int DEF_DIV_SCALE = 10; 5 6 privat ...

  4. java表达式语言mvel2/ognl/spring-expression

    <!-- https://mvnrepository.com/artifact/org.mvel/mvel2 --><dependency> <groupId>or ...

  5. Spring学习总结(四)——表达式语言 Spring Expression Language

    SpEL简介与功能特性 Spring表达式语言(简称SpEL)是一个支持查询并在运行时操纵一个对象图的功能强大的表达式语言.SpEL语言的语法类似于统一EL,但提供了更多的功能,最主要的是显式方法调用 ...

  6. Spring ——表达式语言 Spring Expression Language (转载)

    目录 SpEL简介与功能特性 一.为什么需要Spring表达式语言 二.SpEL表达式Hello World! 三.SpEL表达式 3.1.文字表达式 3.2.SPEL语言特性 3.2.1.属性 3. ...

  7. Java EE之表达式语言EL(上)

    1.了解表达式语言 表达式语言(EL)用于在不使用脚本.声明或者表达式的情况下,在JSP页面中渲染数据. EL曾是JSTL 1.0规范(与JSP 1.2)中的一部分,并且只可以用作JSTL标签的特性. ...

  8. Java Web----EL(表达式语言)详解

     Java Web中的EL(表达式语言)详解 表达式语言(Expression Language)简称EL,它是JSP2.0中引入的一个新内容.通过EL可以简化在JSP开发中对对象的引用,从而规范页面 ...

  9. Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现

    1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  10. 应用AXIS开始Web服务之旅(soap web services)——使用三种不同的语言访问创建的Web服务,分别是JAVA、VB、VC

    一. 介绍 本文并不是想介绍Web服务的原理.系统架构等,我们假设您已经了解了关于Web服务的一些基本的概念.原理等知识.本文主要是针对那些已经了解Web服务概念,但是还没有亲身体会Web服务所带来令 ...

随机推荐

  1. Eclipse中的快捷键:批量修改指定的变量名、方法名、类名等:alt + shift + r

    /* * Eclipse中的快捷键: * 1.补全代码的声明:alt + / * 2.快速修复: ctrl + 1 * 3.批量导包:ctrl + shift + o * 4.使用单行注释:ctrl ...

  2. stdio.h的缓冲机制解析

    1. 令人迷惑的printf() 在C语言中,由于stdio.h中的缓冲机制,printf的输出通常会受到缓冲区的影响. 这种影响可能非常微妙,并常常令人疑惑,比如我们来看下面这段代码 #includ ...

  3. 单机stome安装

    先安装zookeeper,参考本文件夹下安装文档 启动zookeeper 1.解压stome /opt/Server目录下 tar -zxf apache-storm-1.1.0.tar.gz 2.修 ...

  4. linux--安装zeppelin

    可以下载源码安装也可以用docker安装 http://zeppelin.apache.org/download.html Zeppelin 安装目录的bin文件夹下,使用以下命令启动进程: /opt ...

  5. List<Map<String, Object>> 排序

    一.代码 public class Test { public static void main(String[] args) { Map<String, Object> map = ne ...

  6. Luogu P11628 WC2025 猫粮 题解 [ 绿 ] [ 贪心 ] [ adhoc ] [ 鸽巢原理 ]

    猫粮:WC 诈骗题.我竟然能切 WC 的 T3 也是逆天了. 话说切了猫粮能变成猫娘吗 qwq. 思路 首先题目里有下面几点关键的性质: 所有猫粮质量总和等于所有猫要吃的质量总和. 优质的有 \(n\ ...

  7. hbase - [03] 客户端常用命令(hbase shell)

    1.列出所有namespace list_namespace 2.创建namespace create_namespace 'ns_name' 3.修改namespace属性 alter_namesp ...

  8. Maven - 项目的JDK编译level是1.5,修改不掉??

    背景   idea中的maven项目,父项目和子项目的Project Structure的language level都是1.5,怎么修改为8?尝试修改并应用后会失效,还是会自动恢复为1.5. 1.S ...

  9. PowerShell开发游戏 · 打蜜蜂

         可以看到,虽然非常抽象简单,但是基础游戏框架已经搭建,游戏机制完善,就缺美工了,哈哈~~~~ [首先] Powershell不是用来开发游戏的,但是没人规定不能开发.因为它可以调取windo ...

  10. PHP中&&与and、||与or的区别

    https://blog.csdn.net/asty9000/article/details/80652064 在PHP中,&&与and都表示逻辑与,||与or都表示逻辑或,并且它们都 ...