質問

I have a code in Java that opens a excel template by aspose library (it runs perfectly):

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");
    }
}

After I run this on Ruby with RJB (Ruby Java Bridge):

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)

I get this error:

test.rb:57:in `new': Can't find file: java.io.FileInputStream@693a317a. (FileNotFoundException)
    from aspose-test.rb:57:in `<main>'

Why? I run the same code... but in Ruby is not working! How do I fix this?

Update:

In documentation there is the the initializer: Workbook(java.io.InputStreamstream)... but it's not working in RJB. (How is this possible?)

役に立ちましたか?

解決

Your program should have worked, but I could not find any reason why it didn't and I am looking into it.

Now the alternate approaches.

Approach 1 Use Workbook(String) constructor instead of Workbook(FileInputStream). This worked flawlessly at my end. The sample code is

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)

Approach 2 Write a new Java class library. Write all your Aspose.Cells related code in it. Expose very simple and basic methods that needs to be called from Ruby (RJB). Why?

  • It is easy to write program in native Java language. If you use RJB, you need to perform a lot of code conversions
  • It is easy to debug and test in Java.
  • Usage of RJB will only be limited to calling methods from your own Java library. The RJB code will be small and basic.

Similar Example using own library Create a new Java project, lets say "cellstest". Add a new public class in it.

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;
    }
}

Like this, add as many methods as you like, for each operation. Build the project and copy the "cellstest.jar" in same folder where you copied Aspose.Cells jar files. You can return a String from your methods and check the return value in Ruby program for success or error code. The Ruby program will now be like

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 result

PS. I work for Aspose as Developer Evangelist.

他のヒント

In your Java code, you pass a file name string into FileInputStream() constructor:

FileInputStream fstream = new FileInputStream("/home/vmlellis/Testes/aspose-cells/template.xlsx");

In your Ruby code, you pass a file object:

file = file_input.new(file_path)
fin = file_input_stream.new(file)

Have you tried to do the same thing as in Java?

fin = file_input_stream.new(file_path)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top