Hi,
I'm in the process of evaluating Aspose.Cells. I've written some code (based on examples on your website) to get some cells to be read-only, and it is not working. I've pasted the code below. Can you tell me what's wrong with the code? Any help would be appreciated it. Thanks!
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wb As Aspose.Cells.Workbook = New Aspose.Cells.Workbook()
Dim sheet As Aspose.Cells.Worksheet = wb.Worksheets(0)
Dim style As Aspose.Cells.Style
'Dim styleflag As Aspose.Cells.StyleFlag
Dim i As Integer
For i = 0 To 255
style = sheet.Cells.Columns(Convert.ToByte(i)).Style
style.IsLocked = False
Next
style = sheet.Cells("A1").Style
style.IsLocked = True
style = sheet.Cells("B1").Style
style.IsLocked = True
style = sheet.Cells("C1").Style
style.IsLocked = True
Dim protection As Aspose.Cells.Protection = sheet.Protection
wb.Save("d:\LockedMyBook.xls", Aspose.Cells.FileFormatType.Excel2003)
End Sub
Tran Trieu
This message was posted using Email2Forum by Laurence.
Hi Tran,
Thanks for your inquiry.
Well, actually the code would just work fine if you are using some older versions before 4.4.2. But if you are using new versions of Aspose.Cells for .NET, for your info, we have incorporated / enhanced the API for some performance reasons. In the new versions (e.g.., 4.4.2, 4.4.3, 4.5 etc.), the Column.Style is read-only property (for performance sake only), so, it is recommended here that you should use either Cells.ApplyColumnStyle() or Column.ApplyStyle() methods. Also in the end, always use Worksheet.Protect() method instead of Worksheet.Protection.
So, I modify your code accordingly and it would work fine.
Sample code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wb As Aspose.Cells.Workbook = New Aspose.Cells.Workbook()
Dim sheet As Aspose.Cells.Worksheet = wb.Worksheets(0)
Dim style As Aspose.Cells.Style
Dim styleflag As Aspose.Cells.StyleFlag
Dim i As Integer
For i = 0 To 255
style = sheet.Cells.Columns(Convert.ToByte(i)).Style
style.IsLocked = False
styleflag = New StyleFlag()
styleflag.Locked = True
sheet.Cells.Columns(Convert.ToByte(i)).ApplyStyle(style, styleflag)
Next
style = sheet.Cells("A1").Style
style.IsLocked = True
style = sheet.Cells("B1").Style
style.IsLocked = True
style = sheet.Cells("C1").Style
style.IsLocked = True
sheet.Protect(ProtectionType.All)
wb.Save("d:\LockedMyBook.xls", Aspose.Cells.FileFormatType.Excel2003)
End Sub
And we will add/update the related code in the documentation soon.
Thank you.
I'm new and evaluating Aspose Cells for my company. However, I'm having an issue with locking 1 and only 1 cell. I have the code set to:
sheet.Cells[rowindex, 0].Style.IsLocked = true;
and this sets the locked attribute in Excel. However, its still editable. So then I went looking for the sheet.Protect, sheet.Protection and everything I tried seems to either lock nothing, or lock everything. I'm looking for just 1 cell right now.
Help?
Hi,
Please see the document (especially the sub-heading titled "Protect a few Cells in the Worksheet ") for your reference if it fulfils your need.
Protect and Unprotect Worksheet
Thank you.
ah, I see the demo did work, is the key that you have to explicitly set isLocked to false for everything first?
Hi,
Yes, for your info, all the cells in the worksheet are implicitly locked in MS Excel (you may check it manually in MS Excel), so you you have to unlock all the cells first, specify IsLocked for your desired cell or cells, finally protect the sheet.
Thank you.