2012年6月20日星期三

How to creating multiple CSV files from one dataset

Here's a little bit of code that will create multiple CSV files depending on how large the dataset is,
and how many records you want in each CSV. Our company outsources some letters that we need to print and they only process 10,000 letters at a
time, so this was a nice and easy way to split our files up without doing it manually. I'm just going to use SalesTable records as an example.
Optional first step: Create an "Alphabet" enum.(1 = A, 2 = B, 3 = C,...) I use this to tack on an "_A", "_B", "_C", etc. to the end of the filename.
You'll need to add some logic if your csv may potentially loop more than 26 times, though.

static void THK_CSVSplit(Args _args)
{
    SalesTable              SalesTable;
    int                     recordCount;
    int                     recordsPerFile = 500;
    //amAlphabetUppercase     alphabet = amAlphabetUppercase::A;
    int                     alphabet = 65; //"A"
    CommaIO                 file;
    container               line;
    Filepath                filepath = @"C:\TestCSV\";
    String30                filePrefix = "Test";
    Filename                filename;
    #File
;
    filePath = WinAPI::browseForPath(infolog.hWnd(),"Please selected Images folder path");//FilePath = @"d:\Pic0608\";
    if(!filePath)
        return ;
       
    while select SalesTable
        where SalesTable.createdDateTime    >= DateTimeUtil::addDays(DateTimeUtil::getSystemDateTime(),-20) &&
              SalesTable.SalesType          == SalesType::Sales
    {
        if(recordCount MOD recordsPerFile == 0)
        {
            filename = strfmt("%1%2%3_%4.csv",filepath,"\\",filePrefix,num2char(alphabet));
            alphabet    +=1;
           
            file = new CommaIo(filename,#io_write);
            if(!file || file.status() != IO_Status::Ok)
                throw error("File cannot be opened");
               
            line = ["Sales ID","Account number","Order date","Sales status"];
            file.writeExp(line);
           
            info(strfmt("CSV successfully saved to %1",filename));
        }
       
        line = [    SalesTable.SalesId,
                    SalesTable.CustAccount,
                    SalesTable.createdDateTime,
                    enum2str(SalesTable.SalesStatus)
               ];
        file.writeExp(line);
       
        recordCount++;
    }
    IF(winAPI::fileExists(filePath,TRUE))
        winAPI::shellExecute(filePath);
}

没有评论:

发表评论