Jump to content



Photo

_GUICtrlListView_SaveTxt() - Exports the details of a ListView to a .txt file.


  • Please log in to reply
7 replies to this topic

#1 guinness

guinness

    guinness

  • MVPs
  • 10,386 posts

Posted 01 June 2011 - 10:51 AM

For those who enjoy using ListViews and wish to export the data to a Txt 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_SaveTxt() ; Description ...: Exports the details of a ListView to a .txt file. ; Syntax.........: _GUICtrlListView_SaveTxt($hListView, $sFile, [$sDelimiter = "|"]) ; Parameters ....: $hListView - Handle of the ListView. ;                  $sFile - FilePath, this should ideally use the filetype .txt e.g. @ScriptDir & "\Example.txt" ;                  $sDelimiter - [Optional] Delimiter to be used for the txt file. [Default = |] ; 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_SaveTxt($hListView, $sFile, $sDelimiter = "|")     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 &= $sItem             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_SaveTxt

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_SaveTxt() ; Description ...: Exports the details of a ListView to a .txt file. ; Syntax.........: _GUICtrlListView_SaveTxt($aArray, $sFile, [$sDelimiter = "|"]) ; Parameters ....: $aArray - Array returned from _GUICtrlListView_CreateArray() ;                  $sFile - FilePath, this should ideally use the filetype .txt e.g. @ScriptDir & "\Example.txt" ;                  $sDelimiter - [Optional] Delimiter to be used for the txt file. [Default = |] ; 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_SaveTxt($aArray, $sFile, $sDelimiter = "|")     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 &= $sItem             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_SaveTxt

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_SaveTxt()", 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 Txt", 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_SaveTxt($hListView, @ScriptDir & "\Export.txt")                 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_SaveTxt() ; Description ...: Exports the details of a ListView to a .txt file. ; Syntax.........: _GUICtrlListView_SaveTxt($hListView, $sFile, [$sDelimiter = "|"]) ; Parameters ....: $hListView - Handle of the ListView. ;                  $sFile - FilePath, this should ideally use the filetype .txt e.g. @ScriptDir & "\Example.txt" ;                  $sDelimiter - [Optional] Delimiter to be used for the txt file. [Default = |] ; 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_SaveTxt($hListView, $sFile, $sDelimiter = "|")     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 &= $sItem             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_SaveTxt   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   747 downloads

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

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

  • GreenCan likes this

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 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,042 posts

Posted 02 June 2011 - 11:03 AM

Next up... Export to XML, JSON, YAML, Creole, POD, EXI, BSON, Google's Protocol Buffers, reStructuredText, markdown, BBcode, MediaWiki, PmWiki, Org-mode, a billion other wiki formats, RTF, various OpenDocument formats and TeX formats... (list off the top of my head. Probably more out there)

Get working :huh2: How about a single library which just uses _GUICtrlListView_CreateArray? Rather than a thread per function. You could even do a _GUICtrlListView_Export function that takes the format as the first parameter...

I don't know where I'm going, but I'm on my way.


#3 guinness

guinness

    guinness

  • MVPs
  • 10,386 posts

Posted 02 June 2011 - 01:06 PM

It's an idea :huh2: I will be creating new versions using _GUICtrlListView_CreateArray() because it seems people are happy to use this as well. And I am considering XML! But BBCode maybe not ;)

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


#4 Reinhardt1julian

Reinhardt1julian

    Wayfarer

  • Active Members
  • Pip
  • 98 posts

Posted 14 February 2013 - 12:30 AM

A bit late, but u could do txt to listview

#5 guinness

guinness

    guinness

  • MVPs
  • 10,386 posts

Posted 14 February 2013 - 12:53 AM

That would be easy to do, why not give it a go yourself?

My aim at the moment is to update the function, which I've done, but just need to upload it.

Edited by guinness, 14 February 2013 - 12:54 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


#6 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,042 posts

Posted 14 February 2013 - 01:19 AM

AutoIt         
#include <GUIConstantsEx.au3> #include <Constants.au3> #include <GUIListView.au3> Local $sFile = "demo.txt" Local $hAppWin = GUICreate("Test .txt to listview", 600, 400) Local $cList = GUICtrlCreateListView("Company|Year", 4, 4, 592, 392) Local $hFile = FileOpen($sFile, $FO_OVERWRITE) FileWriteLine($hFile, "Benedictine Abbey Weihenstephan Brewery|768") FileWriteLine($hFile, "Andechs Monastery|1138") FileWriteLine($hFile, "Kaiserbrauerei Beck & May o.H.G.|1873") FileWriteLine($hFile, "St. James Gate Brewery|1759") FileWriteLine($hFile, "Plzeňský Prazdroj|1839") FileWriteLine($hFile, "Brew Pub København|1652") FileWriteLine($hFile, "Benleva Hotel|1706") FileWriteLine($hFile, "Coors Brewery|1873") FileWriteLine($hFile, "Anheuser-Busch Merrimack brewery|1876") FileFlush($hFile) FileClose($hFile) GUISetState() _GUICtrlListView_OpenText(GUICtrlGetHandle($cList), $sFile) Local $msg While 1     $msg = GUIGetMsg()     Switch $msg         Case $GUI_EVENT_CLOSE             ExitLoop     EndSwitch WEnd FileDelete($sFile) Func _GUICtrlListView_OpenText($hList, $sFile, $sDeliminator = "|")     Local $hFile = FileOpen($sFile, $FO_READ)     If @error Then Return SetError(1, 0, False)     Local $sRow, $asSubItems, $iIndex     While 1         $sRow = FileReadLine($hFile)         If @error Then ExitLoop         If $sRow <> "" Then             $asSubItems = StringSplit($sRow, $sDeliminator)             $iIndex = _GUICtrlListView_AddItem($hList, $asSubItems[1])             For $i = 2 To $asSubItems[0]                 _GUICtrlListView_AddSubItem($hList, $iIndex, $asSubItems[$i], $i - 1)             Next         EndIf     WEnd     Return True EndFunc   ;==>_GUICtrlListView_OpenText


It took a while to research those years :rolleyes:

Guinness I think you should set the location on your member profile to St. James Gate Brewery.

I don't know where I'm going, but I'm on my way.


#7 guinness

guinness

    guinness

  • MVPs
  • 10,386 posts

Posted 14 February 2013 - 01:25 AM

I will give it some thought and you can't rush perfection.

Edit: Thanks for providing Reinhardt1julian with an example. Hope you didn't find it a challenge!

Edited by guinness, 14 February 2013 - 01:27 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


#8 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,042 posts

Posted 14 February 2013 - 04:20 AM

I will give it some thought and you can't rush perfection.

Edit: Thanks for providing Reinhardt1julian with an example. Hope you didn't find it a challenge!

Some of them were easier than others... The Benleva Hotel I couldn't find anything about when they started brewing, and I couldn't even find the date they built the hotel either (only that it was "over 300 years old", but I doubt they were brewing there until about 50 years ago). Similarly for the Andechs Brewery, I couldn't find any reference to when they started brewing, so I went with a random date instead.

I don't know where I'm going, but I'm on my way.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users