I am using the Apache POI library version 5.2.0 in Java to write an XLS (Excel) file.
void writeExcelFile() {
var workbook = new HSSFWorkbook();
byte[] bytes;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
workbook.write(bos);
bytes = bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
Files.write(Path.of("foo.xls"), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
When I run the resulting foo.xls
file through the strings
program, I see that my system username (christoffer.gamrath
) is part of the file contents:
$ strings foo.xls main?
christoffer.gamrath B
Arial1
Arial1
Arial1
Arial
"$"#,##0_);("$"#,##0)
"$"#,##0_);[Red]("$"#,##0)
"$"#,##0.00_);("$"#,##0.00)
"$"#,##0.00_);[Red]("$"#,##0.00)
_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)
_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)
_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)
_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)
Sheet1
I want to avoid storing my username in the file, so that when I run the same code on a different machine the resulting file will be exactly the same.
I have tried setting properties such as author on the workbook:
void writeExcelFileWithoutProperties() {
var workbook = new HSSFWorkbook();
workbook.createInformationProperties();
SummaryInformation summaryInfo = workbook.getSummaryInformation();
if (summaryInfo != null) {
summaryInfo.setAuthor("");
summaryInfo.setLastAuthor("");
summaryInfo.setTitle("");
summaryInfo.setSubject("");
summaryInfo.setKeywords("");
summaryInfo.setComments("");
}
DocumentSummaryInformation docSummaryInfo = workbook.getDocumentSummaryInformation();
if (docSummaryInfo != null) {
docSummaryInfo.setCompany("");
docSummaryInfo.setManager("");
}
byte[] bytes;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
workbook.write(bos);
bytes = bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
Files.write(Path.of("foo2.xls"), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
This does not resolve the issue: the strings
output is exactly the same.
As soon as you create the HSSFWorkbook
, Apache POI embeds the system username into the file.
Even if you later clear or overwrite the author field (as you do), the username has already been written into internal metadata sections that the high-level API can't fully remove. When you create:
var workbook = new HSSFWorkbook();
there's no going back from here. Once it's initialized, the author will be the one set as system property in:
System.getProperty("user.name")
In order to clear your username: before you create the new HSSFWorkbook
, clear the system property:
System.setProperty("user.name", "");
var workbook = new HSSFWorkbook();