Jump to content

[Solved] Saving an Array (IE table) to a CSV file


Recommended Posts

I did a few searches and came up empty handed.

Is there already a UDF for this? I looked around and found single dimension array string manipulation, but not 2 dimension tables to export to a CSV file.

Thanks !!

Bre

Edited by BreCalmor
Link to comment
Share on other sites

You can do this with the _Excel* functions... I think this is only in the beta right now.

Modified from the beta help file:

#include <Excel.au3>

Local $oExcel = _ExcelBookNew(0) ;Create new book, make it invisible

;Declare the Arrays
Local $aArray[5][2] = [["LocoDarwin", 1],["Jon", 2],["big_daddy", 3],["DaleHolm", 4],["GaryFrost", 5]] ;0-Base Array
Dim $aArrayB[10][10]
For $a = 1 To 10
    For $b = 1 To 10
        $aArrayB[$a - 1][$b - 1] = $a * $b
    Next
Next
_ExcelWriteSheetFromArray($oExcel, $aArray, 1, 1, 0, 0) ;0-Base Array parameters
_ExcelWriteSheetFromArray($oExcel, $aArrayB, 7, 1, 0, 0) ;Add another array below the first

MsgBox(0, "Exiting", "Press OK to Save File and Exit")
_ExcelBookSaveAs($oExcel, @TempDir & "\Temp.csv", "csv", 0, 1) ; Now we save it into the temp directory; overwrite existing file if necessary
_ExcelBookClose($oExcel) ; And finally we close out

If FileExists( @TempDir & "\Temp.csv" ) Then _ExcelBookOpen( @TempDir & "\Temp.csv" ) ; Open the csv file
Link to comment
Share on other sites

or if you want you can use this: :P

#include <File.au3>
#include <array.au3>

func _CSVFiletoArray ($csvfile)
    local $tmpRow
    _FileReadToArray ($csvfile,$tmpRow)
    if @error then return -1
    if not IsArray ($tmpRow) then return -1
    $tmpHeaders = StringSplit ($tmpRow[1],",")
    if not IsArray ($tmpHeaders) then Return -1
    dim $tmpTable[$tmpRow[0]][$tmpHeaders[0]]
    for $i = 1 to $tmpRow[0]
        $CurrentRow = StringSplit ($tmpRow[$i],",")
        for $ii = 1 to $tmpHeaders[0]
            $tmpTable[$i-1][$ii-1]= $CurrentRow[$ii]
        next
    next
    return $tmpTable
EndFunc

func _ArraytoCSVFile ($filename,$array)
    if not IsArray($array) then return -1
    dim $tmpArray[ubound($array,1)]
    for $iRow = 0 to UBound($array,1)-1
        $CurrentRow = ""
        $nullrow = true
        for $iColumn = 0 to ubound($array,2)-1
            $CurrentCell = StringReplace ($array[$iRow][$iColumn],",","")
            $CurrentRow &= $currentcell &","
        next
        $CurrentRow = StringTrimRight($CurrentRow,1)
        $tmparray[$iRow] = $currentRow
    next    
    _FileWriteFromArray ($filename, $tmparray)
EndFunc
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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