0

I have a code in C# that I need to use or convert in Java. It is a code that use a Reference to the Word 12.0 Object Library.

Here the beginning the C# code :

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Word; How can I use C# in Java or how can I rewrite the code withe the reference from C# to Java?

Thank you

2
  • You can't write C# in Java or vise versa. They are completely different languages. Commented Jun 6, 2011 at 14:22
  • Linq? Might be quite hard, as Java doesn't have linq. Commented Jun 6, 2011 at 14:58

3 Answers 3

3

Apache POI - the Java API for Microsoft Documents:

The Apache POI Project's mission is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft's OLE 2 Compound Document format (OLE2). In short, you can read and write MS Excel files using Java. In addition, you can read and write MS Word and MS PowerPoint files using Java. Apache POI is your Java Excel solution (for Excel 97-2008).

Sign up to request clarification or add additional context in comments.

1 Comment

Apache POI has some problems with docx, xslx and pptx.
1

To convert the code you can use the converter mentioned by slandau. You can use a number of solutions for Word interop. Apache POI is one. OpenOffice is another. docx4j is another.

The good thing about those solutions is you will not need to install Microsoft Word to use them (unlike your existing interop code in C#). You could also use JACOB and COM if you wanted to use MS Word directly.

If you want to keep it simple and use your C# code, you might be better off writing a console application in C# and passing parameters to it using Java (or you can write a web service, although Word interop in a web service gets tricky).

2 Comments

I tried those three library that you motion. The problem with Apache POI is that it don't support pptx, and with OpenOffice I have some problems with the format. And with docx4j, it is the same thing. So I was thinking that using MSOffice, the format will be perfect.
Unfortunately, none of the Java libraries are perfect. You could always crack open the pptx file manually, if you have specific requirements. It is just a zip file with XML in it, so you could theoretically manually update it. Otherwise you probably have to go with COM interop or calling your C# code as a console app.
0

You could use a tool such as http://www.cs2j.com/,

However I would recommend you learn the differences between C# and Java, and re-write the library yourself - or look for a similar one that already exists, PLEASE.

1 Comment

I think the easiest think is to re-write, but I don't know how to do this the reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.