Hurdler
Hurdler

Reputation: 891

Access rights to an Excel file created with Java

I’m creating an Excel file and it should be read-only. To do this I use: (sheetName).getSettings().setProtected(true); for every new sheet added.

Only someone with admin rights can get full access to the file, so after confirming a user has admin rights I do: (workbookName).getSheet(0).getSettings().setProtected(false);

After someone with admin rights opens the file, it is not read-only anymore, until an ordinary user adds some more content to it – .getSettings().setProtected(true); again. In other words, someone can change the file after admin accesses it and before non-admin does so. I don’t have a clue how to go around it, maybe you could help me out?

--EDIT--
Users are adding data to the excel file, filling cells with information file - read workbook, copy, add some info, write and close workbook. Non-admin users can open it directly to see if what they added is there, or to see what other users added, but they should NOT be able to change anything in the file.

It is a wizard-like app that asks users for data and then adds cells to the file. One of the methods for adding data is here. The file gathers personal information from the users. The app is used only to add data, not to access it. Users can access the file only directly by double-clicking the output, and only to view the data. Admins can change the file, other users should not be able to do so.

Upvotes: 4

Views: 2756

Answers (2)

Thihara
Thihara

Reputation: 6969

Here's the thing since you told that users can simply edit it when and administrator removes protection there seem to be no standard alternative. However if this file is in use then no one else can modify it outside right? So simply just protect it again when administer exit the excel file, and remove protection when an administrator logs in. Like Tim said. You can create separate functions to edit the excel file and then do

disable protection write modifications enable protection

in all those functions. If this is feasible you can use something like AspectJ to simplify the disable protection and enable protection parts;

Upvotes: 0

vikiiii
vikiiii

Reputation: 9456

If you're willing to pay for commercial software, the latest version of ExtenXLS has full read and write support for all the encryption formats supported by Excel. Just construct an EncryptedWorkBookHandle instead of the normal WorkBookHandle. That will use the strongest possible cipher supported by an unmodified JRE, RC4 for XLS and 128-bit AES for XLSX. If you want to use 256-bit AES with OOXML and you've installed the JCE unlimited policy you can do so with the MSOfficeEncrypter class.

JExcelAPI, a popular open-source Java spreadsheet API, does not appear to support encryption at all. Aspose.Cells, a commercial offering, supports stong encryption. The documentation for Actuate's e.Spreadsheet seems to have disappeared from the 'net, so I can't tell whether it supports encryption or not.

Since none of the freely available Java spreadsheet APIs seems to support writing encrypted spreadsheets, if you're not willing to use commercial software you'll need to come up with a workaround. You could, for example, write the spreadsheet into an encrypted ZIP file. java.util.zip doesn't support encryption, but it looks like Zip4j does.

Upvotes: 1

Related Questions