首页 文章

如何在ABL编码中将数据写入CSV文件

提问于
浏览
2

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

ttcomp.inum
ttcomp.iname
ttcomp.iadd

此临时表中有5000条记录,现在我想写入CSV文件 . 我认为可以用 output stream 完成,但我不知道如何实现这一点 . 请有人帮我解决这个问题 .

3 回答

  • 8

    出口的诀窍是:

    /* 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 */
    
  • 1

    还有另一种方式 . 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.
    
  • 0

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

    /* 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.
    

相关问题