How to use X++ to export to CSV file

 

01)using TextBuffer class handle

 

TextBuffer
//1,使用TextBuffer从Ax 2009中导出数据到CSV文件中:
//http://www.qiuhao.com/forum-redirect-tid-13763-goto-lastpost.html#lastpost
static void Jimmy_ExportToCSVFile01(Args _args)
{
    TextBuffer              textBuffer  = new TextBuffer();
    InventTable             inventTable;
    FileIoPermission        perm;
    counter                 Lines;

    #define.ExampleFile(@"c:\test.csv")
    #define.ExampleOpenMode("W")
;
    try
    {
        // Set code access permission to help protect the use of
        perm = new FileIoPermission(#ExampleFile, #ExampleOpenMode);
        perm.assert();


        textBuffer.appendText("Item Id,");//must be "," suffix
        textBuffer.appendText("Item Name,");//must be "," suffix
        textBuffer.appendText("Item Type");
        textBuffer.appendText("\n");//next line must be "\n" suffix

        while select InventTable
            where  inventTable.ItemType == ItemType::BOM
            &&     inventTable.ItemId like "10*"
        {
            textBuffer.appendText(strfmt("%1,",inventTable.ItemId));

            textBuffer.appendText(strfmt("%1,",global::strReplace(inventTable.ItemName,",","")));//must be repace "," to ""

            textBuffer.appendText(enum2str(inventTable.ItemType));
            textBuffer.appendText("\n");
        }
        Lines = textBuffer.numLines();

        try
        {
            if (textBuffer.toFile(#ExampleFile))
                info( strfmt("File Generated as %1.total insert %2 Lines",#ExampleFile,Lines));
        }
        catch ( Exception::Error )
        {
            error ("Generated file error.");
        }
        // Close the code access permission scope.
        CodeAccessPermission::revertAssert();
    }
    catch (Exception::Deadlock)
    {
        retry;
    }
}

 

02)using [TextBuffer ] the dialog box to save the path.

//2,使用对话框提示保存CSV文件的路径:
static void Jimmy_ExportToCSVFile02(Args _args)
{
    TextBuffer          textBuffer = new TextBuffer();
    CustTable           CustTable;
    FileNameFilter      Filter = ["CSV file""*.csv"];
    FileName            FileName;
    #WinAPI
;
    FileName = winapi::getSaveFileName(infolog.hWnd(), filter, @"c:\...\desktop""Save as CSV file","csv","Customer Infomation");
    if(!FileName)
        return ;

    textBuffer.appendText("Customer,");
    textBuffer.appendText("Group,");
    textBuffer.appendText("Currency,");
    textBuffer.appendText("RecId\n");

    while select CustTable
    {
        textBuffer.appendText(strfmt("%1,", CustTable.AccountNum));
        textBuffer.appendText(strfmt("%1,", CustTable.CustGroup));
        textBuffer.appendText(strfmt("%1,", CustTable.Currency));
        textBuffer.appendText(strfmt("%1",  CustTable.RecId));

        textBuffer.appendText("\n");//next row
    }

    if (textBuffer.toFile(FileName))
    {
        info( strfmt("File Generated as %1.Total %2 Lines",FileName,textBuffer.numLines()));
        winapi::shellExecute(FileName);
    }
}

 

 

03)using the built-in SysExcelApplication class, but this method is inefficient.

 

SysExcelApplication
//3,使用内置的SysExcelApplication类导出,此方法效率极低。
static void Jimmy_ExportToCSVFile03(Args _args)
{
    SysExcelApplication     application;
    SysExcelWorkBooks       workBooks;
    SysExcelWorkBook        workBook;
    SysExcelWorkSheet       workSheet;
    SysExcelCells           cell;
    str                     filename = @"D:\test";

   ;
    application = SysExcelApplication::construct();
    workBooks   = application.workbooks();
    workBook    = workBooks.add();
    workSheet   = workBook.worksheets().itemFromNum(1);
    cell = worksheet.cells();
    cell.item(1,1).value("itemid");
    cell.item(1,2).value("ItemName");

    workbook.saveAs(filename,6);//6 just CSV excel file format
    application.quit();

}

 

posted @ 2011-11-09 20:53  Fandy Xie  Views(505)  Comments(0Edit  收藏  举报