Page 1 of 1

Output ListIconGadget to .csv

Posted: Sun Mar 16, 2008 12:22 am
by Rook Zimbabwe
Here is the procedure I use to output a ListIconGadget() to a .csv file.

Code: Select all

Procedure ExporttoCSV()
  ;*
  yy$ = FormatDate("%yy%mm%dd",Date()) ; I use this for the output filename
  by$ = GetGadgetText(#Text_REPORT) ; this variable holds my report name portion - point to your name for this file
  FileName$ = yy$+by$+".csv" ; add those two variables and add a .csv for comma saved
    StandardFile$ = "C:\My Documents\"+Filename$+""  ; set initial file+path to display in filerequester
      Pattern$ = "CSV (*.csv)|*.csv|Text (*.txt)|*.txt|All files (*.*)|*.*"
        Pattern = 0    
  File$ = SaveFileRequester("Please choose Where to SAVE...", StandardFile$, Pattern$, Pattern)
  If File$
    MessageRequester("Information", "You have saved following file:" + Chr(10) + File$, 0) ; those running unicode may have to use #CRLF (I think)
  Else
    MessageRequester("Information", "The requester was canceled.", 0) 
  EndIf

  OpenFile(#File, File$) ; you will have to declare a #File
  
  Result = CountGadgetItems(#ListIcon_BIG) ; name of my ListIcon() thingee
  For cc = 0 To COLFLAG - 1
    tit$ = GetGadgetItemText(#ListICon_BIG, -1,cc) ; get text of column headers
      tit$ = tit$ +","
      title$ = title$ + tit$
        tit$ = ""
  Next
  WriteStringN(#File, title$) ; write the title string
  title$ = ""
  For lin = 0 To Result - 1 ; number of lines
     For col = 0 To COLFLAG - 1 ; number of columns
      plink$ = GetGadgetItemText(#ListIcon_BIG,lin,col) ; get that LOCATION and put it in plink$
      plink$ = plink$+"," ; add a comma (this is a CSV file)
        plonk$ = plonk$ + plink$ ; add plink$ to plonk$
        Debug "PLoNK: "+plonk$
        ; plink$ = "" ; kill value in plink$
          If col = COLFLAG ; EOL reached
            cut = Len(plonk$)  - 1
              plonk$ = Left(plonk$,cut) ; cut off the last ","
          EndIf
       Next
       WriteStringN(#File, plonk$) ; write the entire plonk$
       plonk$ = "" ; kill value in plonk$
    Next
   CloseFile(#File) 
EndProcedure
Documenting now... NOW commented version... 8)

Posted: Mon Mar 17, 2008 6:56 am
by Baldrick
Very similar to a proc I have recently used in a little project of mine.
Might suggest maybe though that you might be better off to use CreateFile() instead of OpenFile(). This way there is no chance of a user writing a double lot of data to the same file. Also, you might look at your StandardFile$ = "C:\My Documents\"+Filename$+"" & maybe lose the "C:" reference as not every1 will neccessarily be using C drive as the root.

Posted: Mon Mar 17, 2008 7:44 am
by pdwyer
I have something similar too :)

Couple of points you might consider:

1. Use quotes as some fields might have commas in them and this can create problems for the output, a no-brain method is just quote everything

2. For large exports (several thousand lines + ) this can get very slow due to the string catting, if you write into a large buffer or lines into an array or something you can get dramatic performance increases.

Posted: Mon Mar 17, 2008 3:02 pm
by Rook Zimbabwe
:D
Lets start backwards:
1. Use quotes as some fields might have commas in them
Yes, and some fields could have " in them too... If you were going to output as CSV you would need to organize your data a bit. 8)

I hadn't considered the array idea though... nice one. I will let the user code that up. Lazy me!

C
reateFile() instead of OpenFile().
That is my oops, not read the procedure I cut out character by character... I create the file elsewhere... Oy! Being stupid again. :oops:
StandardFile$ = "C:\My Documents\"+Filename$+""
This is actually just a default for the File Requester Gadget... User can click anywhere... OR the programmer can code up a custom location really easy by editing the string. 8)