B4J Question write in Microsoft Word...

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi
There is a library to write in a word file? as the library JPOI?
thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The jPOI library currently doesn't expose the Word related APIs. You can access them with JavaObject:

1. Download the POI binaries: http://archive.apache.org/dist/poi/release/bin/poi-bin-3.12-20150511.zip
2. Copy poi-scratchpad-3.12-20150511.jar to the additional libraries folder.
3. Add the jar file to the project with:
#AdditionalJar: poi-scratchpad-3.12-20150511


4. Now use JavaObject to access the API. See the attached project for a small example.

The most relevant APIs are documented here:
https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDocument.html
https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFParagraph.html
https://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFRun.html

Java example: http://svn.apache.org/repos/asf/poi...apache/poi/xwpf/usermodel/SimpleDocument.java

The output of this project:

SS-2015-10-15_10.41.21.png


Edit: the #AdditionalJar line is not required with the latest version of jPOI.
 

Attachments

  • PoiWord.zip
    1,003 bytes · Views: 632
Last edited:
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
excellent, thanks Erel
I try it right away.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
#AdditionalJar: poi-scratchpad-3.12-20150511

During test in B4A there is the error
B4X:
** Activity (form8) Create, isFirst = true **
** Activity (form8) Resume **
Error occurred on line: 1070 (form8)
java.lang.ClassNotFoundException: org.apache.poi$xwpf$usermodel$XWPFDocument
    at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:288)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:83)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:747)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:267)
    at peacemaker.cvmaster.form8._generdoc(form8.java:999)
    at peacemaker.cvmaster.form8._but1_click(form8.java:969)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4499)
    at android.view.View$PerformClick.run(View.java:18571)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5021)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
    at dalvik.system.NativeStart.main(Native Method)
at
B4X:
document.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Null)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
You need to add jPOI library to the project.
Just added reference to jPOI lib (all other .jars also added into B4A folder), and compilation cannot be finished even with 120 sec timeout :-(
Is it normal for this lib ?

If to add
B4X:
#AdditionalJar: poi-scratchpad-3.12-20150511
- compilation error
UNEXPECTED TOP-LEVEL EXCEPTION
 
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hello ,
I tried to use the above example to open/modify a word document and i got the following error.
Waiting for debugger to connect...
Program started.
Error occurred on line: 40
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:656)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:89)
at anywheresoftware.b4j.objects.NodeWrapper$1.handle(NodeWrapper.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3471)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3399)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3767)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$159/1705475060.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/410424423.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.poi.POIXMLException: Strict OOXML isn't currently supported, please see bug #57699
at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:100)
at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:58)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:122)
... 53 more
Attached my test project
 

Attachments

  • testprj.zip
    2.2 KB · Views: 409
Upvote 0
Top