从Progress Data创建CSV文件

时间:2013-03-20 09:16:52

标签: csv progress-4gl

我正在尝试导出一些数据库记录以创建CSV文件。以下代码创建CSV文件:

方法PRIVATE VOID GenerateCSV():

    OUTPUT TO VALUE ("c:users\mark\test.csv").

    FOR EACH a-table WHERE a-table.id = 1: 

          EXPORT DELIMITER ',' a-table.name a-table.cost a-table.unit.

    END. 

    OS-COMMAND NO-WAIT "c:\users\mark\test.csv". 

结束方法。

这确实有效并创建了一个包含所有必需记录的csv文件但是我想知道如何将标题放入csv文件中,因此对于每个我希望它们说[Name] [Cost] [Unit]。当在excel中打开csv文件时,列具有标题。

有没有人有任何想法如何做到这一点?

2 个答案:

答案 0 :(得分:0)

OUTPUT TO VALUE ("c:users\mark\test.csv").

export delimiter "," "Head1" "Head2" "Head3".

FOR EACH a-table WHERE a-table.id = 1: 
...

答案 1 :(得分:0)

以下是一些代码,用于将三个表提取到3个不同的csv文件,并根据您是在windows还是unix中运行来设置它们将写入的路径。文件名也在运行时创建。

使用逗号分隔的条目创建标头记录(使用数据库字段插入并排除表名,然后在两者之间键入分隔符: - )

我还有一个日志文件,可以在需要时用来写出任何错误等。

如果出现错误,文件将在finally块中关闭。

DEFINE VARIABLE cLogPathFile       AS CHARACTER NO-UNDO.
DEFINE VARIABLE cDataPathFile      AS CHARACTER NO-UNDO.
DEFINE VARIABLE cLogOutputPath     AS CHARACTER NO-UNDO.
DEFINE VARIABLE cDataOutputPath    AS CHARACTER NO-UNDO.

DEFINE BUFFER tbl_1       FOR tbl_1.
DEFINE BUFFER tbl_2       FOR tbl_2.
DEFINE BUFFER tbl_3       FOR tbl_3.

DEFINE NEW SHARED STREAM sm_log_file.
DEFINE NEW SHARED STREAM sm_tbl1.
DEFINE NEW SHARED STREAM sm_tbl2.
DEFINE NEW SHARED STREAM sm_tbl3.

IF OPSYS = "UNIX" THEN 
  DO:
    cDataOutputPath = "/Data/Out/".
    cLogOutputPath  = "/Data/".
  END.
ELSE
  DO:
    cDataOutputPath = "T:~\Data~\Out~\".
    cLogOutputPath  = "T:~\Data~\".
  END.

ASSIGN cLogPathFile = cLogOutputPath  + "myfile_" + STRING(TODAY,"999999") + "_" + STRING(TIME,"999999") + ".log" NO-ERROR.
{ mip/inc/mipreturnerror.i }
ASSIGN cDataPathFile = cDataOutputPath  + "myfile_" + "[Report Name]" + "_" + STRING(TODAY,"999999") + "_" + STRING(TIME,"999999") + ".csv" NO-ERROR.
{ mip/inc/mipreturnerror.i }

OUTPUT STREAM sm_log_file TO VALUE(cLogPathFile). 
OUTPUT STREAM sm_tbl1     TO VALUE(REPLACE(cDataPathFile, "[Report Name]", "tbl_1")). 
OUTPUT STREAM sm_tbl2     TO VALUE(REPLACE(cDataPathFile, "[Report Name]", "tbl_2")). 
OUTPUT STREAM sm_tbl3     TO VALUE(REPLACE(cDataPathFile, "[Report Name]", "tbl_3")). 

 PUT STREAM sm_tbl1 "category_obj,created_datetime,effective_date,lookup_column_heading,owning_entity_mnemonic,owning_obj,user_key,user_mnemonic,user_obj,worksheet_description,worksheet_name,worksheet_obj" SKIP.
 PUT STREAM sm_tbl2 "column_data_type,column_heading,column_number,column_obj,worksheet_obj" SKIP.
 PUT STREAM sm_tbl3 "cell_character_value,cell_date_value,cell_numeric_value,cell_text_value,column_heading,lookup_character_value,lookup_date_value,lookup_numeric_value,row_number,value_obj,worksheet_obj" SKIP.

FOR EACH tbl_1 NO-LOCK:
    EXPORT STREAM sm_tbl1 DELIMITER "," tbl_1.category_obj tbl_1.created_datetime tbl_1.effective_date agm_worksheet.lookup_column_heading tbl_1.owning_entity_mnemonic tbl_1.owning_obj tbl_1.user_tbl_1.user_mnemonic tbl_1.user_obj tbl_1.worksheet_description tbl_1.worksheet_name tbl_1.worksheet_obj.

    FOR each tbl_2 NO-LOCK WHERE tbl_2.worksheet_obj = tbl_1.worksheet_obj.
      EXPORT STREAM sm_tbl2 DELIMITER "," tbl_2.column_data_type tbl_2.column_heading tbl_2.column_number tbl_2.column_obj tbl_2.worksheet_obj.
    END.

    FOR each tbl_3 NO-LOCK WHERE tbl_3.worksheet_obj = tbl_1.worksheet_obj:
        EXPORT STREAM sm_tbl3 DELIMITER "," tbl_3.cell_character_value tbl_3.cell_date_value tbl_3.cell_numeric_value tbl_3.cell_text_value tbl_3.column_heading tbl_3.lookup_character_value tbl_3.lookup_date_value tbl_3.lookup_numeric_value tbl_3.row_number tbl_3.value_obj tbl_3.worksheet_obj.
    END.
END.

FINALLY:
    OUTPUT STREAM sm_log_file CLOSE. 
    OUTPUT STREAM sm_tbl1     CLOSE. 
    OUTPUT STREAM sm_tbl2     CLOSE. 
    OUTPUT STREAM sm_tbl3     CLOSE. 
END FINALLY.