Jump to content

Recommended Posts

Posted (edited)

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

Function without an array:

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

; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListView_SaveCSV
; Description ...: Exports a listview to a CSV file.
; Syntax ........: _GUICtrlListView_SaveCSV($hListView, $sFilePath[, $sDelimiter = '[, $sQuote = '"']])
; Parameters ....: $hListView           - Control ID/Handle to the control
;                  $sFilePath           - Filepath to save the CSV data string to.
;                  $sDelimiter          - [optional] CSV delimiter. Default is ,.
;                  $sQuote              - [optional] CSV quote type. Default is ".
; Return values .: Success - True
;                  Failure - False and sets @error to non-zero.
; Author ........: guinness
; Remarks .......: Thanks to Prog@ndy for the idea of CSV creation. GUICtrlListView.au3 should be included.
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListView_SaveCSV($hListView, $sFilePath, $sDelimiter = ',', $sQuote = '"')
    If $sDelimiter = Default Then
        $sDelimiter = ','
    EndIf
    If $sQuote = Default Then
        $sQuote = '"'
    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 &= $sQuote & StringReplace(_GUICtrlListView_GetItemText($hListView, $i, $j), $sQuote, $sQuote & $sQuote, 0, 1) & $sQuote
            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_SaveCSV
Function with an array: - It uses another function called >_GUICtrlListView_CreateArray()

#include <Constants.au3>; #FUNCTION# ====================================================================================================================
; Name ..........: _GUICtrlListView_SaveCSVEx
; Description ...: Exports a listview to a CSV file.
; Syntax ........: _GUICtrlListView_SaveCSVEx(Const Byref $aArray, $sFilePath[, $sDelimiter = '[, $sQuote = '"']])
; Parameters ....: $aArray              - [in/out and const] A 2-dimensional array returned by _GUICtrlListView_CreateArray.
;                  $sFilePath           - Filepath to save the CSV data string to.
;                  $sDelimiter          - [optional] CSV delimiter. Default is ,.
;                  $sQuote              - [optional] CSV quote type. Default is ".
; Return values .: Success - True
;                  Failure - False and sets @error to non-zero.
; Author ........: guinness
; Remarks .......: Thanks to Prog@ndy for the idea of CSV creation.
; Example .......: Yes
; ===============================================================================================================================
Func _GUICtrlListView_SaveCSVEx(ByRef Const $aArray, $sFilePath, $sDelimiter = ',', $sQuote = '"')
    If $sDelimiter = Default Then
        $sDelimiter = ','
    EndIf
    If $sQuote = Default Then
        $sQuote = '"'
    EndIf

    Local Const $iColumnCount = $aArray[0][1] - 1
    Local $sReturn = ''
    For $i = 1 To $aArray[0][0]
        For $j = 0 To $iColumnCount
            $sReturn &= $sQuote & StringReplace($aArray[$i][$j], $sQuote, $sQuote & $sQuote, 0, 1) & $sQuote
            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_SaveCSVEx
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_SaveHTML(), >_GUICtrlListView_SaveTxt() & >_GUICtrlListView_SaveXML()

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 month later...
Posted

You're Welcome. :)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 year later...
Posted (edited)

After 2 years and 750+ downloads, I decided now was the time to release a new version of exporting to a CSV 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

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