Jump to content



Photo

_GUICtrlListView_SaveCSV() - Exports the details of a ListView to a .csv file.


  • Please log in to reply
2 replies to this topic

#1 guinness

guinness

    guinness

  • MVPs
  • 10,353 posts

Posted 01 June 2011 - 10:47 AM

For those who enjoy using ListViews and wish to export the data to a CSV file, then this Function is for you.

Function without Array:
AutoIt         
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #FUNCTION# ========================================================================================================= ; Name...........: _GUICtrlListView_SaveCSV() ; Description ...: Exports the details of a ListView to a .csv file. ; Syntax.........: _GUICtrlListView_SaveCSV($hListView, $sFile, [$sDelimiter = ",", $sQuote = '"']]) ; Parameters ....: $hListView - Handle of the ListView. ;                  $sFile - FilePath, this should ideally use the filetype .csv e.g. @ScriptDir & "\Example.csv" ;                  $sDelimiter - [Optional] Delimiter to be used for the csv file. [Default = ,] ;                  $sQuote - [Optional] Style of quotes to be used for the csv file. [Default = "] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns filepath. ;                  Failure - Returns filepath & sets @error = 1 ; Author ........: guinness & ProgAndy for the csv idea. ; Example........; Yes ;===================================================================================================================== Func _GUICtrlListView_SaveCSV($hListView, $sFile, $sDelimiter = ",", $sQuote = '"')     Local $hFileOpen, $iError = 0, $sItem, $sString     Local $iColumnCount = _GUICtrlListView_GetColumnCount($hListView)     Local $iItemCount = _GUICtrlListView_GetItemCount($hListView)       For $A = 0 To $iItemCount - 1         For $B = 0 To $iColumnCount - 1             $sItem = _GUICtrlListView_GetItemText($hListView, $A, $B)             $sString &= $sQuote & StringReplace($sItem, $sQuote, $sQuote & $sQuote, 0, 1) & $sQuote             If $B < $iColumnCount - 1 Then                 $sString &= $sDelimiter             EndIf         Next         $sString &= @CRLF     Next     $hFileOpen = FileOpen($sFile, 2)     FileWrite($hFileOpen, $sString)     FileClose($hFileOpen)     If @error Then         $iError = 1     EndIf     Return SetError($iError, 0, $sFile) EndFunc   ;==>_GUICtrlListView_SaveCSV
Function with Array: - It uses another Function of mine called _GUICtrlListView_CreateArray()
AutoIt         
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 ; #FUNCTION# ========================================================================================================= ; Name...........: _GUICtrlListView_SaveCSV() ; Description ...: Exports the details of a ListView to a .csv file. ; Syntax.........: _GUICtrlListView_SaveCSV($aArray, $sFile, [$sDelimiter = ",", $sQuote = '"']]) ; Parameters ....: $aArray - Array returned from _GUICtrlListView_CreateArray() ;                  $sFile - FilePath, this should ideally use the filetype .csv e.g. @ScriptDir & "\Example.csv" ;                  $sDelimiter - [Optional] Delimiter to be used for the csv file. [Default = ,] ;                  $sQuote - [Optional] Style of quotes to be used for the csv file. [Default = "] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns filepath. ;                  Failure - Returns filepath & sets @error = 1 ; Author ........: guinness & ProgAndy for the csv idea. ; Example........; Yes ;===================================================================================================================== Func _GUICtrlListView_SaveCSV($aArray, $sFile, $sDelimiter = ",", $sQuote = '"')     Local $hFileOpen, $iError = 0, $sItem, $sString      For $A = 1 To $aArray[0][0]         For $B = 0 To $aArray[0][1] - 1             $sItem = $aArray[$A][$B]             $sString &= $sQuote & StringReplace($sItem, $sQuote, $sQuote & $sQuote, 0, 1) & $sQuote             If $B < $aArray[0][1] - 1 Then                 $sString &= $sDelimiter             EndIf         Next         $sString &= @CRLF     Next     $hFileOpen = FileOpen($sFile, 2)     FileWrite($hFileOpen, $sString)     FileClose($hFileOpen)     If @error Then         $iError = 1     EndIf     Return SetError($iError, 0, $sFile) EndFunc   ;==>_GUICtrlListView_SaveCSV
Example use of Function without Array:
AutoIt         
#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_SaveCSV()", 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 CSV", 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_SaveCSV($hListView, @ScriptDir & "\Export.csv")                 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_SaveCSV() ; Description ...: Exports the details of a ListView to a .csv file. ; Syntax.........: _GUICtrlListView_SaveCSV($hListView, $sFile, [$sDelimiter = ",", $sQuote = '"']]) ; Parameters ....: $hListView - Handle of the ListView. ;                  $sFile - FilePath, this should ideally use the filetype .csv e.g. @ScriptDir & "\Example.csv" ;                  $sDelimiter - [Optional] Delimiter to be used for the csv file. [Default = ,] ;                  $sQuote - [Optional] Style of quotes to be used for the csv file. [Default = "] ; Requirement(s).: v3.2.12.1 or higher ; Return values .: Success - Returns filepath. ;                  Failure - Returns filepath & sets @error = 1 ; Author ........: guinness & ProgAndy for the csv idea. ; Example........; Yes ;===================================================================================================================== Func _GUICtrlListView_SaveCSV($hListView, $sFile, $sDelimiter = ",", $sQuote = '"')     Local $hFileOpen, $iError = 0, $sItem, $sString     Local $iColumnCount = _GUICtrlListView_GetColumnCount($hListView)     Local $iItemCount = _GUICtrlListView_GetItemCount($hListView)       For $A = 0 To $iItemCount - 1         For $B = 0 To $iColumnCount - 1             $sItem = _GUICtrlListView_GetItemText($hListView, $A, $B)             $sString &= $sQuote & StringReplace($sItem, $sQuote, $sQuote & $sQuote, 0, 1) & $sQuote             If $B < $iColumnCount - 1 Then                 $sString &= $sDelimiter             EndIf         Next         $sString &= @CRLF     Next     $hFileOpen = FileOpen($sFile, 2)     FileWrite($hFileOpen, $sString)     FileClose($hFileOpen)     If @error Then         $iError = 1     EndIf     Return SetError($iError, 0, $sFile) EndFunc   ;==>_GUICtrlListView_SaveCSV   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. Attached File  ListView Export.zip   19.96K   746 downloads

Other Examples for exporting ListView data are: _GUICtrlListView_SaveHTML(), _GUICtrlListView_SaveTxt() & _GUICtrlListView_SaveXML()

Edited by guinness, 30 August 2011 - 05:45 AM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013






#2 Grangato

Grangato

    Seeker

  • Active Members
  • 7 posts

Posted 14 July 2011 - 08:39 PM

Thanks this is marvelous, I was working on an audit program for my IT Group and was looking to add the export. This works great.

#3 guinness

guinness

    guinness

  • MVPs
  • 10,353 posts

Posted 14 July 2011 - 08:42 PM

You're Welcome. :)

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users