【问题标题】:How to write data to CSV file in ABL coding如何在 ABL 编码中将数据写入 CSV 文件
【发布时间】:2014-07-02 12:45:07
【问题描述】:

我从查询中填充了一个临时表,临时表看起来像,

ttcomp.inum
ttcomp.iname
ttcomp.iadd

这个临时表中有 5000 条记录,现在我想写入一个 CSV 文件。我认为可以使用output stream 来完成,但我不知道如何实现。请有人帮助我得到这个。

【问题讨论】:

标签: csv progress-4gl openedge


【解决方案1】:

导出就可以了:

/* Define a stream */
DEFINE STREAM str.

/* Define the temp-table. I did some guessing according datatypes... */
DEFINE TEMP-TABLE ttcomp
    FIELD inum  AS INTEGER
    FIELD iname AS CHARACTER
    FIELD iadd  AS INTEGER.

/* Fake logic that populates your temp-table is here */
DEFINE VARIABLE i AS INTEGER     NO-UNDO.
DO i = 1 TO 5000:
    CREATE ttComp.
    ASSIGN 
        ttComp.inum  = i
        ttComp.iname = "ABC123"
        ttComp.iadd  = 3.

END.
/* Fake logic done... */

/* Output the temp-table */
OUTPUT STREAM str TO VALUE("c:\temp\file.csv").
FOR EACH ttComp NO-LOCK:
    /* Delimiter can be set to anything you like, comma, semi-colon etc */
    EXPORT STREAM str DELIMITER "," ttComp.
END.
OUTPUT STREAM str CLOSE.
/* Done */

【讨论】:

  • OP 要求输入逗号,而不是分号分隔的输出。否则这个例子很好。
  • @TimKuehn 好点。就像我自己通常做的那样。我会编辑答案。
  • @Jensd 很酷,我更喜欢将我的分隔符设置为设置了初始值的变量,因此如果有人希望将“,”更改为“ |"
  • @AquaAlex 这是一个好方法!我只是为您提供了一个基本示例! :)
【解决方案2】:

这是一个没有流的替代方案。

/* Using the temp-table. of Jensd*/
DEFINE TEMP-TABLE ttcomp
    FIELD inum  AS INTEGER
    FIELD iname AS CHARACTER
    FIELD iadd  AS INTEGER.

OUTPUT TO somefile.csv APPEND.

    FOR EACH ttcomp:
                DISPLAY 
                    ttcomp.inum + ",":U  
                    ttcomp.iname + ",":U
                    ttcomp..iadd  SKIP. 
    END.

OUTPUT CLOSE.

【讨论】:

    【解决方案3】:

    还有其他方法。 EXPORT 在格式无关紧要的情况下很好,因为 EXPORT 忽略格式。例如,您不希望日期格式为 mm/dd/yy。在这种情况下,最好使用 PUT STREAM 并明确指定格式。

    FOR EACH ttComp NO-LOCK:
        PUT STREAM str UNFORMATTED SUBSTITUTE("&1;&2;&3 \n", ttComp.inum, ttcomp.iname, ttcomp.iadd).
    END.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      相关资源
      最近更新 更多