How to add Sensitivity Label to an Excel file

Given the workbook object. How do I add the Sensitivity Label in C#?

@BRGQST

To add a Sensitivity Label to an Excel file using Aspose.Cells in C#, you can utilize the Workbook object. While the specific implementation details are not provided in the context, you can refer to Microsoft’s documentation for guidance on applying sensitivity labels to files.

Here’s a general approach you can follow:

  1. Create or Load the Workbook: Start by creating a new workbook or loading an existing one.
  2. Set the Sensitivity Label: Use the appropriate method to set the sensitivity label on the workbook.

Here is a sample code snippet to illustrate how you might implement this:

using Aspose.Cells;

class Program
{
    static void Main()
    {
        // Load an existing workbook
        Workbook workbook = new Workbook("path/to/your/excel/file.xlsx");

        // Set the sensitivity label
        // Note: Replace "YourSensitivityLabel" with the actual label you want to apply
        workbook.SetSensitivityLabel("YourSensitivityLabel");

        // Save the workbook
        workbook.Save("path/to/your/modified/excel/file.xlsx");
    }
}

Make sure to replace "YourSensitivityLabel" with the actual label you wish to apply. For more detailed information, you may want to check the Microsoft documentation on applying sensitivity labels to files, which can provide additional context and examples.

If you need further assistance or specific examples, feel free to ask!

I don’t think this is correct?

@BRGQST
Please refer to the following example code.

// Load an existing workbook
Workbook workbook = new Workbook(filePath + "sample.xlsx");
workbook.Worksheets.SensitivityLabels.Add("id", true, SensitivityLabelAssignmentType.Privileged, "siteid", SensitivityLabelMarkType.Watermark);
            
// Save the workbook
workbook.Save(filePath + "out_net.xlsx");

Regarding the relevant prerequisites for sensitive labels, please refer to the following document.

Hope helps a bit.

1 Like

John,

Got it. Thank you. What about these actions:

  • Remove the Sensitivity Label?
  • Query whether a Sensitivity Label exists?

@BRGQST,

You may easily evaluate your tasks in code. See the following sample lines of code for your reference.
1). Remove the Sensitivity Label

//Remove the first Sensitivity Label
workbook.Worksheets.SensitivityLabels.RemoveAt(0);
//Remove all  Sensitivity Labels in the workbook
workbook.Worksheets.SensitivityLabels.Clear();

2). Query whether a Sensitivity Label exists

//Check if a Sensitivity Label exists or not. If Count value is >0, it exists otherwise not.
int cnt = workbook.Worksheets.SensitivityLabels.Count;

Hope, this helps a bit.