WinCC OA read csv file...


A simple code how to read/import a CSV file into an array (dyn_dyn_string).

main()
{
 string content;
 fileToString(DATA_PATH+"/csvtest.txt", content); // read file into a string variable

 dyn_dyn_string result;
 dyn_string lines = strsplit(content, "\n"); // split the string into lines
 for ( int i=1; i<=dynlen(lines); i++ ) {
  result[i]=strsplit(lines[i], ";"); // delimiter ";"
 }
 DebugTN(result);
}



Limitations:
* Don't do it in that way with very big files because it is read from the file into memory
* The delimiter ";" cannot be inside of a cell value

With oracle you can use external tables to access CSV files. After creating an external table it is possible to access the file by SQL:

select * from ext_tab

CREATE DIRECTORY EXT_DIR AS '/home/oracle/data';

-- Create table
create table ext_tab
(
Col1 VARCHAR2(30),
Col2 VARCHAR2(30),
Col3 VARCHAR2(30)
)
organization external
(
type ORACLE_LOADER
default directory EXT_DIR
access parameters
(
RECORDS DELIMITED BY NEWLINE FIELDS TERMINATED BY ";"
)
location (EXT_DIR:csvtest.csv')
)
reject limit UNLIMITED;
http://www.rocworks.at/wordpress/?p=492

Last update:
2014-08-03 12:18
Author:
Andreas Vogler
Revision:
1.0
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

Comment of Todd Malone:
Be careful about older ASCII files. If your new OA project is UTF-8, you will ... show moreget errors. After the read into local string or dyn_string, be sure to run recode(string, "ISO88591") to convert from old ISO to UTF-8.
Added at: 2014-12-15 23:10