12.10.19          Insert Data into a CSV file

This is essentially the same as the example above to insert data into a text file.  There have been many inquiries, so here is an explicit example.

 

# Report3 creates a CSV File (comma separated values)

#  used for report data in Excel

# It first Tests if ReportClient3.csv file exists,

# then enters a row of time stamped data

# if not, create the csv with column header row

 

if {[file exists "../ReportClient3.csv"]} then {

  set fileid [open "../ReportClient3.csv" a+]

  puts $fileid "[GETVAL %TTMDATE],[GETVAL %TTMTIME],[GETVAL AMPLITUDE],[GETVAL TIMER] "

  close $fileid

 } else {

     set fileid [open "../ReportClient3.csv" a+]

     seek $fileid 0 start

     # Column Headers - optional

     puts $fileid "DATE, TIME, AMPLITUDE, TIMER"

     #Insert data into table

     puts $fileid "[GETVAL %TTMDATE],[GETVAL %TTMTIME],[GETVAL AMPLITUDE],[GETVAL TIMER] "

     close $fileid

 }

If you need a report of values that can be imported to Excel (the common use for CSV files), then you should also look at the Scheduled Reports feature in WebAccess and the Analog Tag log; both create HTML formatted reports that can be copied to EXCEL.  Scheduled reports can be emailed to end users who can then copy them into and EXCEL spreadsheet using copy-paste.