I am creating a dynamic Excel sheet using ExcelWorksheet. I need to create a non-editable excel. ws.Cells["A1:Q12"].Style.Locked = true is not working.
我正在使用ExcelWorksheet创建一个动态Excel表。我需要创建一个不可编辑的excel。ws.Cells .Style(A1:12”)。锁定= true不工作。
Here is my code :
这是我的代码:
Default.aspx.cs
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Server.MapPath("~/Download/Sample.xlsx");
using (ExcelPackage pck = new ExcelPackage())
{
FileInfo summaryFilePath = new FileInfo(filePath);
ExcelWorksheet ws= pck.Workbook.Worksheets.Add("Sample Page");
CreateForamters(ws);
}
}
private void CreateForamters(ExcelWorksheet ws)
{
ws.Cells["B8:L8"].Value = "SamplePage";
ws.Cells["B10:L10"].Value = DateTime.Now.ToString("MMM-yy");
ws.Cells["B11:L11"].Value = "test data........-";
ws.Cells["B8:L11"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["B8:L11"].Style.Font.Bold = true;
ws.Cells["B8:L11"].Style.Font.Name = "Arial";
ws.Cells["B8:L11"].Style.Font.Size = 16;
ws.Cells["B8:L11"].Style.Font.Color.SetColor(Color.Blue);
ws.Cells["B8:L11"].Style.Fill.BackgroundColor.SetColor(Color.White);
ws.Cells["B8:L11"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
ws.Cells["B8:L8"].Merge = true;
ws.Cells["B9:L9"].Merge = true;
ws.Cells["B10:L10"].Merge = true;
ws.Cells["B11:L11"].Merge = true;
ws.Cells["A1:Q12"].Style.Locked = true;
}
Thank you all in advance for your response.
提前感谢大家的回复。
2 个解决方案
#1
3
I am creating a dynamic Excel sheet using ExcelWorksheet. I need to create a non-editable excel. ws.Cells["A1:Q12"].Style.Locked = true is not working.
我正在使用ExcelWorksheet创建一个动态Excel表。我需要创建一个不可编辑的excel。ws.Cells .Style(A1:12”)。锁定= true不工作。
To create NON-Editable Cells, you have to use
要创建不可编辑的单元格,必须使用
ws.get_Range("A1", "Q12").Locked = true;
And then you need to protect the worksheet. Without protecting the worksheet, the .Locked
command doesn't have any significance.
然后你需要保护工作表。在不保护工作表的情况下,. locked命令没有任何意义。
Here is a basic example (TRIED AND TESTED IN VS2010 + OFFICE 2010)
下面是一个基本的示例(在VS2010 + OFFICE 2010中尝试和测试)
object misValue = System.Reflection.Missing.Value;
ws.get_Range("A1", "Q12").Locked = true;
string Password = "Sid";
ws.Protect(Password, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue);
NOTE: By default all cells in Excel are locked. If you don't want to protect the rest of the cells in the sheet then remember to set their .Locked
property to False
.
注意:默认情况下,Excel中的所有单元格都是锁定的。如果您不想保护表中的其他单元格,那么请记住将它们的. locked属性设置为False。
ws.Cells.Locked = false;
and then use the above code.
然后使用上面的代码。
#2
0
if you want to save Excel WorkBook as ReadOnly
Save as Below:
如要将Excel工作簿保存为ReadOnly,请保存如下:
object misValue = System.Reflection.Missing.Value;
ExcelWorkBook.ActiveWorkbook.SaveAs(save_path, Excel.XlFileFormat.xlWorkbookNormal, misValue , misValue , True, True,XlSaveAsAccessMode.xlShared, false, false, misValue, misValue , misValue );
#1
3
I am creating a dynamic Excel sheet using ExcelWorksheet. I need to create a non-editable excel. ws.Cells["A1:Q12"].Style.Locked = true is not working.
我正在使用ExcelWorksheet创建一个动态Excel表。我需要创建一个不可编辑的excel。ws.Cells .Style(A1:12”)。锁定= true不工作。
To create NON-Editable Cells, you have to use
要创建不可编辑的单元格,必须使用
ws.get_Range("A1", "Q12").Locked = true;
And then you need to protect the worksheet. Without protecting the worksheet, the .Locked
command doesn't have any significance.
然后你需要保护工作表。在不保护工作表的情况下,. locked命令没有任何意义。
Here is a basic example (TRIED AND TESTED IN VS2010 + OFFICE 2010)
下面是一个基本的示例(在VS2010 + OFFICE 2010中尝试和测试)
object misValue = System.Reflection.Missing.Value;
ws.get_Range("A1", "Q12").Locked = true;
string Password = "Sid";
ws.Protect(Password, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue);
NOTE: By default all cells in Excel are locked. If you don't want to protect the rest of the cells in the sheet then remember to set their .Locked
property to False
.
注意:默认情况下,Excel中的所有单元格都是锁定的。如果您不想保护表中的其他单元格,那么请记住将它们的. locked属性设置为False。
ws.Cells.Locked = false;
and then use the above code.
然后使用上面的代码。
#2
0
if you want to save Excel WorkBook as ReadOnly
Save as Below:
如要将Excel工作簿保存为ReadOnly,请保存如下:
object misValue = System.Reflection.Missing.Value;
ExcelWorkBook.ActiveWorkbook.SaveAs(save_path, Excel.XlFileFormat.xlWorkbookNormal, misValue , misValue , True, True,XlSaveAsAccessMode.xlShared, false, false, misValue, misValue , misValue );