web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to create a batch class to import an excel sheet to this form

(0) ShareShare
ReportReport
Posted on by 86

Hi Experts,

I want to know how to Create a batch class to import an excel sheet to the form

    1. Path: Path: AP-> Common -> Amortization Header upload
    2. New dialog will open for Browse
    3. Press OK
    4. This will upload excel data to the above form.

Thanks in advance for your valuable time.

I have the same question (0)
  • Suggested answer
    Komi Siabi Profile Picture
    13,107 Most Valuable Professional on at

    Hi Ax.Techn,

    Take a look at this on how to import into Ax from excel. When you have any challenge let us know.

    axpedia.blogspot.com/.../import-from-excel-file-using-x-in-ax.html

  • ax.tech Profile Picture
    86 on at

    Hi Komi Siabi,

    Thanks your valuable and knowledge. I just want to know how can i create a button on the form(front end) so, that user can click on that button browse the desired path where excel is located and it can import data from excel.

  • Suggested answer
    Komi Siabi Profile Picture
    13,107 Most Valuable Professional on at

    Hi Ax.Tech,

    Create an action menu item and call up your class in the property, add it to the form action pane.

    When you click on it, you will see the window to upload your excel file.

  • ax.tech Profile Picture
    86 on at

    Hi Komi Siabi,

    I have created a class

    class AmortizationHeader

    {

    SysExcelApplication application;

    SysExcelWorkbooks workbooks;

    SysExcelWorkbook workbook;

    SysExcelWorksheets worksheets;

    SysExcelWorksheet worksheet;

    SysExcelCells cells;

    COMVariantType type;

    Name name;

    FileName filename;

    AmortizationHeader amortizationHeader;

    int row;

    date _startDate;

    date _endDate;

    int _numberOfPeriods;

    str _vehicleNumber;

    str _invoiceNumber;

    str _documentNumber;

    str _account;

    real _debitAmount;

    real _creditAmount;

    ;

    application = SysExcelApplication::construct();

    workbooks = application.workbooks();

    //specify the file path that you want to read

    filename = "Path\\filename.xlsx";

    try

    {

    workbooks.open(filename);

    }

    catch (Exception::Error)

    {

    throw error("File cannot be opened.");

    }

    workbook = workbooks.item(1);

    worksheets = workbook.worksheets();

    worksheet = worksheets.itemFromNum(4); //Here 3 is the worksheet Number

    cells = worksheet.cells();

    do

    {

    row++;

    _startDate = cells.item(row, 1).value().bStr();

    _endDate = cells.item(row, 2).value().bStr();

    _numberOfPeriods = cells.item(row, 3).value().bStr();

    _vehicleNumber = cells.item(row, 4).value().bStr();

    _invoiceNumber = cells.item(row, 5).value().bStr();

    _documentNumber = cells.item(row, 6).value().bStr();

    _account = cells.item(row, 7).value().bStr();

    _DebitAmount = cells.item(row, 8).value().bStr();

    _creditAmount = cells.item(row, 9).value().bStr();

    AmortizationHeader.StartDate = _startDate;

    AmortizationHeader.EndDate = _endDate;

    AmortizationHeader.NumberOfPeriods = _numberOfPeriods;

    AmortizationHeader.VehicleNumber = _vehicleNumber;

    AmortizationHeader.InvoiceNumber = _invoiceNumber;

    AmortizationHeader.DocumentNumber = _documentNumber;

    AmortizationHeader.Account = _account;

    AmortizationHeader.DebitAmount = _debitAmount;

    AmortizationHeader.CreditAmount = _creditAmount;

    productType.insert();

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

    }

    while (type != COMVariantType::VT_EMPTY);

    application.quit();

    }

    but getting syntax error please help and let me where I am wrong

    Regards

  • Komi Siabi Profile Picture
    13,107 Most Valuable Professional on at

    Hi,

    Could you please, show us the error you are getting ?.

    Please, when pasting your codes,

    Use rich formatting -> Insert code

  • ax.tech Profile Picture
    86 on at

    class AmortizationHeader
    {
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    Name name;
    FileName filename;
    AmortizationHeader amortizationHeader;
    int row;
    date _startDate;
    date _endDate;
    int _numberOfPeriods;
    str _vehicleNumber;
    str _invoiceNumber;
    str _documentNumber;
    str _account;
    real _debitAmount;
    real _creditAmount;
    
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //specify the file path that you want to read
    filename = "Path\\filename.xlsx";
    try
    {
    workbooks.open(filename);
    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened.");
    }
    
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(4); //Here 3 is the worksheet Number
    cells = worksheet.cells();
    do
    {
    row  ;
    _startDate = cells.item(row, 1).value().bStr();
    _endDate = cells.item(row, 2).value().bStr();
    _numberOfPeriods = cells.item(row, 3).value().bStr();
    _vehicleNumber = cells.item(row, 4).value().bStr();
    _invoiceNumber = cells.item(row, 5).value().bStr();
    _documentNumber = cells.item(row, 6).value().bStr();
    _account = cells.item(row, 7).value().bStr();
    _DebitAmount = cells.item(row, 8).value().bStr();
    _creditAmount = cells.item(row, 9).value().bStr();
    
    
    AmortizationHeader.StartDate = _startDate;
    AmortizationHeader.EndDate = _endDate;
    AmortizationHeader.NumberOfPeriods = _numberOfPeriods;
    AmortizationHeader.VehicleNumber = _vehicleNumber;
    AmortizationHeader.InvoiceNumber = _invoiceNumber;
    AmortizationHeader.DocumentNumber = _documentNumber;
    AmortizationHeader.Account = _account;
    AmortizationHeader.DebitAmount = _debitAmount;
    AmortizationHeader.CreditAmount = _creditAmount;
    productType.insert();
    
    type = cells.item(row 1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();
    }
    amort.png

    Hi komi siabi,

    I have attached the code and errors. 

    Regards

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    You forgot to create a method. You're trying to write code direcly in class header.

    Also, please start using line indentation - code is very difficult to read without it, which is bad for you and also for everyone else who wants to read your code. It's possible if you noticed the bug if you followed the right structure. Also, don't use underscore as the prefix of local variable names. This prefix should be used for method parameters.

  • Suggested answer
    Komi Siabi Profile Picture
    13,107 Most Valuable Professional on at

    Hi ax.tech,

    So, let take a look at the errors.

    Line 1. The class already exists.

    This means there is already an existing class called. AmortizationHeader. Kindly rename this one to maybe ImportAmortizationHeader.

    Line 23. 

    You need separate your declaration from your logic by a semi-colon. so after real _creditAmount; you need at the next line.

    class ImportAmortizationHeader
    {
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    Name name;
    FileName filename;
    AmortizationHeader amortizationHeader;
    int row;
    date _startDate;
    date _endDate;
    int _numberOfPeriods;
    str _vehicleNumber;
    str _invoiceNumber;
    str _documentNumber;
    str _account;
    real _debitAmount;
    real _creditAmount;
    ;
    application = SysExcelApplication::construct();

  • ax.tech Profile Picture
    86 on at

    class ImportAmortizationHeader
    {
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    Name name;
    FileName filename;
    ImportAmortizationHeader importAmortizationHeader;
    int row;
    date _startDate;
    date _endDate;
    int _numberOfPeriods;
    str _vehicleNumber;
    str _invoiceNumber;
    str _documentNumber;
    str _account;
    real _debitAmount;
    real _creditAmount;
    ;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //specify the file path that you want to read
    filename = "Path\\filename.xlsx";
    try
    {
    workbooks.open(filename);
    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened.");
    }
    
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(4); //Here 3 is the worksheet Number
    cells = worksheet.cells();
    do
    {
    row  ;
    _startDate = cells.item(row, 1).value().bStr();
    _endDate = cells.item(row, 2).value().bStr();
    _numberOfPeriods = cells.item(row, 3).value().bStr();
    _vehicleNumber = cells.item(row, 4).value().bStr();
    _invoiceNumber = cells.item(row, 5).value().bStr();
    _documentNumber = cells.item(row, 6).value().bStr();
    _account = cells.item(row, 7).value().bStr();
    _DebitAmount = cells.item(row, 8).value().bStr();
    _creditAmount = cells.item(row, 9).value().bStr();
    
    
    AmortizationHeader.StartDate = _startDate;
    AmortizationHeader.EndDate = _endDate;
    AmortizationHeader.NumberOfPeriods = _numberOfPeriods;
    AmortizationHeader.VehicleNumber = _vehicleNumber;
    AmortizationHeader.InvoiceNumber = _invoiceNumber;
    AmortizationHeader.DocumentNumber = _documentNumber;
    AmortizationHeader.Account = _account;
    AmortizationHeader.DebitAmount = _debitAmount;
    AmortizationHeader.CreditAmount = _creditAmount;
    productType.insert();
    
    type = cells.item(row 1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);
    application.quit();
    }
    amortization.png

    Hi Komi Siabi,

    I have done as you said but still getting this.

    Regards

  • Suggested answer
    Komi Siabi Profile Picture
    13,107 Most Valuable Professional on at

    Hello,

    Just like Martin said, you need to have a method on your class.

    I suggest you use a job instead of the class to make it more easier. Create a Job and paste your code in there then run it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 611 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 529 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans