Hi guys ,Today we see How to Import data from Excel file to Ax 2012 using X++.
Write below code in job and Test it.
static void Job15(Args _args)
{
Dialog _dialog;
DialogField _file;
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
FileName filename;
CustGroup custgroup; //Declaring Table Name
CustGroupId _CustGroup;
Name _Name;
str _clearingPeriod;
int row = 1;
_dialog = new Dialog("Import Data From Excel");
_dialog.addText("Select file:");
_file = _dialog.addField(ExtendedTypeStr("FilenameOpen"));
_dialog.run();
if (_dialog.closedOK())
{
info(_file.value());
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename =_file.value(); //ExcelSheet File Name
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++;
_CustGroup = cells.item(row, 1).value().bStr();
_Name = cells.item(row, 2).value().bStr();
_ClearingPeriod = cells.item(row, 3).value().bStr();
select forUpdate custgroup where custgroup.CustGroup==_CustGroup;
if (custgroup)
{
ttsBegin;
custgroup.Name = _Name ;
custgroup.ClearingPeriod = _clearingPeriod;
custgroup.update();
ttsCommit;
}
else
{
custgroup.Name = _Name ;
custgroup.ClearingPeriod = _clearingPeriod;
custgroup.insert();
}
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
info("Data is Imported");
}
Keep Daxing!!
No comments:
Post a Comment