web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Reading and Updating a...
Finance forum

Reading and Updating a Excel file using X++ code

(0) ShareShare
ReportReport
Posted on by

Hi, I need to upload a excel file using dialog and i have to read that excel file. After reading, i have to insert some values into that excel file and download that updated excel in some other name. I am able to read my excel but couldn't modify it,i.e i couldn't insert any new values into the sheet. Anybody help me out.This is my code, 

 

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
class Myclassnew
{        
  public static void main(Args _args)
    {
         CustTable custTable;

         MemoryStream            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);

        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))

                {
                    var currentRow=1;
                    Package.Load(stream);
                    ExcelWorksheet  Worksheets = Package.get_Workbook().get_Worksheets().get_Item(1);
                    OfficeOpenXml.ExcelRange cell = Worksheets.Cells;
                    cell.get_Item(currentRow, 1).Set_Value("Account Number");
                    cell.get_Item(currentRow, 2).Set_Value("Currency");
                     while select custTable
                    {
                        currentRow ++;
                  
                        cell = cell.get_Item(currentRow, 1).Set_Value(CustTable.AccountNum);
                   
                        cell = cell.get_Item(currentRow, 2).Set_Value(CustTable.Currency);
                    
                    }
                package.Save();
                file::SendFileToUser(memoryStream,'downloadedsheet.xlsx');
           }
       }
   }
}
           

  • Suggested answer
    Hemang Panchal Profile Picture
    2 on at
    Hi, 
    Replying to an old post. I have similar requirement where a file should be downloaded from BLOB storage and it should be modified by logic in x++ and then save it for user with modified content.

    I refered your code and replaced the below code part with package.SaveAs() function. This will store the modified content in different stream and then save it for user with modified content.
     
    Package.Load(stream);
    ....
    ....
    ....
    package.Save();
    file::SendFileToUser(memoryStream,'downloadedsheet.xlsx');

    Package.Load(stream);
    ....
    ....
    ....
    ....
    var outStream = new MemoryStream();
    Package.SaveAs(outStream);
    file::SendFileToUser(outStream,'downloadedsheet.xlsx');
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans