Thanks to Mat for the optimised XML format.
This is an Example of the output for a ListView with 2 rows and 2 columns.
<?xml version="1.0" encoding="UTF-8" ?> <listview rows="2" cols="2"> <item> <column01>Row 1: Col 1</column01> <column02>Row 1: Col 2</column02> </item> <item> <column01>Row 2: Col 1</column01> <column02>Row 2: Col 2</column02> </item> </listview>
Function without Array:
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #FUNCTION# ========================================================================================================= ; Name...........: _GUICtrlListView_SaveXML() ; Description ...: Exports the details of a ListView to a .xml file. ; Syntax.........: _GUICtrlListView_SaveXML($hListView, $sFile, [$iDetails = 1]) ; Parameters ....: $hListView - Handle of the ListView. ; $sFile - FilePath, this should ideally use the filetype .xml e.g. @ScriptDir & "\Example.xml" ; $iDetails - [Optional] - Print the number of columns & rows. [Default = 1 - Print the number of columns & rows.] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns filepath. ; Failure - Returns filepath & sets @error = 1 ; Author ........: guinness ; Example........; Yes ;===================================================================================================================== Func _GUICtrlListView_SaveXML($hListView, $sFile, $iDetails = 1) Local $aColumnsText, $hFileOpen, $iError = 0, $sDetails = "", $sItem, $sXMLBody, $sXMLColumns, $sXMLHeader Local $iColumnCount = _GUICtrlListView_GetColumnCount($hListView) Local $iItemCount = _GUICtrlListView_GetItemCount($hListView) Local $aColumns[$iColumnCount] If $iDetails Then $sDetails = ' rows="' & $iItemCount & '" cols="' & $iColumnCount & '"' EndIf $sXMLHeader = '<?xml version="1.0" encoding="UTF-8" ?>' & @CRLF & '<listview' & $sDetails & '>' & @CRLF For $A = 0 To $iColumnCount - 1 ; ListView Columns $aColumnsText = _GUICtrlListView_GetColumn($hListView, $A) $aColumns[$A] = StringStripWS(StringLower($aColumnsText[5]), 8) Next For $A = 0 To $iItemCount - 1 ; ListView Items. $sXMLBody &= @TAB & '<item>' & @CRLF For $B = 0 To $iColumnCount - 1 $sItem = _GUICtrlListView_GetItemText($hListView, $A, $B) $sXMLBody &= @TAB & @TAB & '<' & $aColumns[$B] & '>' & $sItem & '</' & $aColumns[$B] & '>' & @CRLF Next $sXMLBody &= @TAB & '</item>' & @CRLF Next $sXMLBody &= '</listview>' & @CRLF $hFileOpen = FileOpen($sFile, 2) FileWrite($hFileOpen, $sXMLHeader & $sXMLColumns & $sXMLBody) FileClose($hFileOpen) If @error Then $iError = 1 EndIf Return SetError($iError, 0, $sFile) EndFunc ;==>_GUICtrlListView_SaveXML
Function with Array: - It uses another Function of mine called _GUICtrlListView_CreateArray()
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #FUNCTION# ========================================================================================================= ; Name...........: _GUICtrlListView_SaveXML() ; Description ...: Exports the details of a ListView to a .xml file. ; Syntax.........: _GUICtrlListView_SaveXML($aArray, $sFile, [$iDetails = 1]) ; Parameters ....: $aArray - Array returned from _GUICtrlListView_CreateArray() ; $sFile - FilePath, this should ideally use the filetype .xml e.g. @ScriptDir & "\Example.xml" ; $iDetails - [Optional] - Print the number of columns & rows. [Default = 1 - Print the number of columns & rows.] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns filepath. ; Failure - Returns filepath & sets @error = 1 ; Author ........: guinness ; Example........; Yes ;===================================================================================================================== Func _GUICtrlListView_SaveXML($aArray, $sFile, $iDetails = 1) Local $aColumns, $hFileOpen, $iError = 0, $sDetails = "", $sItem, $sXMLBody, $sXMLColumns, $sXMLHeader If $iDetails Then $sDetails = ' rows="' & $aArray[0][0] & '" cols="' & $aArray[0][1] & '"' EndIf $sXMLHeader = '<?xml version="1.0" encoding="UTF-8" ?>' & @CRLF & '<listview' & $sDetails & '>' & @CRLF $aColumns = StringSplit($aArray[0][2], "|") ; ListView Columns For $A = 1 To $aColumns[0] $aColumns[$A] = StringStripWS(StringLower($aColumns[$A]), 8) Next For $A = 1 To $aArray[0][0] ; ListView Items. $sXMLBody &= @TAB & '<item>' & @CRLF For $B = 0 To $aArray[0][1] - 1 $sItem = $aArray[$A][$B] $sXMLBody &= @TAB & @TAB & '<' & $aColumns[$B + 1] & '>' & $sItem & '</' & $aColumns[$B + 1] & '>' & @CRLF Next $sXMLBody &= @TAB & '</item>' & @CRLF Next $sXMLBody &= '</listview>' & @CRLF $hFileOpen = FileOpen($sFile, 2) FileWrite($hFileOpen, $sXMLHeader & $sXMLColumns & $sXMLBody) FileClose($hFileOpen) If @error Then $iError = 1 EndIf Return SetError($iError, 0, $sFile) EndFunc ;==>_GUICtrlListView_SaveXML
Example use of Function without Array:
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <GUIConstantsEx.au3> #include <GUIListView.au3> #include <WindowsConstants.au3> _Main() Func _Main() Local $hListView, $iButton, $iListView, $sPrintOut GUICreate("_GUICtrlListView_SaveXML()", 400, 300, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX)) $iListView = GUICtrlCreateListView("", 0, 0, 400, 270) $hListView = GUICtrlGetHandle($iListView) GUICtrlSetResizing(-1, $GUI_DOCKBORDERS) $iButton = GUICtrlCreateButton("Export XML", 400 - 80, 275, 75, 22.5) GUICtrlSetResizing(-1, $GUI_DOCKRIGHT + $GUI_DOCKSIZE + $GUI_DOCKBOTTOM) GUISetState(@SW_SHOW) __ListViewFill($hListView, Random(1, 5, 1), Random(25, 100, 1)) ; Fill the ListView with Random data. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $iButton $sPrintOut = _GUICtrlListView_SaveXML($hListView, @ScriptDir & "\Export.xml") ShellExecute($sPrintOut) EndSwitch WEnd EndFunc ;==>_Main ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #FUNCTION# ========================================================================================================= ; Name...........: _GUICtrlListView_SaveXML() ; Description ...: Exports the details of a ListView to a .xml file. ; Syntax.........: _GUICtrlListView_SaveXML($hListView, $sFile, [$iDetails = 1]) ; Parameters ....: $hListView - Handle of the ListView. ; $sFile - FilePath, this should ideally use the filetype .xml e.g. @ScriptDir & "\Example.xml" ; $iDetails - [Optional] - Print the number of columns & rows. [Default = 1 - Print the number of columns & rows.] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns filepath. ; Failure - Returns filepath & sets @error = 1 ; Author ........: guinness ; Example........; Yes ;===================================================================================================================== Func _GUICtrlListView_SaveXML($hListView, $sFile, $iDetails = 1) Local $aColumnsText, $hFileOpen, $iError = 0, $sDetails = "", $sItem, $sXMLBody, $sXMLColumns, $sXMLHeader Local $iColumnCount = _GUICtrlListView_GetColumnCount($hListView) Local $iItemCount = _GUICtrlListView_GetItemCount($hListView) Local $aColumns[$iColumnCount] If $iDetails Then $sDetails = ' rows="' & $iItemCount & '" cols="' & $iColumnCount & '"' EndIf $sXMLHeader = '<?xml version="1.0" encoding="UTF-8" ?>' & @CRLF & '<listview' & $sDetails & '>' & @CRLF For $A = 0 To $iColumnCount - 1 ; ListView Columns $aColumnsText = _GUICtrlListView_GetColumn($hListView, $A) $aColumns[$A] = StringStripWS(StringLower($aColumnsText[5]), 8) Next For $A = 0 To $iItemCount - 1 ; ListView Items. $sXMLBody &= @TAB & '<item>' & @CRLF For $B = 0 To $iColumnCount - 1 $sItem = _GUICtrlListView_GetItemText($hListView, $A, $B) $sXMLBody &= @TAB & @TAB & '<' & $aColumns[$B] & '>' & $sItem & '</' & $aColumns[$B] & '>' & @CRLF Next $sXMLBody &= @TAB & '</item>' & @CRLF Next $sXMLBody &= '</listview>' & @CRLF $hFileOpen = FileOpen($sFile, 2) FileWrite($hFileOpen, $sXMLHeader & $sXMLColumns & $sXMLBody) FileClose($hFileOpen) If @error Then $iError = 1 EndIf Return SetError($iError, 0, $sFile) EndFunc ;==>_GUICtrlListView_SaveXML Func __ListViewFill($hListView, $iColumns, $iRows) ; Required only for _Main(), but not the UDF! For $A = 0 To $iColumns - 1 _GUICtrlListView_InsertColumn($hListView, $A, "Column " & $A + 1, 50) _GUICtrlListView_SetColumnWidth($hListView, $A - 1, -2) Next For $A = 0 To $iRows - 1 _GUICtrlListView_AddItem($hListView, Chr(Random(65, 90, 1)) & " - Row " & $A + 1 & ": Col 1", $A) For $B = 1 To $iColumns _GUICtrlListView_AddSubItem($hListView, $A, "Row " & $A + 1 & ": Col " & $B + 1, $B) Next Next EndFunc ;==>__ListViewFill
Download the ZIP file to obtain all exporting Examples.
ListView Export.zip 19.96K
747 downloadsOther Examples for exporting ListView data are: _GUICtrlListView_SaveCSV(), _GUICtrlListView_SaveHTML() & _GUICtrlListView_SaveTxt()
Edited by guinness, 30 August 2011 - 05:46 AM.






