问题描述
可以使用哪些工具将OpenOffice或Excel电子表格(及其所有公式)转换为可在运行时调用的Java对象?
What tools are available to convert an OpenOffice or Excel spreadsheet (with all its formulas) into a Java object that can be called at run-time?
显然,只能做一个计算引擎,只是关于数字和文本,而不是时间或API调用。
Obviously this would only make a calculation engine and just be about numbers and text, not timing or API calls.
即使使用命名单元格范围(有效地)命名变量输出代码可能难以理解。它需要重构以获得更像普通的Java代码。但是,我认为这对于原型化一些数据处理类型的作业将是有用的。或者嵌入由高级Excel用户维护的一些计算引擎。
Even with named cell ranges being used to (effectively) name variables the output code would presumably be difficult to understand. It would need refactoring to get more like normal Java code. However I think it would be useful for prototyping some data processing type jobs. Or for embedding some calculation engines maintained by an advanced Excel user.
修改:一个简单的例子:
外观
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,如下所示:
would translate into Java as something like:
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。
I'd assume fixed type for cells - either a java.math.BigDecimal or a String.
推荐答案
-
Apache POI项目提供Java可以阅读(主要是)Excel电子表格的代码。
The Apache POI project provides Java code that can read (mostly) Excel spreadsheets.
另一个项目Jacob为任意Windows程序的COM自动化提供了一个Java界面,当然包括Excel。您实际上是从Java外部处理Excel的API。
Another project, Jacob, provides a Java interface for COM automation of arbitrary Windows programs, of course including Excel. You're essentially working Excel's API from the outside, in Java.
这篇关于将电子表格公式转换为java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!