如何使用C#.net创建只读Excel表 [英] How to create readonly Excel sheet using C# .net
问题描述
是我的代码:
Default.aspx.cs
protected void Page_Load(object sender,EventArgs e)
{
string filePath = Server.MapPath(〜/ Download / Sample.xlsx);
使用(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]。值=测试数据........-;
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;
}
感谢您提前回复。
我正在使用ExcelWorksheet创建一个动态Excel表。我需要创建一个不可编辑的excel。 ws.Cells [A1:Q12]。Style.Locked = true不起作用。
要创建非可编辑单元格,你必须使用
ws.get_Range(A1,Q12)Locked = true;
然后您需要保护工作表。不保护工作表, .Locked
命令没有任何意义。
这是一个基本的例子
对象misValue = System.Reflection.Missing.Value在VS2010 + OFFICE 2010中检查和测试 ;
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);
注意:默认情况下,Excel中的所有单元格都被锁定。如果您不想保护表单中的其余单元格,请记住将 .Locked
属性设置为 False
。
ws.Cells.Locked = false;
然后使用上述代码。
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.
Here is my code :
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.
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.
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.
Here is a basic example (TRIED AND TESTED IN 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
.
ws.Cells.Locked = false;
and then use the above code.
这篇关于如何使用C#.net创建只读Excel表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!