Page 1 of 1

LibXL: read cell

Posted: Sun Jun 25, 2023 2:52 pm
by MBV
Hi There,

It's about reading a text cell in an xlsx sheet with PureBasic and LibXL.

Has anyone some experience how to do this using the procedure below
(or otherwise) and can help me with a code snippet?

Code: Select all

Procedure.l xlSheetReadStr(sheet.i, row.i, col.i, *format)
  ProcedureReturn CallCFunction(libXLid, "xlSheetReadStrW", sheet, row, col, *format)
EndProcedure
Many Thanks
MBV

Re: LibXL: read cell

Posted: Sun Jun 25, 2023 6:13 pm
by spikey
There's an example at viewtopic.php?p=554132&hilit=libxl#p554132 to get you started.

You'll need to import one or more of the read functions depending on exactly what you plan to be reading from the cell.

Re: LibXL: read cell

Posted: Mon Jun 26, 2023 5:54 am
by MBV
Thanks for the answer.
But unfortunately that's not the point.

Of course I have studied all the posts in the forum on the subject of LibXL.
Many operations work well, including:
xlSheetWriteStrW
xlSheetWriteNumW and even
xlSheetReadNumW

But I can't get xlSheetReadStrW to work.
Maybe because of pointers?.

Hence my request for a code example.

Many thanks
MBV

Re: LibXL: read cell

Posted: Mon Jun 26, 2023 7:04 am
by infratec

Code: Select all

CompilerIf #PB_Compiler_32Bit
  ImportC "libxl32.lib"
    xlCreateBook.i() As "_xlCreateBookW"
    xlBookRelease.i(book.i) As "_xlBookReleaseW"
    xlBookAddSheet.i(book.i, sheet.s, flag.l) As "_xlBookAddSheetW"
    xlBookSave.i(book.i, save.s) As "_xlBookSaveW"
    xlBookLoad(book.i, filename.s) As "_xlBookLoadW"
    xlBooksetActiveSheet.i(book.i, index.l) As "_xlBooksetActiveSheetW"
    xlBookActiveSheet.i(book.i) As "_xlBookActiveSheetW"
    xlSheetWriteStr.i(sheet.i, x.l, y.l, string.s, flag.l) As "_xlSheetWriteStrW"
    xlSheetWriteNum.i(sheet.i, x.l, y.l, value.d, flag.l) As "_xlSheetWriteNumW"
    xlBookGetSheet.i(handle.i, index.l) As "_xlBookGetSheetW"
    xlBookSheetCount.i(book.i) As "_xlBookSheetCountW"
    xlSheetReadStr_.i(sheet.i, row.l, col.l, *format) As "_xlSheetReadStrW"
    xlSheetReadNum.d(handle.i, row.l, col.l, *format) As "_xlSheetReadNumW"
    xlBookErrorMessage_.i(book.i) As "_xlBookErrorMessageW"  
  EndImport
CompilerElse
  ImportC "libxl64.lib"
    xlCreateBook.i() As "xlCreateBookW"
    xlBookRelease.i(book.i) As "xlBookReleaseW"
    xlBookAddSheet.i(book.i, sheet.s, flag.l) As "xlBookAddSheetW"
    xlBookSave.i(book.i, save.s) As "xlBookSaveW"
    xlBookLoad(book.i, filename.s) As "xlBookLoadW"
    xlBooksetActiveSheet.i(book.i, index.l) As "xlBooksetActiveSheetW"
    xlBookActiveSheet.i(book.i) As "xlBookActiveSheetW"
    xlSheetWriteStr.i(sheet.i, x.l, y.l, string.s, flag.l) As "xlSheetWriteStrW"
    xlSheetWriteNum.i(sheet.i, x.l, y.l, value.d, flag.l) As "xlSheetWriteNumW"
    xlBookGetSheet.i(handle.i, index.l) As "xlBookGetSheetW"
    xlBookSheetCount.i(book.i) As "xlBookSheetCountW"
    xlSheetReadStr_.i(sheet.i, row.l, col.l, *format) As "xlSheetReadStrW"
    xlSheetReadNum.d(handle.i, row.l, col.l, *format) As "xlSheetReadNumW"
    xlBookErrorMessage_.i(book.i) As "xlBookErrorMessageW"  
  EndImport
CompilerEndIf
  


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

Macro xlSheetReadStr(sheet, row, col, format)
  PeekS(xlSheetReadStr_(sheet, row, col, format))
EndMacro

Define.i book, sheet, bret


book = xlCreateBook()
Debug xlBookErrorMessage(book)
If book
  sfn$="example.xls"
  bret = xlBookLoad(book, sfn$)
  If bret
    
    sheet = xlBookGetSheet(book, 0)
    
    Debug "Sheets: "+ Str(xlBookSheetCount(book))
    Debug xlSheetReadNum(sheet, 3, 1, #Null)
    Debug xlSheetReadStr(sheet, 2, 1, #Null)
    
  Else
    Debug xlBookErrorMessage(book)
  EndIf
  xlBookRelease(book)
EndIf

Re: LibXL: read cell

Posted: Mon Jun 26, 2023 11:39 am
by MBV
Very helpful. Thank you infratec!

By the way, I have the same question related to VB6.
Maybe someone knows a solution for this?
MBV