测试表达式工具分类
这里测试了几种方式,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();
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'] * #cartItem['quantity']")
.getValue(context, BigDecimal.class);
// 这里只是为了演示EL,实际上可以直接使用orderItem.getTotalPrice()
System.out.println("Order item total price calculated by Spring EL: " + totalPrice);
}
/*
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.4.13.Final</version>
*/
@Test
public void testMvELFormula() throws IOException {
Map<String, Object> context = new HashMap<>();
Map<String, Object> cartItem = new HashMap<>();
context.put("cartItem", cartItem);
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() {
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开发理解,功能强大,性能优秀