Hello,
We have been using Aspose products in our different applications/websites. Now we have requirement of making particular whole workbook readonly. we need to apply only read only setting. we don’t want to make it password protected. We are creating excel workbook from stream object. Is there any read only property available for workbook which we can set it to true? please find following code which we use for creating workbook.
byte[] byteArray = excelFile.OpenBinary();
MemoryStream stream = new MemoryStream(byteArray);
Workbook excel = new Workbook(stream);
Now is there any property available as follows?
excel.IsReadOnly = ?
how can we make whole workbook readonly? Can anyone help me in this?
Thanks & Regards
Ashish Rajguru
Hi,
Well, you may try to use WriteProtection options for your needs. See the following sample code segment for your reference:
e.g
Sample code:
excel.Settings.WriteProtection.Password = “007”;
// excel.Settings.WriteProtection.RecommendReadOnly = true;
excel.Save(“e:\test2\out1.xlsx”, SaveFormat.Xlsx);
Now when you open the file into MS Excel, either the user will provide the write password or he has to open the file in Read-only mode, so he cannot save the changes or file contents of the original file.
Hope, this helps a bit.
Thank you.
Hi,
Well, yes, you need to download and install our latest version/fix: v8.7.1.x to execute my code segment. And, if you need to stick with your older version, you may try the following lines of code:
e.g
Sample code:
excel.Settings.IsWriteProtected = true;
excel.Settings.WriteProtectedPassword = “007”;
Hope, this helps a bit.
Thank you.
Hello.
Now I am getting both above properties in my version of dll. i am able to set excel.Settings.IsWriteProtected = true; in the code. but it is not making that excel file as readonly. i don’t want to make that excel file as password protected. i only want to make it readonly so when user opens it, it will be only in readonly format and he can’t save. it should not ask password prompt.
user should be able to open that file without asking him password prompt but it should default open in readonly format.
Can you help me how to make it only readonly?
will only excel.Settings.IsWriteProtected = true not work?
i don’t want to use excel.Settings.WriteProtectedPassword = "007"
Thanks & Regards
Ashish Rajguru
Hello,
Thanks it is working according to what we needed. now we have additional requirement that user should not able to add new sheet or delete existing sheet. Can you help me? how can we protect that part as well?
foreach (Worksheet sheet in excel.Worksheets)
{
sheet.Protect(ProtectionType.All);
}
excel.Save(“e:\test2\out1.xlsx”);
I applied above code and all the sheet became readonly/protected. but now we need that user should not be able to add new sheet or delete existing sheet. and if we can achieve it without asking password prompt then it would be great help.
Thanks & Regards
Ashish Rajguru
Hello Babar raza,
I have applied your code of protecting whole workbook instead of sheets but it seems not working. i have applied following lines of code in my project.
MemoryStream stream = new MemoryStream(byteArray);
Workbook excel = new Workbook(stream);
excel.Protect(Aspose.Cells.ProtectionType.All, null);
excel.Save(“C:\ExcelReadonly.Xlsx”, Aspose.Cells.SaveFormat.Xlsx);
And Output file not seems proper. Actual excel file has total 3 sheets but in output file i can see only 1 sheet and it is got minimized. Not able to see all the columns. It is not readonly/protected. it is not giving prompt while i try to modify it. I can modify it and can save it at my end. so making whole workbook protected not working. Please find attached output file which it generated after applying above changes.
Thanks & Regards
Ashish Rajguru
Hi Ashish,
First of all, please note that protecting the workbook will not make the cells of any worksheet read-only. You have to protect the desired worksheet separately as suggested earlier in this thread. Regarding your other concerns (missing/minimized worksheets/columns), please share your input spreadsheet for further investigation. Thank you for your cooperation.
Hi,
Well, you should try to accomplish the task via both using worksheet protection and workbook protection. Also, it might not be possible to accomplish your exact requirements even in MS Excel. Anyways, please see the updated sample code segment below for your reference, it might suit your needs to certain extent:
e.g
Sample code:
…
foreach (Worksheet sheet in excel.Worksheets)
{
sheet.Protect(ProtectionType.All);
sheet.Protection.Password = “test”;
}
excel.Protect(ProtectionType.Contents, null);
excel.Settings.WindowTop = 0;
excel.Settings.WindowLeft = 0;
excel.Settings.WindowWidth = 23475 / 18.0;
excel.Settings.WindowHeight = 10080 / 18.0;
excel.Save(“e:\test2\outBook1.xlsx”);
If you still think that you may accomplish your exact task in MS Excel manually, please do it in MS Excel manually and save an Excel file to provide us here, we will check on how to do it via Aspose.Cells APIs.
Thank you.
Hello,
I still can’t see all my sheets exist in workbook after applying your latest code which you suggested. I can see only one sheet in workbook and there are total 2 sheets in input sheet. I have applied following code.
foreach (Worksheet sheet in excel.Worksheets)
{
sheet.Protect(Aspose.Cells.ProtectionType.All);
sheet.Protection.Password = “test”;
}
excel.Protect(Aspose.Cells.ProtectionType.All, null);
excel.Settings.WindowTop = 0;
excel.Settings.WindowLeft = 0;
excel.Settings.WindowWidth = 23475 / 18.0;
excel.Settings.WindowHeight = 10080 / 18.0;
excel.Save(“C:\ExcelReadonly.Xlsx”, Aspose.Cells.SaveFormat.Xlsx);
Please find attached input.xlsx and ExcelReadonly.xlsx(output) files.
Thanks & Regards
Ashish Rajguru
Hi,
Thanks for the template files.
The example code I provided earlier is just for learning purpose and is generic. Please try to update the lines of code accordingly:
e.g
excel.Settings.WindowWidth = 23475 / 18.0;
excel.Settings.WindowHeight = 10080 / 18.0;
to:
excel.Settings.WindowWidth = 23475 / 23.0;
excel.Settings.WindowHeight = 10080 / 23.0;
This is again just an example and you can update it in accordance with your output device with specified resolutions etc. You may try changing the values of NUMERATOR and DENOMINATOR in the given fractions.
Thank you.