Jump to content

Recommended Posts

Posted (edited)

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

Function without an array:

#include <Constants.au3>
#include <GUIListView.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListView_SaveTxt
; Description ...: Exports a listview to a txt file.
; Syntax ........: _GUICtrlListView_SaveTxt($hListView, $sFilePath[, $sDelimiter = '|'])
; Parameters ....: $hListView           - Control ID/Handle to the control
;                  $sFilePath           - Filepath to save the txt data string to.
;                  $sDelimiter          - [optional] Delimiter to distinguish between columns. Default is |.
; Return values .: Success - True
;                  Failure - False and sets @error to non-zero.
; Author ........: guinness
; Remarks .......: GUICtrlListView.au3 should be included.
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListView_SaveTxt($hListView, $sFilePath, $sDelimiter = '|')
    If $sDelimiter = Default Then
        $sDelimiter = '|'
    EndIf

    Local Const $iColumnCount = _GUICtrlListView_GetColumnCount($hListView) - 1
    Local Const $iItemCount = _GUICtrlListView_GetItemCount($hListView) - 1
    Local $sReturn = ''
    For $i = 0 To $iItemCount
        For $j = 0 To $iColumnCount
            $sReturn &= _GUICtrlListView_GetItemText($hListView, $i, $j)
            If $j < $iColumnCount Then
                $sReturn &= $sDelimiter
            EndIf
        Next
        $sReturn &= @CRLF
    Next

    Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
    If $hFileOpen = -1 Then
        Return SetError(1, 0, False)
    EndIf
    FileWrite($hFileOpen, $sReturn)
    FileClose($hFileOpen)
    Return True
EndFunc   ;==>_GUICtrlListView_SaveTxt

Function with an array: - It uses another function called >_GUICtrlListView_CreateArray()


#include <Constants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListView_SaveTxtEx
; Description ...: Exports a listview to a txt file.
; Syntax ........: _GUICtrlListView_SaveTxtEx(Const Byref $aArray, $sFilePath[, $sDelimiter = '|'])
; Parameters ....: $aArray              - [in/out and const] A 2-dimensional array returned by _GUICtrlListView_CreateArray.
;                  $sFilePath           - Filepath to save the txt data string to.
;                  $sDelimiter          - [optional] Delimiter to distinguish between columns. Default is |.
; Return values .: Success - True
;                  Failure - False and sets @error to non-zero.
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListView_SaveTxtEx(ByRef Const $aArray, $sFilePath, $sDelimiter = '|')
    If $sDelimiter = Default Then
        $sDelimiter = '|'
    EndIf

    Local Const $iColumnCount = $aArray[0][1] - 1
    Local $sReturn = ''
    For $i = 1 To $aArray[0][0]
        For $j = 0 To $iColumnCount
            $sReturn &= $aArray[$i][$j]
            If $j < $iColumnCount Then
                $sReturn &= $sDelimiter
            EndIf
        Next
        $sReturn &= @CRLF
    Next

    Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
    If $hFileOpen = -1 Then
        Return SetError(1, 0, False)
    EndIf
    FileWrite($hFileOpen, $sReturn)
    FileClose($hFileOpen)
    Return True
EndFunc   ;==>_GUICtrlListView_SaveTxtEx

Download the ZIP file to obtain all exporting UDFs as well as an example of usage. ListView Export.zip

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

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

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...

Posted

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 ;)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 year later...
Posted (edited)

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

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

#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.

Posted (edited)

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

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 2/14/2013 at 1:25 AM, 'guinness said:

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.
  • 3 months later...
Posted (edited)

After 2 years and 750+ downloads, I decided now was the time to release a new version of exporting to a Txt format from a listview. An example as well as all the exporting UDFs (minus the HTML for now) can be found in the ZIP file. Download and enjoy!

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • Moderators
Posted

debugcs,

This is an English-speaking forum. If you must post in your native tongue, please add a translation as you did >here. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

It would be massive though, that's why I scrapped the idea ages ago.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...