Java: Add Document Properties to Excel

Adding document properties to an Excel file is a simple and convenient way to provide additional context and information about the file. Document properties can be either standard or custom. Standard document properties, such as author, title, and subject, offer basic information about the file and make it easier to locate and identify. Custom document properties allow users to add specific details about the file, such as project name, client name, or department owner, providing relevant information and context to the data presented in the file. In this article, we will demonstrate how to add standard document properties and custom document properties to an Excel file in Java using Spire.XLS for Java library.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Add Standard Document Properties to an Excel File in Java

Standard document properties are pre-defined by Microsoft Excel and include fields such as Title, Subject, Author, Keywords, and Comments. The following steps demonstrate how to add standard document properties to an Excel file in Java using Spire.XLS for Java:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile(String fileName) method.
  • Add standard document properties, such as title, subject and author to the file using the Workbook.getDocumentProperties().setTitle(String value), Workbook.getDocumentProperties().setSubject(String value), Workbook.getDocumentProperties().setAuthor(String value) methods.
  • Save the result file using the Workbook.saveToFile(String fileName, ExcelVersion version) method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

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

        //Add standard document properties to the file
        workbook.getDocumentProperties().setTitle("Add Document Properties");
        workbook.getDocumentProperties().setSubject("Spire.XLS for Java Demo");
        workbook.getDocumentProperties().setAuthor("Shaun");
        workbook.getDocumentProperties().setManager("Bill");
        workbook.getDocumentProperties().setCompany("E-iceblue");
        workbook.getDocumentProperties().setCategory("Spire.XLS for Java");
        workbook.getDocumentProperties().setKeywords("Excel Document Properties");

        //Save the result file
        workbook.saveToFile("AddStandardDocumentProperties.xlsx", ExcelVersion.Version2016);
        workbook.dispose();
    }
}

Java: Add Document Properties to Excel

Add Custom Document Properties to an Excel File in Java

Custom document properties are user-defined and can be tailored to suit specific needs or requirements. The data type of the custom document properties can be Yes or No, Text, Number, and Date. The following steps demonstrate how to add custom document properties to an Excel file in Java using Spire.XLS for Java:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook.loadFromFile(String fileName) method.
  • Add a custom document property of "Yes or No" type to the file using the Workbook.getCustomDocumentProperties().add(String var1, boolean var2) method.
  • Add a custom document property of "Text" type to the file using the Workbook.getCustomDocumentProperties().add(String var1, String var2) method.
  • Add a custom document property of "Number" type to the file using the Workbook.getCustomDocumentProperties().add(String var1, int var2) method.
  • Add a custom document property of "Date" type to the file using the Workbook.getCustomDocumentProperties().add(String var1, Date var2) method.
  • Save the result file using the Workbook.saveToFile(String fileName, ExcelVersion version) method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

import java.util.Date;

public class AddCustomDocumentProperties {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Input.xlsx");
        
        //Add a “yes or no” custom document property
        workbook.getCustomDocumentProperties().add("Revised", true);
        //Add a “text” custom document property
        workbook.getCustomDocumentProperties().add("Client Name", "E-iceblue");
        //Add a “number” custom document property
        workbook.getCustomDocumentProperties().add("Phone number", 81705109);
        //Add a “date” custom document property
        workbook.getCustomDocumentProperties().add("Revision date", new Date());

        //Save the result file
        workbook.saveToFile("AddCustomDocumentProperties.xlsx", ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Add Document Properties to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.