0

Is there a way to produce a workbook using NPOI that has doesn't allow user to edit the workbook unless saving a new copy?

Basically I want to produce a file that is a read-only excel, and that no users will have conflict when opening them when accessing them on a shared network drive.

P/S: I am not looking for password protection.

var wb = new XSSFWorkbook();
var sheet = wb.CreateSheet("Sheet1");
sheet.CreateRow(0).CreateCell(0).SetCellValue("Hello World");

wb.SetReadOnly(true);//Something like this?
Liam
  • 27,717
  • 28
  • 128
  • 190
CozyAzure
  • 8,280
  • 7
  • 34
  • 52
  • NPOI or EPPlus can only do what Excel already does and use more or less the same objests, methods. Are you asking about cell protection? Making sheets readonly? Which menu/dialog/option would you use to do what you want in Excel? The NPOI/EPPlus objects will have similar names. If you can'f find them this way, record a macro while setting the protection and check the generated code. The objects/properties should exist in Epplus, NPOI as well – Panagiotis Kanavos Oct 01 '18 at 08:32
  • BTW if you want a *shared file* to be read-only, you should modify the file's/folder's sharing permissions. You can set that `Everyone` has only read permission just once and 1-2 admin/editor accounts have full control – Panagiotis Kanavos Oct 01 '18 at 08:36

2 Answers2

1

Readonly is an attribute for file

File.SetAttributes("workbook.xlsx", FileAttributes.ReadOnly);
shingo
  • 18,436
  • 5
  • 23
  • 42
0

You don't need to that with NPOI library

to set security permission for all files in a directory

var directory = new DirectoryInfo(folderPath);
foreach (var file in di.GetFiles("*", SearchOption.AllDirectories))
{
 File.SetAttributes(folderPath, FileAttributes.ReadOnly);
}

or for a specific file

File.SetAttributes(folderPath, FileAttributes.ReadOnly);
Idriss Benbassou
  • 1,481
  • 2
  • 14
  • 23