Adding Document Properties in Excel in Java Application

·

3 min read

Excel document properties are some descriptive information like author, title, subject, keywords, and category. Adding document properties in Excel can make the documents management more convenient. This article will introduce how to add built-in document properties and custom document properties in Excel by using Free Spire.XLS for Java.

Installation

Method 1: Download the Free Spire.XLS for Java and unzip it. Then add the Spire.Xls.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

Add built-in document properties:

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class BuiltinProperties {
    public static void main(String[] args){
        //Load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Input.xlsx");

        //Add built-in document properties to the file
        workbook.getDocumentProperties().setTitle("Add Document Properties");
        workbook.getDocumentProperties().setSubject("Test File");
        workbook.getDocumentProperties().setAuthor("Mary");
        workbook.getDocumentProperties().setManager("Paul");
        workbook.getDocumentProperties().setCompany("AAA");
        workbook.getDocumentProperties().setCategory("Demo");
        workbook.getDocumentProperties().setKeywords("Excel Document Properties");

        //Save the resultant file
        workbook.saveToFile("BuiltinDocumentProperties.xlsx", ExcelVersion.Version2013);
    }
}

in.png

Add custom document properties:

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

import java.util.Date;

public class CustomProperties {
    public static void main(String[] args){
        //Load an Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Input.xlsx");

        //Add custom properties to the file
        workbook.getCustomDocumentProperties().add("_MarkAsFinal", true);
        workbook.getCustomDocumentProperties().add("The Editor", "Mary");
        workbook.getCustomDocumentProperties().add("Phone number", 12345678);
        workbook.getCustomDocumentProperties().add("Revision number", 7.12);
        workbook.getCustomDocumentProperties().add("Revision date", new Date());

        //Save the resultant file
        workbook.saveToFile("CustomDocumentProperties.xlsx", ExcelVersion.Version2013);
    }
}

cus.jpg