Page 1 of 1

Help with LibXL

Posted: Sat May 09, 2020 9:49 am
by Oliver13
Hi, could somebody help please using LibXL (https://www.libxl.com) with PB ?

I try the following code, but no file (even the sample file "demo.xslx" coming with LibXL) can be opened.
Resulting error message: "can't open file for reading".. But: the file is not write protected or in use

That's my code (tested with PB 5.72 and PB.562, current version 3.9.0.0 of LibXL):

TIA..

Code: Select all

ImportC "libxl.lib"
  xlCreateBookW.i()
  xlBookReleaseW.i(book.i)
  xlBookAddSheetW.i(book.i,sheet.s,flag.i)
  xlBookSaveW.i(book.i,save.s)
  xlBookLoadW(book.i,filename.p-ascii)
  xlBooksetActiveSheetW(book.i,index.i)
  xlBookActiveSheetW(book.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)
  xlBookSheetCountW(book.i)
  xlBookErrorMessageW.i(book.i)
EndImport

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

book = xlCreateBookW()
Debug PeekS(xlBookErrorMessageW(book),-1,#PB_Ascii)

sfn$="demo.xlsx"
If book
  bret=xlBookLoadW(book,sfn$)
If bret=0
  Debug PeekS(xlBookErrorMessageW(book),-1,#PB_Ascii)
; -> results in "can't open file for reading".
Else
 Debug "Sheets:"+Str(xlBookSheetCountW(book))
EndIf
  xlBookReleaseW(book)
EndIf



Re: Help with LibXL

Posted: Sat May 09, 2020 11:37 am
by infratec
If a function ends with W it indicates that it is unicode.

So:

Code: Select all

xlBookLoadW(book.i,filename.s)
Strange that Error... returns an ascii string.

P.S.:

I would do it this way:

Code: Select all

ImportC "libxl.lib"
  xlCreateBook.i() As "_xlCreateBookW"
  xlBookRelease.i(book.i) As "_xlBookReleaseW"
  xlBookAddSheet.i(book.i,sheet.s,flag.i) As "_xlBookAddSheetW"
  xlBookSave.i(book.i,save.s) As "_xlBookSaveW"
  xlBookLoad(book.i,filename.s) As "_xlBookLoadW"
  xlBooksetActiveSheet.i(book.i,index.i) As "_xlBooksetActiveSheetW"
  xlBookActiveSheet.i(book.i) As "-xlBookActiveSheetW"
  xlSheetWriteStr.i(sheet.i,x.i,y.i,string.s,flag.i) As "_xlSheetWriteStrW"
  xlSheetWriteNum.i(sheet.i,x.i,y.i,value.i,flag.i) As "_xlSheetWriteNumW"
  xlBookSheetCount.i(book.i) As"_xlBookSheetCountW"
  xlBookErrorMessage_.i(book.i) As "_xlBookErrorMessageW"
EndImport

Macro xlBookErrorMessage(book)
  PeekS(xlBookErrorMessage_(book), -1, #PB_Ascii)
EndMacro



Define.i book, sheet, bret


book = xlCreateBook()
Debug xlBookErrorMessage(book)
If book
  sfn$="example.xls"
  bret = xlBookLoad(book, sfn$)
  If bret
    Debug "Sheets:"+Str(xlBookSheetCount(book))
  Else
    Debug xlBookErrorMessage(book)
  EndIf
  xlBookRelease(book)
EndIf

Re: Help with LibXL

Posted: Sat May 09, 2020 8:45 pm
by Shardik
Oliver13 wrote:I try the following code, but no file (even the sample file "demo.xslx" coming with LibXL) can be opened.
Resulting error message: "can't open file for reading".. But: the file is not write protected or in use
You have 3 mistakes in your code:
1. You have to specify the complete path to your demo file demo.xlsx.
2. For .xlsx files you have to use the function xlCreateXMLBookW() instead of xlCreateBookW() in order to create a new book.
3. You have to define function xlBookLoadW() with filename.s instead of filename.p-ascii.

I have tested your modified example successfully on Windows 7 SP1 x64 with PB 5.71 x64 and on Windows 10 x64 V1909 with PB 5.72 x64. Before starting the program I copied the library files libxl.dll from bin64, libxl.lib from lib64 and demo file demo.xlsx from the website into the same folder as the source code.

Code: Select all

ImportC "libxl.lib"
  xlCreateBookW.i()
  xlCreateXMLBookW()
  xlBookReleaseW.i(book.i)
  xlBookAddSheetW.i(book.i,sheet.s,flag.i)
  xlBookSaveW.i(book.i,save.s)
  xlBookLoadW(book.i,filename.s)
  xlBooksetActiveSheetW(book.i,index.i)
  xlBookActiveSheetW(book.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)
  xlBookSheetCountW(book.i)
  xlBookErrorMessageW.i(book.i)
EndImport

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

book = xlCreateXMLBookW()
Debug PeekS(xlBookErrorMessageW(book),-1,#PB_Ascii)

sfn$=GetCurrentDirectory()+"demo.xlsx"
If book
  bret=xlBookLoadW(book,sfn$)
If bret=0
  Debug PeekS(xlBookErrorMessageW(book),-1,#PB_Ascii)
; -> results in "can't open file for reading".
Else
 Debug "Sheets:"+Str(xlBookSheetCountW(book))
EndIf
  xlBookReleaseW(book)
EndIf

Re: Help with LibXL

Posted: Sun May 10, 2020 9:29 am
by Oliver13
Thank you Infratec & Shardik, now it works. :idea: