Saturday, February 15, 2014

Import Xls file via Code Class: X++

// Import Xls file via Code Class:


class ImportNewCustomer extends RunBaseBatch
{

    FileName                    filename;

    DialogField                 dialogFileName;
    int row;
     ImportCSV    Importquality;
    ImportNewCustomer       customer;

     #define.CurrentVersion(1)
    #define.Version1(1)
    #localmacro.CurrentList
        filename
    #endmacro
}


static void main(Args _args)
{

   ImportNewCustomer     customer = new ImportNewCustomer();
        if (customer.prompt())
    {
        customer.run();
    }
}

public boolean unpack(container _packedClass)
{
    Integer     version     = conpeek(_packedClass,1);
    ;
    switch (version)
    {
    case #CurrentVersion:
    [version,#CurrentList]      = _packedClass;
    break;

    default :

    return false;
    }
    return true;
}


public container pack()
{
    return [#CurrentVersion,#CurrentList];
}

public void run()
{
     this.customer();
    super();
}

public void new()
{
    super();
    row =1;
}

public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    return true;
}

public Object dialog()
{
   DialogRunbase       dialog = super();
    //DialogField          dialogFileName;
     #Excel


    dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));
    dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS100852","*.csv"]);
    dialog.filenameLookupTitle("Upload from EXCEL/CSV");
    dialogFilename.value(filename);


    return dialog;
}

public void customer()
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;

    DirPartyTable               dirPartyTable;
    CustTable                   custTable,cust;
    LogisticsPostalAddress      logisticsPostalAddress;
    LogisticsElectronicAddress  logisticsElectronicAddress;
    DirParty                        dirParty;
    DirPartyPostalAddressView       dirPartyPostalAddressView;
    DirPartyContactInfoView         dirPartyContactInfo;
    Name                            name;

    str AccountNo,Address,CurrencyC,custGrp,Location,PaymTerm,Paymode,city,StreetNo,country,zip,LocationName;
    str phone;


    str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
    {
    switch (_cv.variantType())
    {
    case (COMVariantType::VT_BSTR):
    return _cv.bStr();
    case (COMVariantType::VT_R4):
    return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
    case (COMVariantType::VT_R8):
    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
    case (COMVariantType::VT_DECIMAL):
    return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
    case (COMVariantType::VT_DATE):
    return date2str(_cv.date(),123,2,1,2,1,4);
    case (COMVariantType::VT_EMPTY):
    return "";
    default:
    throw error(strfmt("@SYS26908", _cv.variantType()));
    }
    return "";
    }




    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //specify the file path that you want to read

    try
    {
    workbooks.open(filename);
    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened.");
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1); //Here 3 is the worksheet Number
    cells = worksheet.cells();
    //row =1;
     ttsBegin;
    custTable.initValue();
    do
    {
        row++;
                AccountNo   =  COMVariant2Str(cells.item(row,1).value());
                custGrp     =  COMVariant2Str(cells.item(row,2).value());
                CurrencyC   =  COMVariant2Str(cells.item(row,3).value());
                PaymTerm    =  COMVariant2Str(cells.item(row,4).value());
                Paymode     =  COMVariant2Str(cells.item(row,5).value());
                Address     =  COMVariant2Str(cells.item(row,6).value());
                city        =  COMVariant2Str(cells.item(row,7).value());
                StreetNo       =  COMVariant2Str(cells.item(row,8).value());
                country     =  COMVariant2Str(cells.item(row,9).value());
                zip         =  COMVariant2Str(cells.item(row,10).value());
                Phone       =  COMVariant2Str(cells.item(row,11).value());
                LocationName=  COMVariant2Str(cells.item(row,12).value());
                Location   = COMVariant2Str(cells.item(row,13).value());
                name        = COMVariant2Str(cells.item(row,14).value());

                custTable.AccountNum    = AccountNo;
                custTable.CustGroup     = custGrp;
                custTable.Currency      = CurrencyC;
                custTable.PaymTermId    = PaymTerm;
                custTable.PaymMode      = Paymode;
             
                custTable.insert(DirPartyType::Organization,name);

                /* Creates a new instance of the DirParty class from an address book entity
                   that is represented by the custTable parameter. */

                dirParty = DirParty::constructFromCommon(custTable);

                dirPartyPostalAddressView.LocationName     = Location;
                dirPartyPostalAddressView.City              = city;
                dirPartyPostalAddressView.Street            = Address;
                dirPartyPostalAddressView.StreetNumber      = StreetNo;
                dirPartyPostalAddressView.CountryRegionId   = country;

                dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);

                dirPartyContactInfo.LocationName    = LocationName;
                dirPartyContactInfo.Locator         = (Phone);
                dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Phone;
                dirPartyContactInfo.IsPrimary       = NoYes::Yes;


                dirParty.createOrUpdateContactInfo(dirPartyContactInfo);

                type = cells.item(row+1, 1).value().variantType();
         ttsCommit;
    }
     while (type != COMVariantType::VT_EMPTY);
                application.quit();

}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...