Monday, February 17, 2014

Export Data into CSV

Using CommaIO

static void ExportDataToCSV(Args _args)
{
    Query                             q;
    QueryBuildDataSource    qbds;
    QueryBuildRange            qbr;
    QueryRun                       qr;
    CommaIO                       commaIO;
    FileName                        fileName;
    InventTable                     inventTable;
    ;

    fileName       = WINAPI::getTempPath() + "ItemDetails" + ".csv";
    commaIO      = new CommaIO(fileName,'W');

    q                  = new Query();
    qbds             = q.addDataSource(tablenum(InventTable));
    qbr               = qbds.addRange(fieldnum(InventTable,ItemId));

    qr                = new QueryRun(q);

    commaIO.write("ItemId","Item Name","Item Type","Item GroupId","Dimension GroupId","Model GroupId");
    while( qr.next() )
    {
        inventTable = qr.get(tablenum(InventTable));

        commaIO.write(inventTable.ItemId,inventTable.ItemName,enum2str(inventTable.ItemType),inventTable.ItemGroupId,
                      inventTable.DimGroupId,inventTable.ModelGroupId);
    }

    WINAPI::shellExecute(fileName);
}

Another way by using TextIO

public void ExportDataToCSVDelimit()
{
    Query q;
    QueryBuildDataSource qbds, qbdsworker;
    QueryBuildRange qbr;
    QueryRun qr;
    FileName fileName;
    A_PTSTransferToPayItems A_PTSTransferToPayItems;
    DictTable dictTable;
    DictField dictField;
    int fldCnt;
    HcmWorker hcmworker;
    boolean exported = false;
    TextIO file;
    container line;

    #File
    ;
    fileName       = JmgParameters::find().PayFileName;
    file = new TextIO(filename, 'W'); //#io_write);
    file.outFieldDelimiter(';');// for semicolon seperator

    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
    q = this.queryRun().query();

    qbdsworker = q.dataSourceTable(tableNum(HcmWorker));
    qbds = qbdsworker.addDataSource(tablenum(A_PTSTransferToPayItems));
    qbds.addLink(fieldNum(HcmWorker,RecId),fieldNum(A_PTSTransferToPayItems,HcmWorkerRecId));

    qr = new QueryRun(q);

    while(qr.next())
    {
        A_PTSTransferToPayItems = qr.get(tablenum(A_PTSTransferToPayItems));

        A_PTSTransferToPayItems = qr.get(tablenum(A_PTSTransferToPayItems));
        line = [A_PTSTransferToPayItems.HcmWorkerRecId,A_PTSTransferToPayItems.ProfileDate,A_PTSTransferToPayItems.PayType,
                      A_PTSTransferToPayItems.PayUnit,'Dim1','Dim2'];
        file.writeExp(line);
    }
}

Another way to export data into CSV file

public void Job_File_IO_TextIo_Write_Read()
{
    TextIo txIoRead, file,
    txIoWrite;
    FileIOPermission fioPermission;
    container containFromRead;
    int xx,
        iConLength;
    str sTempPath,
        sFileName = "Test_File_IO.txt",
        sOneRecord;
    str strdate,name,temp;
    date currentDate = today();
    List list;
    ListEnumerator enumerator;
    Query q;
    QueryBuildDataSource qbds, qbdsworker;
    QueryBuildRange qbr;
    QueryRun qr;
    container line;
    A_PTSTransferToPayItems A_PTSTransferToPayItems;
    boolean exported;

    strdate= date2Str
        (currentDate,
        123,
        DateDay::Digits2,

        DateSeparator::Hyphen,
        DateMonth::Digits2,
        DateSeparator::Hyphen,

        DateYear::Digits4
        );

    #File
    // Get the temporary path.
    //sTempPath = WINAPI::getTempPath();
    name = JmgParameters::find().PayFileName;
    list = strSplit(name,".");
    enumerator = list.getEnumerator();

    while(enumerator.moveNext()) {
        temp = enumerator.current();
        break;
    }
    sfileName = strFmt("%1_%2.csv",temp,strdate);
    //info("File is at: " + sFileName);

    // Assert permission.
    fioPermission = new FileIOPermission(sFileName ,"RW");
    fioPermission.assert();

    // If the test file already exists, delete it.
    /*if (WINAPI::fileExists(sFileName))
    {
        WINAPI::deleteFile(sTempPath + sFileName);
    }*/

    // Open a test file for writing.
    // "W" mode overwrites existing content, or creates the file.
    txIoWrite = new TextIo( sTempPath + sFileName ,"W");
    txIoWrite.outFieldDelimiter(','); // for semicolon seperator
    /*
    // Write records to the file.
    txIoWrite.write("Hello        World.");
    txIoWrite.write("The sky is blue.");
    txIoWrite.write("");
    txIoWrite.write("// EOFile");

    // Close the test file.
    txIoWrite = null;

    // Open the same file for reading.
    txIoRead = new TextIo(sTempPath + sFileName ,"R");
    // Read the entire file into a container.
    containFromRead = txIoRead.read();

    // Loop through the container of file records.
    while (containFromRead)
    {
        sOneRecord = "";
        iConLength = conLen(containFromRead);
        // Loop through the token in the current record.
        for (xx=1; xx <= iConLength; xx++)
        {
            if (xx > 1) sOneRecord += " ";
            sOneRecord += conPeek(containFromRead ,xx);
        }
        info(sOneRecord);

        // Read the next record from the container.
        containFromRead = txIoRead.read();
    }
    */
    q = this.queryRun().query();
    //info(q.toString());
    qbdsworker = q.dataSourceTable(tableNum(ProjWorkerSetup));
    qbds = qbdsworker.addDataSource(tablenum(A_PTSTransferToPayItems));
    qbds.addLink(fieldNum(ProjWorkerSetup,Worker),fieldNum(A_PTSTransferToPayItems,HcmWorkerRecId));

    if(payDate)
    {
        qbr = qbds.addRange(fieldNum(A_PTSTransferToPayItems,ProfileDate));
        qbr.value(queryValue(payDate));
    }
    qr = new QueryRun(q);
    //info(q.toString());
    line = conNull();
    while(qr.next())
    {
        A_PTSTransferToPayItems = qr.get(tablenum(A_PTSTransferToPayItems));
        line = [HcmWorker::find(A_PTSTransferToPayItems.HcmWorkerRecId).name(),A_PTSTransferToPayItems.ProfileDate,A_PTSTransferToPayItems.PayType,
                      A_PTSTransferToPayItems.PayUnit,'',''];
        //file.writeExp(line);
        txIoWrite.write(line);
        exported = true;
    }
    CodeAccessPermission::revertAssert();
    if(exported == true)
         info(strFmt("Pay Item exported successfully %1",sfileName));
    // Close the test file.
    txIoRead = null;
    // Delete the test file.
    //WINAPI::deleteFile(sTempPath + sFileName);

    // revertAssert is not really necessary here,
    // because the job (or method) is ending.
    CodeAccessPermission::revertAssert();
}

No comments:

Post a Comment