B4J Question [XLUtils] XLUtils can't run from class/code module?

MM2forever

Active Member
Licensed User
Longtime User
Hello community,

I just started experiementing with the amazing Excel library that is XLUtils to use with a project at work.
However, I hit a wall when I wanted to abstract and group some functionality inside my own class. The Excel claims the files that are saved are corrupted.

This is my simplest test:
If I run

B4X:
    Dim Workbook1 As XLWorkbookWriter = XL.CreateWriterBlank
    Dim sheet1 As XLSheetWriter = Workbook.CreateSheetWriterByName("Sheet1")
    sheet1.PutString(XL.AddressOne("A",1), "lol")
    Workbook1.SaveAs(File.DirApp, "mytest.xlsx",False)

From the main code module it works fine,
If I run it e.g. in a class initializer it produces the corrupt excel file.

So does that mean XLUtils can't run from class module and if so how can I solve this? Has it to do with some form of threading or is this a bug?
Many thanks in advance!

Christian
 

MicroDrie

Well-Known Member
Licensed User
File.DirApp returns the application folder which is protected by Windows as read-only. The recommended way is to use xui.DefaultFolder
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upload a small project showing the problem.

It will work fine in a Classmodule. Maybe it does not work in a Codemodule.

Seeing a project will be help to understand the problem.

If you got any Error you should have posted them.
 
Upvote 0

MM2forever

Active Member
Licensed User
Longtime User
File.DirApp returns the application folder which is protected by Windows as read-only. The recommended way is to use xui.DefaultFolder

Thanks but first of all its not true that the path returned via File.DirApp is in any way write-protected by Windows, it simply returns the path to the .jar/ exe in the bin folder in a packed version.
It may happen if you happen to put your jar or exe somwhere in "Progam Files" that needs elevation but I'm not doing that.

Other than that I mentioned that in both cases (running from "Main"/ running from a class) an .xlsx is even generated, but its a bit shorter when its generated from the class and Excel deems it corrupted.

Upload a small project showing the problem.

It will work fine in a Classmodule. Maybe it does not work in a Codemodule.

Seeing a project will be help to understand the problem.

If you got any Error you should have posted them.

There weren't any errors, otherwise I would have had a starting point to investigate.

I started making a sample project as you recommended and that brought me to the issue:

XLUtils must be .Initialize'd but didn't throw any error.
The example I modified did this but I never implemented this in my own class initializer... so there we go - Problem found and solved.

Also the example was set to Release mode what didn't throw the "... must be initialized first" error... Which is a pretty big thing I would have expected to be thrown in release as well actually.

Anyway, its Friday and a bit hot around here so....

Thanks and a great weekend.
 
Upvote 0
Top