Below is the code snippet which will be handy to read the data from excel using code in D365/ax 7
Please note that, in D365 SysExcel* classes have been deprecated.
Under your references node in solution explorer, add Microsoft.Office.InterOp.Excel reference
Use OfficeOpenXML namespace to achieve this. Improvise the below snippet based on your need.
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;
class SRReadFromExcel_D365
{
public static void main(Args _args)
{
System.IO.Stream stream;
ExcelSpreadsheetName sheeet;
FileUploadBuild fileUpload;
DialogGroup dlgUploadGroup;
FileUploadBuild fileUploadBuild;
FormBuildControl formBuildControl;
Dialog dialog = new Dialog(“Import the data from Excel”);
dlgUploadGroup = dialog.addGroup(“@SYS54759”);
formBuildControl = dialog.formBuildDesign().control(dlgUploadGroup.name());
fileUploadBuild = formBuildControl.addControlEx(classstr(FileUpload), ‘Upload’);
fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
fileUploadBuild.fileTypesAccepted(‘.xlsx’);
if (dialog.run() && dialog.closedOk())
{
FileUpload fileUploadControl = dialog.formRun().control(dialog.formRun().controlId(‘Upload’));
FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
if (fileUploadResult != null && fileUploadResult.getUploadStatus())
{
stream = fileUploadResult.openResult();
using (ExcelPackage Package = new ExcelPackage(stream))
{
int rowCount, i;
Package.Load(stream);
ExcelWorksheet worksheet = package.get_Workbook().get_Worksheets().get_Item(1);
OfficeOpenXml.ExcelRange range = worksheet.Cells;
rowCount = worksheet.Dimension.End.Row – worksheet.Dimension.Start.Row + 1;
for (i = 2; i<= rowCount; i++)
{
info(range.get_Item(i, 1).value);
info(range.get_Item(i, 2).value);
}
}
}
else
{
error(“Error here”);
}
}
}
}
Your support has been invaluable to us. Thank you for being there!
ReplyDeleteYou can read data from Excel in D365 F&O / AX 7 using DomaiNesia with the OfficeOpenXmlPackage class and its methods to load and process the Excel file.
ReplyDelete