Excel File Properties
The following example shows how you can read and write Excel file's Document Properties like Title, Author, Comments, etc.

import com.gembox.spreadsheet.*;
import java.util.Map;
class Program {
public static void main(String[] args) throws java.io.IOException {
// If using Professional version, put your serial key below.
SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");
ExcelFile workbook = ExcelFile.load("%InputFileName%");
// Add Sheet
ExcelWorksheet worksheet = workbook.getWorksheets().insertEmpty(0, "Document Properties");
workbook.getWorksheets().setActiveWorksheet(worksheet);
int rowIndex = 0;
// Read Built-in Document Properties
worksheet.getCell(rowIndex++, 0).setValue("Built-in document properties");
worksheet.getCell(rowIndex, 0).setValue("Property");
worksheet.getCell(rowIndex++, 1).setValue("Value");
for (Map.Entry<BuiltInDocumentProperties, String> keyValue : workbook.getDocumentProperties().getBuiltIn().entrySet()) {
worksheet.getCell(rowIndex, 0).setValue(keyValue.getKey().toString());
worksheet.getCell(rowIndex++, 1).setValue(keyValue.getValue());
}
// Read Custom Document Properties
worksheet.getCell(++rowIndex, 0).setValue("Custom Document Properties");
worksheet.getCell(++rowIndex, 0).setValue("Property");
worksheet.getCell(rowIndex++, 1).setValue("Value");
// Custom document properties are not supported in XLS
for (Map.Entry<String, Object> keyValue : workbook.getDocumentProperties().getCustom().entrySet()) {
worksheet.getCell(rowIndex, 0).setValue(keyValue.getKey());
worksheet.getCell(rowIndex++, 1).setValue(keyValue.getValue().toString());
}
// Write/Modify Document Properties
workbook.getDocumentProperties().setBuiltIn(BuiltInDocumentProperties.AUTHOR, "John Doe");
workbook.getDocumentProperties().setBuiltIn(BuiltInDocumentProperties.TITLE, "Generated title");
worksheet.getColumn(0).setWidth(192, LengthUnit.PIXEL);
worksheet.getColumn(1).setWidth(217, LengthUnit.PIXEL);
workbook.save("Document Properties.%OutputFileType%");
}
}
Want more?
Like it?
Published: December 13, 2018 | Modified: December 16, 2019 | Author: Marek Turis