简体   繁体   中英

c# using EPPLUS make name of the sheet read only?

I have locked the excel cells with a password and generated the excel file but the name of the sheet is still editable. How do I make it uneditable? Please help.

the formatting code :

      string schoolHeader = "A2:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "2";
        string addressHeader = "A3:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "3";
        string classHeader = "A5:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "5";
        string subjectHeader = "A7:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "7";
        string examHeader = "A8:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "8";
        string headerRange = "A10:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "10";

        string rangeupdate = (StudentCount + 11).ToString();
        string unprotectedRange = "F11:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + rangeupdate;
        string EntryRange = "A11:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + rangeupdate;

        subject.Cells[schoolHeader].Merge = true;
        subject.Cells[addressHeader].Merge = true;
        subject.Cells[classHeader].Merge = true;
        subject.Cells[subjectHeader].Merge = true;
        subject.Cells[examHeader].Merge = true;

        subject.Cells[schoolHeader].Value = Header;
        subject.Cells[schoolHeader].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        subject.Cells[schoolHeader].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        subject.Cells[schoolHeader].Style.Font.Bold = true;
        subject.Cells[schoolHeader].Style.Font.Size = 16;

        subject.Cells[addressHeader].Value = place;
        subject.Cells[addressHeader].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        subject.Cells[addressHeader].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        subject.Cells[addressHeader].Style.Font.Bold = true;
        subject.Cells[addressHeader].Style.Font.Size = 11;

        subject.Cells[classHeader].Value = name;
        subject.Cells[classHeader].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        subject.Cells[classHeader].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        subject.Cells[classHeader].Style.Font.Bold = true;

        subject.Cells[subjectHeader].Value = subject;
        subject.Cells[subjectHeader].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        subject.Cells[subjectHeader].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        subject.Cells[subjectHeader].Style.Font.Bold = true;
        subject.Cells[subjectHeader].Style.Font.Size = 12;

        subject.Cells[examHeader].Value = date;
        subject.Cells[examHeader].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        subject.Cells[examHeader].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
        subject.Cells[examHeader].Style.Font.Bold = true;

        subject.Cells[headerRange].LoadFromArrays(headerRow);

        subject.Cells[headerRange].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
        subject.Cells[headerRange].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Left;
        subject.Cells[headerRange].Style.Font.Bold = true;
        subject.Cells[headerRange].Style.Font.Size = 12;
        subject.Cells[headerRange].Style.Font.Color.SetColor(System.Drawing.Color.White);
        subject.Cells[headerRange].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
        subject.Cells[headerRange].Style.Fill.BackgroundColor.SetColor((System.Drawing.Color.MediumPurple));

        subject.Cells[headerRange].Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        subject.Cells[headerRange].Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        subject.Cells[headerRange].Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
        subject.Cells[headerRange].Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;

        subject.Cells[EntryRange].Style.Font.Bold = true;
        subject.Cells[EntryRange].Style.Font.Size = 12;

        subject.Cells[subject.Dimension.Address].AutoFitColumns();
        subject.Cells[headerRange].Style.Locked = true;
        subject.Cells[unprotectedRange].Style.Locked = false;
        subject.Protection.IsProtected = true;
        subject.Protection.AllowFormatColumns = true;
        subject.Protection.SetPassword(pass.ToString());

This can only lock the cells not the sheet name. Even setting the password doest seem to lock the sheet name. You can just change the name and it doesnt do anything.

You also need to protect the workbook:

package.Workbook.Protection.LockStructure = true;
package.Workbook.Protection.SetPassword("password");

In this way also the workbook structure is protected, included the sheet names, but it will be valid for all sheets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM