Sunday, September 23, 2018

Import/Export template excel in D365FO X++ Sample

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;

class VendInvoiceXLUploadMatch
{
    public static void main(Args _args)
    {
        VendInvoiceInfoTable        localVendInvoiceInfoTable;
        VendInvoiceXLUploadMatch  Upload = new VendInvoiceXLUploadMatch();
        localVendInvoiceInfoTable = _args.record();

if(_args.parmEnum() == NoYes::Yes)
        {
            Upload.export();
        }
        else
        {
            Upload.import(localVendInvoiceInfoTable);
        }
    }

public void export()
    {
        MemoryStream memoryStream = new MemoryStream();
        using (var package = new ExcelPackage(memoryStream))
        {
            var currentRow = 1;
            var worksheets = package.get_Workbook().get_Worksheets();
            var CustTableWorksheet = worksheets.Add("Export");
            var cells = CustTableWorksheet.get_Cells();
            OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
            System.String value = "Item number";
            cell.set_Value(value);
            cell = null;
            value = "Sales order Id";
            cell = cells.get_Item(currentRow, 2);
            cell.set_Value(value);
            value = "Serial Number";
            cell = cells.get_Item(currentRow, 3);
            cell.set_Value(value);

            package.Save();
            file::SendFileToUser(memoryStream, strFmt('SeriaLog_%1.xlsx',curUserId()));
        }
    }

    public void import(VendInvoiceInfoTable        _localVendInvoiceInfoTable)
    {
        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++)
                    {
                       //range.get_Item(i, 1).value;
                    }
                }
            }
            else
            {
                error("Error here");
            }
        }
    }
}

No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...