将电子表格公式转换为Java
有哪些工具可将OpenOffice或Excel电子表格(及其所有公式)转换为可在运行时调用的Java对象?
显然,这只会构成一个计算引擎,而只是数字和文本,而不是计时或API调用。
即使使用命名的单元格范围来(有效)命名变量,输出代码也可能难以理解。需要进行重构以使其更像普通的Java代码。但是,我认为这对原型某些数据处理类型的作业非常有用。或用于嵌入一些由高级Excel用户维护的计算引擎。
编辑 :一个简单的例子:
出现
A B C D
1 Mortgage Value 100,000.00
2 Interest rate 4.5%
3 Type Interest-only
4 Years 3
5 Regular payment 4,500.00
6 Total interest 13,500.00
小区名称
A B C D
1 Mortgage Value VALUE
2 Interest rate INTEREST
3 Type TYPE
4 Years YEARS
5 Regular payment REGPYMT
6 Total interest TOTALPYMT
配方
A B C D
1 Mortgage Value 100,000.00
2 Interest rate 4.5%
3 Type Interest-only
4 Years 3
5 Regular payment =VALUE*INTEREST
6 Total interest =YEARS*REGPYMT
会翻译成Java,就像:
package example.calcengine;
import java.math.*;
public class MyCalcEngine {
// unnamed cells
public String A1 = "Mortgage Value";
public String A2 = "Interest rate";
public String A3 = "Type";
public String A4 = "Years";
public String A5 = "Regular payment";
public String A6 = "Total interest";
// named cells
public BigDecimal VALUE = new BigDecimal(100000.00);
public BigDecimal INTEREST = new BigDecimal(0.045);
public String TYPE = "Interest-only";
public BigDecimal YEARS = new BigDecimal(3);
public BigDecimal REGPYMT = new BigDecimal(0);
public BigDecimal TOTALPYMT = new BigDecimal(0);
// formulas
public void calculate() {
REGPYMT = VALUE.multiply(INTEREST);
TOTALPYMT = REGPYMT.multiply(YEARS);
}
}
我假设单元格为固定类型-java.math.BigDecimal或String。