Friday, January 24, 2014

Backup Excel data to table through X++

// Backup Excel data to table through X++

Void clicked()
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    Name name;
    FileName filename;

    EmployeeBackup employeeBackup;
    Args          args = new Args();
    int row;

    int empcode;
    str salary;
    str age;
    str YearsOfexperience;
    str empname;

    TypeOfJob jobtype;


    EmployeeDesgination employeedesgination;
    employeedesgination tmpDesgination;

    CurrencyValue currencyValue;
    currencyValue tmpCurrency;

    date dateofbirth,dateofjoining,dateofreveling;
    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //specify the file path that you want to read
    filename = "C:\\Test.xlsx";//FIle path

        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 1 is the worksheet Number
        cells = worksheet.cells();
            do

            {
                row++;

                empcode = any2int(cells.item(row,1).value().double());
                empname = cells.item(row,2).value().bstr();
                tmpDesgination = str2enum(employeedesgination,cells.item(row, 3).value().bStr());
                dateofjoining = any2date(cells.item(row,4).value().date());
                dateofreveling = any2date(cells.item(row,5).value().date());
                dateofbirth = any2date(cells.item(row,6).value().date());
                age = any2str(cells.item(row,7).value().toString());
                salary = any2str(cells.item(row,8).value().toString());
                tmpCurrency = str2enum(currencyValue,cells.item(row,9).value().bStr());
                jobtype = str2enum(jobtype,cells.item(row, 10).value().bStr());
                YearsOfexperience = any2str(cells.item(row,11).value().toString());



                 employeeBackup.EmpCode = empcode;
                 employeeBackup.EmpName = empname;
                 employeeBackup.EmployeeDesgination = tmpDesgination;
                 employeeBackup.DateofJoining = dateofjoining;
                 employeeBackup.DateOfTermination = dateofreveling;
                 employeeBackup.DateOfBirth = dateofbirth;
                 employeeBackup.Age = age;
                 employeeBackup.Salary = salary;
                 employeeBackup.CurrencyValue = tmpCurrency;
                 employeeBackup.TypeOfJob = jobtype;
                 employeeBackup.YearsOfExperience = YearsOfexperience;

                 employeeBackup.insert();

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

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...