0

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

方法私有无效生成CSV():

    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 文件时,这些列都有标题。

有谁知道如何做到这一点?

4

2 回答 2

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

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

FOR EACH a-table WHERE a-table.id = 1: 
...
于 2013-03-20T10:16:14.100 回答
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.
于 2013-05-20T16:54:32.110 回答