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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to Import data(Header and Lines) from excel to ax 2012 through x++ code?

(0) ShareShare
ReportReport
Posted on by 2,172

Hi Experts,

I am having Transaction Form, which is having Header with respective lines. My task is to import data from excel to AX 2012(Through Code Only) by validating header with respective lines. Also I am having auto number sequence field in Header. How to do this? Please give me your valuable ideas. And I am  having some simple X++ Code for import data to single table through form by clicked method. I am sharing it below.

void Clicked()
{
AsciiIO asciiIO;
Filename filename;
NoYesId skipFirstLine;
Container line;
Dialog dialog;
DialogField dialogFileName, dialogSkipFirstLine;
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
Name name;
ProductType PType;
int row;
int _PTypeId;
str _PType;
str _description;
;

dialog = new Dialog("Import file");
dialogFileName = dialog.addField(extendedTypeStr(Filenameopen), "File name");

dialog.run();

if (dialog.run())
{
filename = dialogFileName.value();

}

asciiIO = new AsciiIO(filename, 'R');

if (!asciiIO || asciiIO.status() != IO_Status::Ok )
{
throw error (strfmt("No Path Specified",filename));
}

ttsbegin;
asciiIO.inRecordDelimiter('\r\n');
asciiIO.inFieldDelimiter(',');


while (asciiIO.status() == IO_status::Ok)
{
line = asciiIO.read();
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = dialogFileName.value();
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1); //worksheet Number
cells = worksheet.cells();
do
{
row++;
_PTypeId = any2int(cells.item(row, 1).value().toString());
_PType = cells.item(row, 2).value().bStr();
_description = cells.item(row, 3).value().bStr();

PType.productTypeId = _PTypeId;
PType.productType = _PType;
PType.description = _description;
PType.insert();

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

ttscommit;
super();
ProductType_1_ds.reread();
ProductType_1_ds.refresh();
ProductType_1_ds.research(true);
}

Could anyone please provide code for data import((Header and Lines) from excel to ax 2012(Transaction Form). 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi

    You got the right idea (or at least the right code snippet) but you need to give some more information regarding the Excel sheet. 

    I find that reading from the Excel sheet is the hardest part - once you have read all your variables from the spreadsheets there are many examples on how to create a header and line transaction.

    So, traverse the speadsheet and assign values to variables and then create the header and line transaction.

  • Anand AX Profile Picture
    2,172 on at

    Hi Steffen,

    Thanks for your response. I am having vendinvoiceheader(Custom) table with invoice no, Description, Vendor no, Date..... fields etc.. and vendinvoicelines(Custom)  table with some related fields. And having two excel file separately for header and Lines. While importing I need to do some basic validations.The end user going to use this, so i need to give in a simple way like tool.

    I am new to ax and x++ coding, so i dont know, how to proceed?? Please help me with this..

  • Community Member Profile Picture
    on at

    Hi

    I think that it's going to be a fairly complex task if you have your headers in one sheet and the lines in another.

    Perhaps you could get the data that forms the relation behind the header and line table to be represented in the line table, e.g. if Invoice number as primary key in you header table, then have invoice number inserted in a new column in the line sheet, then you can have one run inserting the header tables, and a separate run inserting/updating the lines.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 21

#2
dserp Profile Picture

dserp 4

#2
dekion Profile Picture

dekion 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans