Skip to main content
InSource KnowledgeCenter

TN WW156 How to create a string of data for a CSV file

Description

 

This article from InSource shows how to use the StringBuilder() class to create a string of data for a Comma Separated Values or CSV file. 

  • Author: Michael Walker
  • Published: 03/16/2016
  • Applies to: Wonderware Application Server (All Versions)

Details

When scripting the content of a .CSV file, the StringBuilder class should be used. Using the StringBuilderprovides a memory efficient way to build content for a .CSV file, with the ability to append multiple string values and records together. Using the “ + “ operator to build strings could be used, however each string that is called to be appended will create a new string object in memory, therefore making the script inefficient.   The StringBuilder class will be declared as a variable of type System.String.StringBuilder. Before using the string builder, the declared variable has to be set to a new System.Text.StringBuilder();  Using the newly declared variable values can be appended using the .Append() Method, also the .AppendLine() method can be used to add a new line within the .CSV content string. The figure below shows a script that creates new StringBuilder variable and populates it with data. It then sends the string as a message to the system Management Console.

The resulting message is logged to the SMC log viewer.

Value 1, 1234, HELLO WORLD!, 3/9/2016 3:21:24 PM

Value 2, 5678, HELLO WORLD, AGAIN!, 3/9/2016 3:21:24 PM

Attached is a text document that provides the sample script.