我有幾個設定了“只讀推薦”標志的 xlsx 檔案。
- 是否有可用于禁用此復選框的 POI 5.x 方法?

我查看了POI XML 屬性,但沒有運氣。
uj5u.com熱心網友回復:
在Office Open XML-Excel 檔案中,設定“只讀推薦”/xl/workbook.xml作為屬性存盤在fileSharingXML 元素中。
...
<fileSharing readOnlyRecommended="1"/>
...
這可以apache poi僅使用低級別的類來設定或取消設定org.openxmlformats.schemas.spreadsheetml.x2006.main.*。
完整示例:
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();
}
}
如果sourceFile.xlsx設定了該選項,但沒有密碼,則在運行該代碼后,resultFile.xlsx不再設定該選項。
如果也設定了密碼,則首先需要解密,并且也fileSharing需要取消設定密碼。當然,至少需要知道解密密碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/480291.html
標籤:爪哇 擅长 apache-poi 只读属性
上一篇:為什么我的CSS邊框沒有出現在JavaFXgui中?
下一篇:逗號后洗掉不同小數位的復雜函式
