几种JAVA表达式语言计算工具

打印 上一主题 下一主题

主题 1829|帖子 1829|积分 5487

测试表达式工具分类

这里测试了几种方式,MS excel,Spring SEPL,MVEL,Google aviator
  1. import com.googlecode.aviator.AviatorEvaluator;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  4. import org.junit.jupiter.api.Test;
  5. import org.mvel2.MVEL;
  6. import org.springframework.expression.ExpressionParser;
  7. import org.springframework.expression.spel.standard.SpelExpressionParser;
  8. import org.springframework.expression.spel.support.StandardEvaluationContext;
  9. import java.io.*;
  10. import java.math.BigDecimal;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. public class ExcelTest {
  14.     @Test
  15.     public void testExcelFormula() throws IOException {
  16.         // 加载 Excel 文件
  17.         InputStream fis = this.getClass().getResourceAsStream("/formula.xlsx");
  18.         Workbook workbook = new XSSFWorkbook(fis);
  19.         Sheet sheet = workbook.getSheetAt(0);
  20.         // 读取公式计算后的值
  21.         Cell formulaCell1 = sheet.getRow(1).getCell(1); // 假设公式在第2行第2列
  22.         System.out.println("公式计算前的值: " + formulaCell1.getNumericCellValue());
  23.         // 修改单元格值
  24.         Row row = sheet.getRow(1); // 假设修改第2行
  25.         Cell cell = row.getCell(0); // 假设修改第1列
  26.         cell.setCellValue(15); // 设置新值
  27.         // 重新计算公式
  28.         FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
  29.         for (Row r : sheet) {
  30.             for (Cell c : r) {
  31.                 if (c.getCellType() == CellType.FORMULA) {
  32.                     evaluator.evaluateFormulaCell(c); // 重新计算公式
  33.                 }
  34.             }
  35.         }
  36.         // 读取公式计算后的值
  37.         Cell formulaCell = sheet.getRow(1).getCell(1); // 假设公式在第2行第2列
  38.         System.out.println("公式计算后的值: " + formulaCell.getNumericCellValue());
  39.         // 保存修改后的文件(可选)
  40.         FileOutputStream fos = new FileOutputStream("example_updated.xlsx");
  41.         workbook.write(fos);
  42.         fos.close();
  43.         // 关闭资源
  44.         fis.close();
  45.         workbook.close();
  46.     }
  47.     @Test
  48.     public void testSELFormula() throws IOException {
  49.         ExpressionParser parser = new SpelExpressionParser();
  50.         StandardEvaluationContext context = new StandardEvaluationContext();
  51.         Map<String, Object> cartItem = new HashMap<>();
  52.         cartItem.put("price", new BigDecimal("20.56"));
  53.         cartItem.put("quantity", 2);
  54.         context.setVariable("cartItem", cartItem);
  55.         BigDecimal totalPrice = parser.parseExpression("#cartItem['price'] * #cartItem['quantity']")
  56.                 .getValue(context, BigDecimal.class);
  57.         // 这里只是为了演示EL,实际上可以直接使用orderItem.getTotalPrice()
  58.         System.out.println("Order item total price calculated by Spring EL: " + totalPrice);
  59.     }
  60.     /*
  61.      <groupId>org.mvel</groupId>
  62.      <artifactId>mvel2</artifactId>
  63.      <version>2.4.13.Final</version>
  64.      */
  65.     @Test
  66.     public void testMvELFormula() throws IOException {
  67.         Map<String, Object> context = new HashMap<>();
  68.         Map<String, Object> cartItem = new HashMap<>();
  69.         context.put("cartItem", cartItem);
  70.         String expressions = "cartItem['price'] = cartItem['price'] == null ? new java.math.BigDecimal('15') : cartItem['price']; " +
  71.                 "cartItem['quantity'] = cartItem['quantity'] == null ? 2 : cartItem['quantity']; " +
  72.                 "cartItem['price'] * cartItem['quantity']";
  73.         Object res = MVEL.eval(expressions, context);
  74.         System.out.println(res.getClass());
  75.         System.out.println("res: " + res); // 输出: John
  76.     }
  77.     /*
  78.     <groupId>com.googlecode.aviator</groupId>
  79.     <artifactId>aviator</artifactId>
  80.     <version>5.3.1</version>
  81.      */
  82.     @Test
  83.     public void testAviator() {
  84.         Map<String, Object> context = new HashMap<>();
  85.         Map<String, Object> cartItem = new HashMap<>();
  86. //        cartItem.put("price", new BigDecimal("20.56"));
  87. //        cartItem.put("quantity", 2);
  88.         context.put("cartItem", cartItem);
  89.         String expression = "cartItem.price = cartItem.price == nil ? 15 : cartItem.price;" +
  90.                 "cartItem.quantity = cartItem.quantity == nil ? 2 : cartItem.quantity;" +
  91.                 "cartItem.price * cartItem.quantity";
  92.         Object result = AviatorEvaluator.execute(expression, context);
  93.         System.out.println(result.getClass());
  94.         // Print the result
  95.         System.out.println("Final price: " + result); // Output: Final price: 56.5
  96.     }
  97. }
复制代码
测试结果


使用推荐

推荐使用MVEL,易于java开发明白,功能强大,性能良好

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

北冰洋以北

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表