使用X++导出CSV文件3种方法汇总

1,使用TextBuffer从Ax 2009中导出数据到CSV文件中:
static void ExportToCSVFile01(Args _args)  
{  
    TextBuffer              textBuffer  = new TextBuffer();  
    InventTable             inventTable;  
    FileIoPermission        perm;  
    counter                 Lines;  
       
    #define.ExampleFile(@"c:\\AX2009.csv")  
    #define.ExampleOpenMode("W")  
;  
    try 
    {  
        perm = new FileIoPermission(#ExampleFile, #ExampleOpenMode);  
        perm.assert();  
   
           
        textBuffer.appendText("Item Id,");//必须用逗号分开
        textBuffer.appendText("Item Name,");//必须用逗号分开
        textBuffer.appendText("Item Type");  
        textBuffer.appendText("\n");//下一行必须以回车分开 
   
        while select InventTable  
            where  inventTable.ItemType == ItemType::BOM  
            &&     inventTable.ItemId like "20*" 
        {  
            textBuffer.appendText(strfmt("%1,",inventTable.ItemId));  
              
            textBuffer.appendText(strfmt("%1,",global::strReplace(inventTable.ItemName,",","")));
              
            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.");  
        }  
        CodeAccessPermission::revertAssert();  
    }  
    catch (Exception::Deadlock)  
    {  
        retry;  
    }  

2,使用对话框提示保存CSV文件的路径:
static void ExportToCSVFile03(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);  
    }  



3,使用内置的SysExcelApplication类导出,此方法效率极低。

static void ExportToCSVFile02(Args _args)  
{  
    SysExcelApplication         application;  
    SysExcelWorkBooks       workBooks;  
    SysExcelWorkBook        workBook;  
    SysExcelWorkSheet       workSheet;  
    SysExcelCells                 cell;  
    str                                 filename = @"c:\\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 @ 2014-01-02 09:25  Huaen  阅读(404)  评论(0编辑  收藏  举报