![](/img/trans.png)
[英]How to disable "Remove Personal Information from file properties on save" in Excel using Java and Apache POI
[英]How can I check if an information is present or not in Excel file using Apache POI and Java Code
我有個問題:
我正在使用以下Java代碼從Excel文件中讀取一些信息:
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook(file);
}catch (IOException ex) {
...
}
SummaryInformation summaryInfo = workbook.getSummaryInformation();
if (summaryInfo.getTitle() != null) {
System.out.println(summaryInfo.getTitle());
}
if (summaryInfo.getAuthor() != null) {
System.out.println(summaryInfo.getAuthor());
}
但是出現此錯誤,但我沒有“標題”信息:
java.lang.NullPointerException
我在此行有此錯誤:
if (summaryInfo.getTitle() != null) {
現在,如何檢查“標題”值(或其他值)是否存在,如果這種情況給我一個錯誤?
您需要確保summaryInfo
不為null。
SummaryInformation summaryInfo = workbook.getSummaryInformation();
if (summaryInfo != null) {
if (summaryInfo.getTitle() != null) {
System.out.println(summaryInfo.getTitle());
}
if (summaryInfo.getAuthor() != null) {
System.out.println(summaryInfo.getAuthor());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.