;Source: https://www.libxl.com/read-write-excel-date-time.html
#AutoIt3Wrapper_UseX64 = n
Local $hDLL = DllOpen('libxl32.dll')
Local $BookHandle = xlCreateBook()
xlBookSetKey($BookHandle) ;must License
Local $format1 = xlBookAddFormat($BookHandle)
xlFormatSetNumFormat($format1, 14) ;NUMFORMAT_DATE
Local $format2 = xlBookAddFormat($BookHandle)
xlFormatSetNumFormat($format2, 22) ;NUMFORMAT_CUSTOM_MDYYYY_HMM
Local $format3 = xlBookAddFormat($BookHandle)
xlFormatSetNumFormat($format3, xlBookAddCustomNumFormat($BookHandle, 'd mmmm yyyy'))
Local $format4 = xlBookAddFormat($BookHandle)
xlFormatSetNumFormat($format4, 18) ;NUMFORMAT_CUSTOM_HMM_AM
Local $sheet = xlBookAddSheet($BookHandle, 'Sheet1')
xlSheetSetCol($sheet, 1, 1, 15)
;// writing
xlSheetWriteNum($sheet, 2, 1, xlBookDatePack($BookHandle, 2010, 3, 11), $format1)
xlSheetWriteNum($sheet, 3, 1, xlBookDatePack($BookHandle, 2010, 3, 11, 10, 25, 55), $format2)
xlSheetWriteNum($sheet, 4, 1, xlBookDatePack($BookHandle, 2010, 3, 11), $format3)
xlSheetWriteNum($sheet, 5, 1, xlBookDatePack($BookHandle, 2010, 3, 11, 10, 25, 55), $format4)
;// reading
Local $Time = DllStructCreate('int Year;int Month; int Day; int Hour; int Min; int Sec')
xlBookDateUnpack($BookHandle, xlSheetReadNum($Sheet, 2, 1), DllStructGetPtr($Time, 'Year'), DllStructGetPtr($Time, 'Month'), DllStructGetPtr($Time, 'Day'))
ConsoleWrite(DllStructGetData($Time, 'Year') & '-' & DllStructGetData($Time, 'Month') & '-' & DllStructGetData($Time, 'Day') & @CRLF)
xlBookDateUnpack($BookHandle, xlSheetReadNum($Sheet, 3, 1), _
DllStructGetPtr($Time, 'Year'), _
DllStructGetPtr($Time, 'Month'), _
DllStructGetPtr($Time, 'Day'), _
DllStructGetPtr($Time, 'Hour'), _
DllStructGetPtr($Time, 'Min'), _
DllStructGetPtr($Time, 'Sec'))
ConsoleWrite(DllStructGetData($Time, 'Year') & '-' & DllStructGetData($Time, 'Month') & '-' & DllStructGetData($Time, 'Day') & @TAB & _
DllStructGetData($Time, 'Hour') & ':' & DllStructGetData($Time, 'Min') & ':' & DllStructGetData($Time, 'Sec') & @CRLF)
xlBookSave($BookHandle, @ScriptDir & '\datetime.xls')
xlBookRelease($BookHandle)
DllClose($hDLL)
Func xlCreateBook()
Local $aRet = DllCall($hDLL, 'handle:cdecl', 'xlCreateBookW')
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc ;==>xlCreateBook
Func xlBookSetKey($BookHandle, $Name = 'GCCG', $Key = 'windows-282123090cc0e6036db16b60a1o3p0h9')
Local $aRet = DllCall($hDLL, 'none:cdecl', 'xlBookSetKeyW', 'handle', $BookHandle, 'wstr', $Name, 'wstr', $Key)
If @error Then Return SetError(@error, @extended, 0)
EndFunc ;==>xlBookSetKey
Func xlBookAddFormat($BookHandle, $FormatHandle = 0)
Local $aRet = DllCall($hDLL, 'handle:cdecl', 'xlBookAddFormatW', 'handle', $BookHandle, 'handle', $FormatHandle)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlFormatSetNumFormat($FormatHandle, $sFormat)
Local $aRet = DllCall($hDLL, 'none:cdecl', 'xlFormatSetNumFormatW', 'handle', $FormatHandle, 'int', $sFormat)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlBookAddCustomNumFormat($BookHandle, $sFormat)
Local $aRet = DllCall($hDLL, 'int:cdecl', 'xlBookAddCustomNumFormatW', 'handle', $BookHandle, 'wstr', $sFormat)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlBookAddSheet($BookHandle, $sSheetName, $SheetHandle = 0)
Local $aRet = DllCall($hDLL, 'handle:cdecl', 'xlBookAddSheetW', 'handle', $BookHandle, 'wstr', $sSheetName, 'handle', $SheetHandle)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc ;==>xlBookAddSheet
Func xlSheetSetCol($SheetHandle, $colFirst, $colLast, $width, $FormatHandle = 0, $hidden = False)
Local $aRet = DllCall($hDLL, 'int:cdecl', 'xlSheetSetColW', _
'handle', $SheetHandle, _
'int', $colFirst, _
'int', $colLast, _
'double', $width, _
'handle', $FormatHandle, _
'bool', $hidden)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlSheetWriteNum($SheetHandle, $row, $col, $value, $FormatHandle = 0)
Local $aRet = DllCall($hDLL, 'int:cdecl', 'xlSheetWriteNumW', _
'handle', $SheetHandle, _
'int', $row, _
'int', $col, _
'double', $value, _
'handle', $FormatHandle)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlSheetReadNum($SheetHandle, $iRow, $iCol, $FormatHandle = 0)
Local $aRet = DllCall($hDLL, 'double:cdecl', 'xlSheetReadNumW', _
'handle', $SheetHandle, _
'int', $iRow, _
'int', $iCol, _
'handle', $FormatHandle)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlBookDatePack($BookHandle, $Year, $Month, $Day, $Hour = Null, $Min = Null, $Sec = Null, $Msec = Null)
Local $aRet = DllCall($hDLL, 'double:cdecl', 'xlBookDatePackW', _
'handle', $BookHandle, _
'int', $Year, _
'int', $Month, _
'int', $Day, _
'int', $Hour, _
'int', $Min, _
'int', $Sec, _
'int', $Month)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlBookDateUnpack($BookHandle, $Value, $Year, $Month, $Day = 0, $Hour = 0, $Min = 0, $Sec = 0, $Msec = 0)
Local $aRet = DllCall($hDLL, 'int:cdecl', 'xlBookDateUnpackW', _
'handle', $BookHandle, _
'double', $Value, _
'ptr', $Year, _
'ptr', $Month, _
'ptr', $Day, _
'ptr', $Hour, _
'ptr', $Min, _
'ptr', $Sec, _
'ptr', $Month)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc
Func xlBookSave($BookHandle, $sFileName)
Local $aRet = DllCall($hDLL, 'int:cdecl', 'xlBookSaveW', 'handle', $BookHandle, 'wstr', $sFileName)
If @error Or Not $aRet[0] Then
Return SetError(@error, @extended, 0)
Else
Return $aRet[0]
EndIf
EndFunc ;==>xlBookSave
Func xlBookRelease($BookHandle)
DllCall($hDLL, 'none:cdecl', 'xlBookReleaseW', 'handle', $BookHandle)
EndFunc ;==>xlBookRelease