Job example using COMVariant

static void TIDF_UploadUnitConversion(Args _args)
{
    //coded by fanddy
    // for upload Unit Conversion Purpose
    SysExcelApplication     application;
    SysExcelWorkbooks       workbooks;
    SysExcelWorkbook        workbook;
    SysExcelWorksheets      worksheets;
    SysExcelWorksheet       worksheet;
    SysExcelCells           cells;
    SysExcelCell            cell;
    COMVariantType          type;
    int                     row;

    UnitOfMeasureConversion UnitOfMeasureConversion;
    //table's field data
    ItemId                  itemid;
    UnitOfMeasureSymbol     unitTo;
    unitOfMeasureSymbol     unitFrom;
    Factor                  factor;



    Filename                fileName = 'C:\\#TECTURA#\\Master Data\\UploadFormula\\UnitConversionrev4.xlsx';

    int                     loop;
    int                     lastSerial = 1;


    application = SysExcelApplication::construct();
    workbooks   = application.workbooks();

    try
    {
        workbooks.open(fileName);
    }
    catch
    {
        throw error(strFmt("File: %1 cannot be opened", fileName));
    }

    workbook    = workbooks.item(1);
    worksheets  = workbook.worksheets();

    worksheet   = worksheets.itemFromNum(1);
    cells       = worksheet.cells();

    row         = 2;
    type        = cells.item(row, 1).value().variantType();

    ttsBegin;
    while (type != COMVariantType::VT_EMPTY)
    {
        itemid      = cells.item(row, 1).value().bStr();
        unitFrom    = cells.item(row, 2).value().bStr();
        unitTo      = cells.item(row, 3).value().bStr();
        factor      = cells.item(row, 4).value().double();

        UnitOfMeasureConversion.clear();
        UnitOfMeasureConversion.Product             = EcoResProduct::findByProductNumber(itemid).RecId;
        UnitOfMeasureConversion.FromUnitOfMeasure   = UnitOfMeasure::findBySymbol(unitFrom).RecId;
        UnitOfMeasureConversion.ToUnitOfMeasure     = UnitOfMeasure::findBySymbol(unitTo).RecId;
        UnitOfMeasureConversion.Factor              = factor;
        UnitOfMeasureConversion.Numerator           = 1;
        UnitOfMeasureConversion.Denominator         = 1;

        UnitOfMeasureConversion.insert();
        row++;
        type    = cells.item(row, 1).value().variantType();
    }
    ttsCommit;
    application.quit();

    info(strFmt('Upload Success'));
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s