xlsxio library

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

xlsxio library

Post by fan2006 »

XLSX I/O aims to provide a C library for reading and writing .xlsx files. The .xlsx file format is the native format used by Microsoft(R) Excel(TM) since version 2007.
source:https://github.com/brechtsanders/xlsxio
xlsxio_read.bi:

Code: Select all

#pragma once
#ifndef INCLUDED_XLSXIO_READ_H

#define INCLUDED_XLSXIO_READ_H
#include once "crt/stdlib.bi"
#include once "crt/stdint.bi"
#include once "crt/time.bi"
#inclib "xlsxio_read"
#inclib "expat"
#inclib "minizip"
#inclib "z"
extern "C"

type XLSXIOCHAR as zstring

Declare Sub xlsxioread_get_version(ByVal pmajor As Long Ptr, ByVal pminor As Long Ptr, ByVal pmicro As Long Ptr)
declare function xlsxioread_get_version_string() as const zstring ptr
Type xlsxioreader As Any Ptr
Declare Function xlsxioread_open(ByVal filename As Const ZString Ptr) As xlsxioreader
Declare Function xlsxioread_open_filehandle(ByVal filehandle As Long) As xlsxioreader
declare function xlsxioread_open_memory(byval data as any ptr, byval datalen as ulongint, byval freedata as long) as xlsxioreader
Declare Sub xlsxioread_close(ByVal HANDLE As xlsxioreader)
Type xlsxioread_list_sheets_callback_fn As Function(ByVal name_ As Const ZString Ptr, ByVal callbackdata As Any Ptr) As Long
Declare Sub xlsxioread_list_sheets(ByVal HANDLE As xlsxioreader, ByVal callback As xlsxioread_list_sheets_callback_fn, ByVal callbackdata As Any Ptr)

const XLSXIOREAD_SKIP_NONE = 0
const XLSXIOREAD_SKIP_EMPTY_ROWS = &h01
const XLSXIOREAD_SKIP_EMPTY_CELLS = &h02
const XLSXIOREAD_SKIP_ALL_EMPTY = XLSXIOREAD_SKIP_EMPTY_ROWS or XLSXIOREAD_SKIP_EMPTY_CELLS
const XLSXIOREAD_SKIP_EXTRA_CELLS = &h04
const XLSXIOREAD_SKIP_HIDDEN_ROWS = &h08
type xlsxioread_process_cell_callback_fn as function(byval row as uinteger, byval col as uinteger, byval value as const zstring ptr, byval callbackdata as any ptr) as long
type xlsxioread_process_row_callback_fn as function(byval row as uinteger, byval maxcol as uinteger, byval callbackdata as any ptr) as long
Declare Function xlsxioread_process(ByVal HANDLE As xlsxioreader, ByVal sheetname As Const ZString Ptr, ByVal FLAGS As ULong, ByVal cell_callback As xlsxioread_process_cell_callback_fn, ByVal row_callback As xlsxioread_process_row_callback_fn, ByVal callbackdata As Any Ptr) As Long
Type xlsxioreadersheetlist As Any Ptr

Declare Function xlsxioread_sheetlist_open(ByVal HANDLE As xlsxioreader) As xlsxioreadersheetlist
declare sub xlsxioread_sheetlist_close(byval sheetlisthandle as xlsxioreadersheetlist)
declare function xlsxioread_sheetlist_next(byval sheetlisthandle as xlsxioreadersheetlist) as const zstring ptr
Type xlsxioreadersheet As Any Ptr
Declare Function xlsxioread_sheet_last_row_index(ByVal sheethandle As xlsxioreadersheet) As UInteger
declare function xlsxioread_sheet_last_column_index(byval sheethandle as xlsxioreadersheet) as uinteger
declare function xlsxioread_sheet_flags(byval sheethandle as xlsxioreadersheet) as ulong
declare function xlsxioread_sheet_open(byval handle as xlsxioreader, byval sheetname as const zstring ptr, byval flags as ulong) as xlsxioreadersheet
Declare Sub xlsxioread_sheet_close(ByVal sheethandle As xlsxioreadersheet)
Declare Function xlsxioread_sheet_next_row(ByVal sheethandle As xlsxioreadersheet) As Long
declare function xlsxioread_sheet_next_cell(byval sheethandle as xlsxioreadersheet) as zstring ptr
declare function xlsxioread_sheet_next_cell_string(byval sheethandle as xlsxioreadersheet, byval pvalue as zstring ptr ptr) as long
declare function xlsxioread_sheet_next_cell_int(byval sheethandle as xlsxioreadersheet, byval pvalue as longint ptr) as long
declare function xlsxioread_sheet_next_cell_float(byval sheethandle as xlsxioreadersheet, byval pvalue as double ptr) as long
declare function xlsxioread_sheet_next_cell_datetime(byval sheethandle as xlsxioreadersheet, byval pvalue as time_t ptr) as long
Declare Sub xlsxioread_free(ByVal data_ As ZString Ptr)

