zoukankan      html  css  js  c++  java
  • 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();

    }
  • 相关阅读:
    XCode9打包上传遇到的问题
    iOS Category
    ios view生成图片
    xcode8 注释快捷键不能用的解决办法
    warning: templates not found
    Quartz 2D编程指南
    关于键盘输入中文控制字数 (找了很久,最好的写法)真是完美
    小知识
    UIView 的hitTest 添加屏蔽层 但不影响下一层操作
    ios 绘制不规则 图形
  • 原文地址:https://www.cnblogs.com/Fandyx/p/2243606.html
Copyright © 2011-2022 走看看