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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

Modify Cells in Excel

(0) ShareShare
ReportReport
Posted on by 9,167 Super User 2026 Season 1

Hi All,

I'd like to use X++ code to update an uploaded excel file before importing it using data entities; any suggestions?

I found code that updates an XML field before import; can I do it for Excel?

 

Regards,

Waed

I have the same question (0)
  • Martin Dráb Profile Picture
    239,738 Most Valuable Professional on at

    You can do it with OfficeOpenXml.ExcelPackage. Here is an example as a starting point: Import data from Excel using X++.

  • Waed Ayyad Profile Picture
    9,167 Super User 2026 Season 1 on at

    Hi goshoom,

    I tried it; I tried to save the modified Excel sheet in the memory stream, but the memory stream does not have any new modifications of cells in Excel, although I tried to debug the cell and it had a new value.

    Regards,

    Waed

  • Martin Dráb Profile Picture
    239,738 Most Valuable Professional on at

    Finding a bug in code is not easy without seeing the code...

    Did you use SaveAs() method of ExcelPackage class?

  • Waed Ayyad Profile Picture
    9,167 Super User 2026 Season 1 on at

      //Start Excel

           System.IO.MemoryStream outputStream;

           OfficeOpenXml.ExcelPackage package = new OfficeOpenXml.ExcelPackage(fileStream);

           package.Load(fileStream);

           ExcelWorksheet worksheet         = package.get_workbook().get_worksheets().get_Item(1);

           OfficeOpenXml.ExcelRange range   = worksheet.Cells;

           int rowCount, iterator;

           rowCount = worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row + 1;

           var cells = worksheet.get_Cells();

           for(iterator = 2; iterator<=rowCount ; iterator++)

           {

               var cell = cells.get_Item(iterator,1);

               cell.set_Value(DateTimeUtil::applyTimeZoneOffset(uploadDateTime,

               DateTimeUtil::getUserPreferredTimeZone()));  

           }

           outputStream = new System.IO.MemoryStream();

           fileStream.Position = 0;

           fileStream.CopyTo(outputStream);

           outputStream.Seek(0, System.IO.SeekOrigin::Begin);

    goshoom 

  • Martin Dráb Profile Picture
    239,738 Most Valuable Professional on at

    Please try the suggestion I gave you in my previous reply.

  • Waed Ayyad Profile Picture
    9,167 Super User 2026 Season 1 on at

    Hi goshoom

    An exception was thrown :

    at Microsoft.Dynamics.Ax.Xpp.AxShared.utcdatetime.ToString(IFormatProvider provider)

      at OfficeOpenXml.ExcelWorksheet.UpdateRowCellData(StreamWriter sw)

      at OfficeOpenXml.ExcelWorksheet.SaveXml(Stream stream)

      at OfficeOpenXml.Packaging.ZipPackagePart.WriteZip(ZipOutputStream os)

      at OfficeOpenXml.Packaging.ZipPackage.Save(Stream stream)

      at OfficeOpenXml.ExcelPackage.Save()

      at OfficeOpenXml.ExcelPackage.SaveAs(Stream OutputStream)

  • Martin Dráb Profile Picture
    239,738 Most Valuable Professional on at

    This is just a stack trace; it should neither the error message or the exception type (which are two most important pieces of infomation).

    But it seems that there is a problem with your datetime value. You may want to convert it to string before putting it into the file.

    Assuming that this is a parallel discussion about the same topic as in IntergrationBeginner's thread and you're importing the data through a data entity, can't you simply populate the value in the data entity?

  • Waed Ayyad Profile Picture
    9,167 Super User 2026 Season 1 on at

    Hi goshoom

    I want to get the datetime field once I upload the Excel field to file upload control  and also, this field is mandatory and part of the key in the data entity, so it should be unique for each row.

    Regards,

    Waed

  • Martin Dráb Profile Picture
    239,738 Most Valuable Professional on at

    Then your code doesn't meet your requirements, because time likely won't be unique. Definitely it doens't have to be unique.

  • Waed Ayyad Profile Picture
    9,167 Super User 2026 Season 1 on at

    Hi goshoom

    This requirement already exist in the standards but using XML "Composite data Entity".

    I have question regarding modify datetime field in Excel from X++ code should I set its value to string or datetime because it give me  the above exception.

    Regards,

    Waed

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 566 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 504 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 429

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans