libXL; PB wrapper how to do?

Just starting out? Need help? Post your questions and find answers here.
HanPBF
Enthusiast
Enthusiast
Posts: 564
Joined: Fri Feb 19, 2010 3:42 am

libXL; PB wrapper how to do?

Post by HanPBF »

Hello,

I have the libXL library which has a windows DLL.

How would I write a wrapper for PureBasic for this?

I have the documentation from the web site like:
int xlBookLoad(BookHandle handle, const wchar_t* filename)

So, "simply" prototype it as given as example in PureBasic help and map the types accordingly?

Is that correct so far? What needs also to be checked before?

Thanks a lot in advance,
regards!
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: libXL; PB wrapper how to do?

Post by Mijikai »

This worked for me :)

Code:

Code: Select all

;Tested with PureBasic 7.51b (x64) 

EnableExplicit

ImportC "libxl.lib"
  xlCreateBookW.i()
  xlBookReleaseW.i(book.i)
  xlBookAddSheetW.i(book.i,sheet.s,flag.i)
  xlSheetWriteStrW.i(sheet.i,x.i,y.i,string.s,flag.i)
  xlSheetWriteNumW.i(sheet.i,x.i,y.i,value.i,flag.i)
  xlBookSaveW.i(book.i,save.s)
  ;...
EndImport
    
Global book.i
Global sheet.i

book = xlCreateBookW()
If book
  sheet = xlBookAddSheetW(book,"sheet1",#Null)
  If sheet
    xlSheetWriteStrW(sheet,2,2,"Hello World!",#Null)
    xlSheetWriteNumW(sheet,3,1,1000,#Null)
    xlBookSaveW(book,"test.xls")
  EndIf 
  xlBookReleaseW(book)
EndIf 

End
HanPBF
Enthusiast
Enthusiast
Posts: 564
Joined: Fri Feb 19, 2010 3:42 am

Re: libXL; PB wrapper how to do?

Post by HanPBF »

As easy as expected...
Using the DLL directly should also work.

Thanks a lot for the example!!!
Oliver13
User
User
Posts: 82
Joined: Thu Sep 30, 2010 6:40 am

Re: libXL; PB wrapper how to do?

Post by Oliver13 »

Mijikai wrote:This worked for me :)]
Confirm this works here, too.
But just tried to load and modify a file; this does not work..
ImportC "libxl.lib"
xlCreateBookW.i()
xlBookReleaseW.i(book.i)
xlBookAddSheetW.i(book.i,sheet.s,flag.i)
xlSheetWriteStrW.i(sheet.i,x.i,y.i,string.s,flag.i)
xlSheetWriteNumW.i(sheet.i,x.i,y.i,value.i,flag.i)
xlBookSaveW.i(book.i,save.s)
xlBookLoadW(book.i,filename.s, tempfile.s)

EndImport

Global book.i
Global sheet.i
Define bret.i

book = xlCreateBookW()
If book
sheet = xlBookAddSheetW(book,"sheet1",#Null)
If sheet
xlSheetWriteStrW(sheet,2,2,"Hello World!",#Null)
xlSheetWriteNumW(sheet,3,1,1000,#Null)
xlBookSaveW(book,"test.xls")
EndIf
xlBookReleaseW(book)
EndIf

book = xlCreateBookW()
If book
bret=xlBookLoadW(book,"test.xls","")
Debug bret
sheet = xlBookAddSheetW(book,"sheet2","") ;- <-- Always stops with IMA error
If sheet
xlSheetWriteStrW(sheet,2,2,"Hello World!",#Null)
xlSheetWriteNumW(sheet,3,1,1000,#Null)
xlBookSaveW(book,"test2.xls")
EndIf
xlBookReleaseW(book)
EndIf

End
Do you have an idea ? TIA..
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: libXL; PB wrapper how to do?

Post by Mijikai »

Probably because of the empty string.

Try:

Code: Select all

sheet = xlBookAddSheetW(book,"sheet2",#Null)
Also you create a book instead of using the loaded book.

Use:

Code: Select all

sheet = xlBookAddSheetW(bret,"sheet2",#Null)
Post Reply