How to Import Records from Excel Using X++ Code in D365FO
How to Import Records from Excel Using X++ Code in D365FO
Include the below namespace in X++
code it will requires to import Excel.
Using System.IO;
Using OfficeOpenXml;
Using OfficeOpenXml.ExcelPackage;
Using OfficeOpenXml.ExcelRange;
In addition to the basic reference, include
(Directory and DirectoryUpgrade Packages)
X++ Code:
Using System.IO;
Using OfficeOpenXml;
Using OfficeOpenXml.ExcelPackage;
Using OfficeOpenXml.ExcelRange;
class ReadExcel
{
public static void
main(Args _args)
{
Int id;
Str Name;
System.IO.Stream
stream;
ExcelSpreadsheetName sheet;
FileUploadBuild
fileUpload,fileUploadBuild;
DialogGroup
dialogUploadGroup;
FormBuildControl
formBuildControl;
Dialog dialog=new
Dialog("Excel Import using dialog"); dialogUploadGroup=dialog.addGroup("@SYS54759");
formBuildControl=dialog.formBuildDesign().control(dialogUploadGroup.name());
fileUploadBuild=formBuildControl.addControlEx(classStr(fileUpload),"UploadExcelFile");
fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
fileUploadBuild.fileTypesAccepted(".xlsx");
if(dialog.run()
&& dialog.closedOk())
{
FileUpload
fileUploadControl=dialog.formRun().control(dialog.formRun().controlId("Upload"));
FileUploadTemporaryStorageResult
fileUploadResult=file::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy));
if(fileUploadResult!= null &&
fileUploadResult.getUploadStatus())
{
stream=fileUploadResult.openResult();
using(ExcelPackage package= new
ExcelPackage(stream)) {
int rowCount, iterator;
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(iterator=2;iterator<=rowCount;iterator++)
{
Id = str2Int(range.get_Item(iterator,1).Value);
Name = range.get_Item(iterator,2).Value;
}
}
}
}
}
}
After build and run the
project, a browser will open with dialogue window to import the excel file.
You can import the excel file
by clicking on the import button,
Comments
Post a Comment