preface

The author is developing a report export service this week, but he encountered a strange problem card for nearly a day, and found many ignored problems of Jacob, which are uniformly recorded here.

Introduction of Jacob

Many people think that Jacob is a report generation library, but Jacob is not a report generation library. . JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls to the COM libraries. In adult words, through JAVA JNI technology to call COM components, you can call Word can call Excel can also call other software based on COM component technology. It can be understood as a bridge between JAVA and COM components. JAVA communicates with COM components through Dispatch,Variant and other interfaces in JNI mode.

The main problem

The basic principle of

Just remember this:

Jacob calls the COM component’s interface through Java’s JNI.

Since Java JNI calls COM components in a Native way, COM interface rules must be followed. Hence the bizarre usage:

Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch(); Dispatch inLineShapes = Dispatch.get(selection, "InLineShapes").toDispatch(); Dispatch picture = Dispatch.call(inLineShapes, "AddPicture", imgPath).toDispatch(); // Select picture Dispatch. Call (picture, "Select"); // Set the Width height to Dispatch. Put (picture, "Width", new Variant(10)); Dispatch.put(picture, "Height", new Variant(10));Copy the code

When I saw this API for the first time, IT was definitely beyond my understanding of what Dispatch is, why Dispatch is always used, and why call is needed to change the method. The basic concept is as follows:

ComThread: com component management, used to initialize the COM thread, release the thread, so it will be used before the operation of Office, after the operation is completed. ActiveXComponent: an application to create Office, such as whether you are using Word or Excel Dispatch: All operable objects in Office are basically of this type. Therefore, Jacob is a chained operation mode, just like the StringBuilder object, which returns a StringBuilder object Variant after calling append() : Encapsulate parameter data type, because some method parameters of office operation may be string type or number type, although both are 1, but cannot pass, you can use Variant to convert common parameter type. Static methods for new Variant(1), New Variant(” 1 “), Dispatch: These methods are used to manipulate Office. • Call () method: Calls a COM object’s method that returns a Variant type value. • Invoke () method: Does the same thing as the call method, but does not return a value. • Get () method: gets COM object properties and returns variant type values. • Put () method: Sets COM object properties. Some of the above methods have many overloaded methods, and different parameters need to be placed when calling different methods. As for what parameters stand for and what values to put, you need to refer to the VBA code, which cannot be changed by Jacob alone. The toDispatch() method on a Variant object: converts the Variant type returned by the above method toDispatch for the next chain operation.

The above quote is from the blog.

Compatibility issues

Because it is through the Java JNI call COM components to achieve the control of Word so only compatible with Windows system, friends of other systems can wash and sleep, simple functions can be achieved through POI. Jacob’s compatibility with Word versions is good after testing, ranging from Word 2010 to Office 365. The 32-bit version is incompatible with the 64-bit version. Select an appropriate version based on your machine and put it in the Java bin directory. Make sure the machine, Jacob’s JAR and DLL versions are the same.

Jacob encapsulation

The tools for Jacob on the Internet are all the same, and it feels like a sticky source. But that’s really all you need to generate a report. Here is a typical replacement API implementation idea: it is not easy to generate a report from scratch directly in the background, but based on a known template with appropriate replacements, it is much easier and can meet 80% of the requirements. The basic idea of replacing text is to define the placeholder of the content to be replaced in advance in the template, and then use the code to dynamically replace the placeholder. I used to use the following form ${ABC}$. In this way, the text can be replaced directly, and the results can have the original format, so that our program can not worry about the format problem, greatly simplifying the business complexity. Specific packaging can refer to a few examples:

Methods that extend the Jacob utility class

If you want to extend the packaged tool class and add your own functions, you can refer to the following materials: Microsoft official website API link, find this reference is really not easy, useful reference materials are really few pitiful, according to the clues of the Internet to find this reference address, this reference can be said to be unusually powerful, with this almost has all the ability of Word.

In the pit of

1. Failed to open the document, you can try manually opening the Word program. TypeParagraph or table creation always results in the disappearance of some content. This is actually related to the selected state of Word itself. If something is selected, the insertion instruction will cause the disappearance of the selected content. You can solve this problem by using InsertParagraphBefore or InsertParagraphAfter. 3. Want to get into the form of the question is which one form torture, a long day, I didn’t get the last can’t curve for national salvation, no longer struggle with which one table, table placeholder instead by a form of way to locate and inserted into the table, in a form object to create the content directly, and the form title finally unified handling. Do not forget to let me know if anyone has found a way.

conclusion

When solving problems related to Jacob this week, I just wanted to find a way to get the number of the form, but I only knew that Jacob was used, so I searched Jacob API all over the world, but found nothing. I passed by the official website for many times, but I did not see any word related to Word. I felt that I must have been on a fake official website. Nothing happened. As a result, I saw a page where someone said to insert a blank line, using a TypeParagraph method I hadn’t seen before, which opened a new door. If you’re faced with a problem that someone should have solved a long time ago, but you can’t find an answer online, or other people are talking about it, then check the direction of your thinking or the assumptions you made.