我已使用密码锁定了excel单元格并生成了excel文件,但工作表的名称仍然可以编辑。我怎样才能使它不可编辑?请帮忙。
格式代码:
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());
这只能锁定单元格而不是工作表名称。即使设置密码doest似乎也会锁定工作表名称。你可以改变名称而不做任何事情。
答案 0 :(得分:2)
您还需要保护工作簿:
package.Workbook.Protection.LockStructure = true;
package.Workbook.Protection.SetPassword("password");
通过这种方式,工作簿结构也受到保护,包括工作表名称,但它对所有工作表都有效。