end extern
#endif
xlsxio_write.bi:

Code: Select all

#pragma once
#ifndef INCLUDED_XLSXIO_WRITE_H

#define INCLUDED_XLSXIO_WRITE_H
#include once "crt/stdlib.bi"
#include once "crt/stdint.bi"
#include once "crt/time.bi"
#inclib "xlsxio_write"
#inclib "minizip"
#inclib "z"
extern "C"

Declare Sub xlsxiowrite_get_version(ByVal pmajor As Long Ptr, ByVal pminor As Long Ptr, ByVal pmicro As Long Ptr)
declare function xlsxiowrite_get_version_string() as const zstring ptr
Type xlsxiowriter As Any Ptr
declare function xlsxiowrite_open(byval filename as const zstring ptr, byval sheetname as const zstring ptr) as xlsxiowriter
Declare Function xlsxiowrite_close(ByVal HANDLE As xlsxiowriter) As Long
declare sub xlsxiowrite_set_detection_rows(byval handle as xlsxiowriter, byval rows as uinteger)
Declare Sub xlsxiowrite_set_row_height(ByVal HANDLE As xlsxiowriter, ByVal height As UInteger)
Declare Sub xlsxiowrite_add_column(ByVal HANDLE As xlsxiowriter, ByVal name_ As Const ZString Ptr, ByVal width_ As Long)
declare sub xlsxiowrite_add_cell_string(byval handle as xlsxiowriter, byval value as const zstring ptr)
Declare Sub xlsxiowrite_add_cell_int(ByVal HANDLE As xlsxiowriter, ByVal value As LongInt)
declare sub xlsxiowrite_add_cell_float(byval handle as xlsxiowriter, byval value as double)
declare sub xlsxiowrite_add_cell_datetime(byval handle as xlsxiowriter, byval value as time_t)
Declare Sub xlsxiowrite_next_row(ByVal HANDLE As xlsxiowriter)
End Extern
#endif
xlsxio_version.bi:

Code: Select all

#pragma once

#ifndef INCLUDED_XLSXIO_VERSION_H
#define INCLUDED_XLSXIO_VERSION_H


const XLSXIO_VERSION_MAJOR = 0
const XLSXIO_VERSION_MINOR = 2
const XLSXIO_VERSION_MICRO = 35

#define XLSXIO_VERSION_STRINGIZE_(major, minor, micro) #major "." #minor "." #micro
#define XLSXIO_VERSION_STRINGIZE(major, minor, micro) XLSXIO_VERSION_STRINGIZE_(major, minor, micro)
#define XLSXIO_VERSION_STRING XLSXIO_VERSION_STRINGIZE(XLSXIO_VERSION_MAJOR, XLSXIO_VERSION_MINOR, XLSXIO_VERSION_MICRO)

const XLSXIO_VERSION_ID = ((XLSXIO_VERSION_MAJOR * 10000) + (XLSXIO_VERSION_MINOR * 100)) + XLSXIO_VERSION_MICRO

