You can take some idea in the code below my signature.
// Begin code.
/*
LC_XlsxToCsv.prg
Save all sheets of an Excel 2007 or later file to csv files.
Using HB32, Windows10, Libre Office last version.
Compile with -lhbwin
*** Change the parameters before run this code. ***
*** This code works only when Calc is not hidden. ***
This code is a basic sample for tests.
web :
http://bernard.mouille.free.fr/mso-hb32/LC_XlsxToCsv.txt Last change : 2023-07-05
*/
// Parameters : filters options for csv file.
//
https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options//#define BM_cListSeparator "44" // ASCII Field separator : 44 = commas.
#define BM_cListSeparator "59" // ASCII Field separator : 59 = Semicolon.
#define BM_cFieldDelimiter "34" // ASCII Text field delimiter : 34 = double quotes.
//#define BM_cEncoding "76" // Encoding : 76 = Unicode (UTF-8).
#define BM_cEncoding "0" // Encoding : 0 = System.
#define BM_cFirstLine "1" // First line to be treated in csv file.
// You can add columns options ( look the before web link ).
#define BM_FiltersOptions BM_cListSeparator + "," + BM_cFieldDelimiter + "," + BM_cEncoding + "," + BM_cFirstLine
// Parameter : csv output file.
#define BM_FILE_CSV hb_dirbase() + "_Result_" // Base files CSV to create with his path.
procedure Main
local i // Numeric count.
local Excel_File // Excel file to convert.
local aFiles // Files array.
local args_Book // Book create or open parameter(s).
local args_Csv // Save as csv file parameters.
local oServiceManager // Libre Office Object first.
local oDesktop // Libre Office Object 2.
local oBook // Book object.
local oSheet // Sheet object.
setmode( 25, 80 )
setcolor( "GR+/B" )
@ 0, 0, maxrow(), maxcol() box space( 9 )
? "Save all sheets of an Excel 2007 or later file to csv files."
?
aFiles := hb_Directory( BM_FILE_CSV + "*.csv" )
for i := 1 to len( aFiles )
ferase( aFiles[ i, 1 ] ) // Delete the old files created with this program.
endfor
Excel_File := win_GetOpenFileName( , "Select an Excel file",,, { "Excel files ; *.xlsx" } )
if .not. file( Excel_File )
Alert( "Excel file not found, abort." )
return
endif
// Create the Libre Office objects.
oServiceManager := win_oleCreateObject( "com.sun.star.ServiceManager" )
oDesktop := oServiceManager:createInstance( "com.sun.star.frame.Desktop" )
// Create the create book property : display Calc and open xlsx file.
args_Book := { lo_PropertyCreate( oServiceManager, "Hidden" , .F. ) ;
, lo_PropertyCreate( oServiceManager, "ReadOnly" , .T. ) ;
, lo_PropertyCreate( oServiceManager, "FilterName", "Calc MS Excel 2007 XML" ) }
// Create the book.
oBook := oDesktop:loadComponentFromURL( bh_FileToUrl( Excel_File ) ;
, "_blank" ;
, 0, args_Book )
for i := 0 to oBook:Sheets:Count - 1
oSheet := oBook:GetSheets:getByIndex( i )
oBook:CurrentController:setActiveSheet( oSheet ) // *** Not works if book is hidden. ***
// Create the save as csv file properties.
args_Csv := { lo_PropertyCreate( oServiceManager, "FilterName" , "Text - txt - csv (StarCalc)" ) ;
, lo_PropertyCreate( oServiceManager, "FilterOptions", BM_FiltersOptions ) }
oBook:storeAsURL( bh_FileToUrl( BM_FILE_CSV + hb_ntos( i ) + ".csv" ), args_Csv ) // Write the csv file on the disk.
endfor
oBook:Close( .T. )
oServiceManager := nil
return
// Tools functions Libre Office.
// Create a property object.
function lo_PropertyCreate( oServiceManager, cName, xValue )
local oProperty
oProperty := oServiceManager:bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
oProperty:Name := cName
oProperty:Value := xValue
return oProperty
// Tools functions common.
// Convert a file and path Windows name to URL.
function bh_FileToUrl( cFile )
local wFile := cFile
if hb_at( "/", cFile ) > 0
return cFile
endif
if left( wFile, 1 ) == "\"
wFile := left( hb_dirbase(), 2 ) + wFile
endif
wFile := strtran( wFile, ":", "|" )
wFile := strtran( wFile, "\", "/" )
wFile := strtran( wFile, " ", "%20" )
wFile := "///" + wFile
return "file:" + wFile
// End code.