Simple Import / Export from Excel file to Ax 2012 Table

EXPORT

static void ExportToExcel(Args _args)
{
Bill_TabMethod_1 billTable;
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
SysExcelCell cell;
int row;
;

application = SysExcelApplication::construct();
workbooks = application.workbooks();
workbook = workbooks.add();
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
cells.range(‘A:A’).numberFormat(‘@’);

cell = cells.item(1,1);
cell.value(“CustName”);
cell = cells.item(1,2);
cell.value(“ItemName”);
row = 1;
while select billTable
{
row++;
cell = cells.item(row, 1);
cell.value(billTable.CustName);
cell = cells.item(row, 2);
cell.value(billTable.ItemName);
}
application.visible(true);

}

IMPORT

static void ImportFromExcel(Args _args)
{

SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row=1;
ItemId itemid;
Name name;
FileName filename;
Bill_TabMethod_1 billTable;

application = SysExcelApplication::construct();

workbooks = application.workbooks();
//specify the file path that you want to read
filename = “E:\\Johnkrish1.xls”;
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error(“File cannot be opened.”);
}

workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
row++;
name = cells.item(row, 1).value().bStr();
itemId = cells.item(row, 2).value().bStr();
//info(strfmt(‘%1 – %2’, itemId, name));

billTable.CustName=name;
billTable.ItemName=itemid;
billTable.insert();

}

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

}
while (type != COMVariantType::VT_EMPTY);

application.quit();

}

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