#define XLSXIOREAD_NAME "libxlsxio_read"
#define XLSXIOWRITE_NAME "libxlsxio_write"
#define XLSXIOREAD_FULLNAME XLSXIOREAD_NAME " " XLSXIO_VERSION_STRING
#define XLSXIOWRITE_FULLNAME XLSXIOWRITE_NAME " " XLSXIO_VERSION_STRING

#endif
fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

Re: xlsxio library

Post by fan2006 »

test for read xlsx file.
xlsxio_readdemo.bas:

Code: Select all

#include once "xlsxio_read.bi"
#include once "crt/stdio.bi"
#ifdef __FB_WIN32__
'switch Windows console to UTF-8
 #include Once "windows.bi"
  SetConsoleOutputCP(65001)
#endif
#ifndef __FB_64BIT__ 
#libpath "win32"
#else
#libpath "win64"
#endif

'open .xlsx file for reading
Dim xlsxioread As xlsxioreader
Dim filename As ZString Ptr = StrPtr("1.xlsx")
xlsxioread = xlsxioread_open(filename)
If xlsxioread= NULL Then 
  fprintf(stderr, "Error opening .xlsx file\n")
  End
End If
' read values from first sheet
Dim value As ZString Ptr
Dim sheet As xlsxioreadersheet
Dim xrow As Long
Dim sheetname As  ZString Ptr = StrPtr("Sheet1")
Print "Contents of first sheet:"
sheet = xlsxioread_sheet_open(xlsxioread, sheetname,XLSXIOREAD_SKIP_EMPTY_ROWS) 
  'read all rows
  If sheet <> NULL Then
  xrow = xlsxioread_sheet_next_row(sheet)
  While xrow>0
   value = xlsxioread_sheet_next_cell(sheet)
    While  value <> NULL
      printf("%s\t", value)
      xlsxioread_free(value)
      value = xlsxioread_sheet_next_cell(sheet)
   Wend
   Print ""
    xrow = xlsxioread_sheet_next_row(sheet)
 Wend
 xlsxioread_sheet_close(sheet)
End If

' clean up
xlsxioread_close(xlsxioread)
Sleep

fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

Re: xlsxio library

Post by fan2006 »

test of creating/writing data to xlsx file.
xlsxio_writedemo.bas:

Code: Select all

#include "xlsxio_write.bi"
#include once "crt/stdio.bi"
#ifndef __FB_64BIT__ 
#libpath "win32"
#else
#libpath "win64"
#endif
'open .xlsx file for writing (will overwrite if it already exists)
Var filename="2.xlsx"
Dim handle As xlsxiowriter=xlsxiowrite_open(filename, "Sheet1")
If (handle = NULL) Then 
  fprintf(stderr, "Error creating .xlsx file\n")
  End
End If

' write column names
xlsxiowrite_add_column(handle, "Col1", 16)
xlsxiowrite_add_column(handle, "Col2", 0)
xlsxiowrite_next_row(handle)

'write data
Dim i As Long
For i = 0 To  999
  xlsxiowrite_add_cell_string(handle, "Test")
  xlsxiowrite_add_cell_int(handle, i)
  xlsxiowrite_next_row(handle)
Next

' close .xlsx file
xlsxiowrite_close(handle)
Print "create and write data to 2.xlsx"
Sleep

St_W
Posts: 1632
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: xlsxio library

Post by St_W »

I noted that you have created bindings for several C libraries. I have no idea how you created them, but in case you used fbfrog: did you consider contributing to https://github.com/freebasic/fbbindings/tree/raw or publish the used configuration?
fan2006
Posts: 32
Joined: Jun 07, 2020 3:05

Re: xlsxio library

Post by fan2006 »

St_W wrote: Jan 12, 2025 20:42 I noted that you have created bindings for several C libraries. I have no idea how you created them, but in case you used fbfrog: did you consider contributing to https://github.com/freebasic/fbbindings/tree/raw or publish the used configuration?
i don't know many fbfrog commands. only mongoose library need configuration i describe in details.the other libs just use "fbfrog xxx.h" to get the bi and then i modify it by hand to get things work.
Post Reply