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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

how to lock certain excel cells or make them read-only using X++?

(0) ShareShare
ReportReport
Posted on by

Hi All,

I want to lock some of excel cells (not all) using X in my Export to Excel utility class.
When someone opens the excel file, they will be able to edit any cell except those cells. And if someone wishes to delete all editable data then read-only cells will retain their value and other editable cells will lose their value. Because the same file I have to use for import and these fields will used to identify unique values from AX.

For reference a part of code is attached below 

 

 using (var package = new OfficeOpenXml.ExcelPackage(memoryStream))
            {                 
                var currentRow = 10;
                var worksheets = package.get_Workbook().get_Worksheets();
                var worksheet  = worksheets.Add("@ApplicationFoundation:DMFExportOperation");
                var cells      = worksheet.get_Cells();
               

                //<---- Start---> Setup Header creation
                //Want to make lock below field 
                cell=null;
                value="XYZ";
                cell=cells.get_Item(4,2);
                cell.set_Value(value);
                this.setCellBold(cell);

                
            }

Thanks in Advance!!!

Regards,

Aimon Fatima

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi Aimon,

    What is the real business requirement for blocking the cells? Are you afraid that users are not familiar with Excel and mess up the sheet before you can import it?

    Probably, you can get an answer on an Excel/Office forum how to achieve locking fields using the Open Office XML interface.

    techcommunity.microsoft.com/.../Excel_Cat

  • Community Member Profile Picture
    on at

    Yes, it's a validation that user will not be able to change values in some cells.

    So I need a code to write in my class

  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi Aimon,

    Did you ask your question on the Excel forum? There might be volunteers more familiar with all coding options.

  • Community Member Profile Picture
    on at

    Not yet. Will I get reference for coding in X++ ??  

  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    You can expect answers how to use the OfficeOpenXML dll. Which methods and how to use them. Then it doesn't matter if the methods will be used in x++ or a C# project.

    As you noticed, there is not much traction on this community for this topic. Your requirement is not common in the context of Dynamics 365. So, I think you will get quicker and better answers on an Excel forum.

  • Suggested answer
    Pete Alberts Profile Picture
    3,542 on at

    Hi Aimon

    You wouldn't have this issue if you were using the MS Open in Excel feature. Writing custom export and import to excel features are old. Maybe there are still cases where it is needed. But I highly doubt it.

    And then which ever method you choose I suggest you stay with validations. On import check that the values that shouldn't have been edited indeed have not been edited. If this is enforced on table level (which should be the case), a data entity will do that for you without any extra work. I am sure it is possible to accomplish non-editable excel fields, but it won't be straight forward and it will take a lot of your time.

    All of the best.

  • Suggested answer
    Rahul Mohta Profile Picture
    21,032 on at

    check layout in budget planning and how it uses Excel to make fields editable and read only

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans