我在Java中有一个代码,它通过赋值库打开一个excel模板(它运行得很完美):
import com.aspose.cells.*;
import java.io.*;
public class test
{
public static void main(String[] args) throws Exception
{
System.setProperty("java.awt.headless", "true");
FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");
Workbook workbook = new Workbook(fstream);
workbook.save("final.xlsx");
}
}在我用RJB (Ruby )在上运行这个程序之后:
require 'rjb'
#RJM Loading
JARS = Dir.glob('./jars/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
file_input = Rjb::import('java.io.File')
file_input_stream = Rjb::import('java.io.FileInputStream')
workbook = Rjb::import('com.aspose.cells.Workbook')
system.setProperty("java.awt.headless", "true")
file_path = "/home/vmlellis/Testes/aspose-cells/template.xlsx"
file = file_input.new(file_path)
fin = file_input_stream.new(file)
wb = workbook.new(fin)我知道这个错误:
test.rb:57:in `new': Can't find file: java.io.FileInputStream@693a317a. (FileNotFoundException)
from aspose-test.rb:57:in `<main>'为什么?我运行同样的代码..。但是在Ruby中却不起作用!我该怎么解决这个问题?
更新:
在文档中有一个初始化程序:工作簿(java.io.InputStreamstream).但在RJB不起作用。(这怎么可能?)
发布于 2013-06-07 10:32:45
你的程序应该是有效的,但我找不到任何理由它没有,我正在调查它。
现在交替的方法。
方法1使用工作簿(String)构造函数,而不是工作簿(FileInputStream)。这件事在我的结尾是完美无缺的。示例代码是
require 'rjb'
#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
workbook = Rjb::import('com.aspose.cells.Workbook')
system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"
wb = workbook.new(file_path)
wb.save(save_path)方法2编写了一个新的类库。在其中编写所有与Aspose.Cells相关的代码。公开需要从Ruby (RJB)调用的非常简单和基本的方法。为什么?
类似的例子使用自己的库创建了一个新的项目,可以说是“最快速的”。在其中添加一个新的公共类。
package cellstest;
import com.aspose.cells.Workbook;
public class AsposeCellsUtil
{
public String doSomeOpOnWorkbook(String inFile, String outFile)
{
String result = "";
try
{
// Load the workbook
Workbook wb = new Workbook(inFile);
// Do some operation with this workbook
// ..................
// Save the workbook
wb.save(outFile);
// everything ok.
result = "ok";
}
catch(Exception ex)
{
// Return the exception to calling program
result = ex.toString();
}
return result;
}
}像这样,为每个操作添加任意多个方法。构建项目并将"cellstest.jar“复制到复制Aspose.Cells jar文件的文件夹中。您可以从方法中返回一个字符串,并在Ruby程序中检查返回值,以获得成功或错误代码。Ruby程序现在将类似于
require 'rjb'
#RJM Loading
JARS = Dir.glob('/home/saqib/cellslib/*.jar').join(':')
print JARS
Rjb::load(JARS, ['-Xmx512M'])
system = Rjb::import('java.lang.System')
AsposeCellsUtil = Rjb::import('cellstest.AsposeCellsUtil')
system.setProperty("java.awt.headless", "true")
file_path = "/home/saqib/rjb/template.xlsx"
save_path = "/home/saqib/rjb/final.xlsx"
# initialize instance
asposeCellsUtil = AsposeCellsUtil.new()
# call methods
result = asposeCellsUtil.doSomeOpOnWorkbook(file_path, save_path)
puts resultPS。我是阿斯坡公司的开发人员,布道者。
发布于 2013-06-06 17:53:47
在Java代码中,将文件名字符串传递给FileInputStream()构造函数:
FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");在Ruby代码中,传递一个文件对象:
file = file_input.new(file_path)
fin = file_input_stream.new(file)您试过做与Java相同的事情吗?
fin = file_input_stream.new(file_path)https://stackoverflow.com/questions/16950372
复制相似问题