I have several xlsx files that have the "read-only recommended" flag set.
I looked through POI XML Properties with no luck.
In Office Open XML
-Excel files the setting "read only recommended" is stored in /xl/workbook.xml
as an attribute in a fileSharing
XML-element.
...
<fileSharing readOnlyRecommended="1"/>
...
This can be set or unset using apache poi
only using the low level org.openxmlformats.schemas.spreadsheetml.x2006.main.*
-classes.
Complete example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import java.io.*;
class ExcelRemoveReadOnlyRecommended {
static void removeReadOnlyRecommended(XSSFWorkbook workbook) {
CTWorkbook ctWorkbook = workbook.getCTWorkbook();
CTFileSharing ctfilesharing = ctWorkbook.getFileSharing();
if (ctfilesharing != null) {
ctfilesharing.setReadOnlyRecommended(false);
}
}
public static void main(String[] args) throws Exception {
String sourceFilePath = "./sourceFile.xlsx";
String resultFilePath = "./resultFile.xlsx";
Workbook workbook = WorkbookFactory.create(new FileInputStream(sourceFilePath));
if (workbook instanceof XSSFWorkbook) {
removeReadOnlyRecommended((XSSFWorkbook)workbook);
}
OutputStream out = new FileOutputStream(resultFilePath);
workbook.write(out);
out.close();
workbook.close();
}
}
If the sourceFile.xlsx
has set that option, but without passwords, then after running that code the resultFile.xlsx
has not set that option anymore.
If passwords are set too, then decryption is needed at first and the password in fileSharing
needs to be unset too. Of course then at least the decryption-password needs to be known.