Java add, read and delete Excel document properties

Keywords: Excel Java Attribute Maven

In document properties, you can set many information about the document, such as creation time, author, company, category, keywords, notes and other summary information, as well as some custom document properties. Next, we will use Java program to demonstrate how to set it. At the same time, we can also read and delete the existing information in the document.

Sample Outline:

1. Add document properties

1.1 add summary information

1.2 add custom document information

2. Read document properties

3. Delete document information

3.1 delete all summary information and custom document properties

3.2 delete specified summary information and custom document attributes

 

Using tool: Spire.XLS for Java

Access method 1: through the official website download Bag. After downloading, extract the file and import the jar file under the lib folder into the java program; or Maven Warehouse download import. Jar import effect is as follows:

Java code example

[example 1] add Excel document attribute

import com.spire.xls.*;
import java.util.Date;

public class AddProperties {
    public static void main(String[] args) {
        //Load Excel document
        Workbook wb = new Workbook();
        wb.loadFromFile("input.xlsx");

        //Set built-in document properties such as title, subject, author, etc. for the document
        wb.getDocumentProperties().setTitle("Set document properties");
        wb.getDocumentProperties().setSubject("A class");
        wb.getDocumentProperties().setAuthor("Bubble");
        wb.getDocumentProperties().setManager("July");
        wb.getDocumentProperties().setCompany("Alibaba");
        wb.getDocumentProperties().setCategory("inside");
        wb.getDocumentProperties().setKeywords("Document, draft");

        //Add custom document properties to a document
        wb.getCustomDocumentProperties().add("_MarkAsFinal", true);
        wb.getCustomDocumentProperties().add("edit", "Administrator");
        wb.getCustomDocumentProperties().add("Contact number", 12345678);
        wb.getCustomDocumentProperties().add("Update date", new Date());

        //Save result document
        wb.saveToFile("AddProperties.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}

The generated document can view the effect of attribute addition.

 

[example 2] Reading Excel document properties

import com.spire.xls.*;

public class ReadProperties {
    public static void main(String[] args) {
        //Load Excel document
        Workbook wb = new Workbook();
        wb.loadFromFile("AddProperties.xlsx");

        //Get Excel built in document properties
        System.out.println("Title: " + wb.getDocumentProperties().getTitle());
        System.out.println("Theme: " + wb.getDocumentProperties().getSubject());
        System.out.println("Author: " + wb.getDocumentProperties().getAuthor());
        System.out.println("Company: " + wb.getDocumentProperties().getCompany());
        System.out.println("Executive director: " + wb.getDocumentProperties().getManager());
        System.out.println("Category: " + wb.getDocumentProperties().getCategory());
        System.out.println("Keyword: " + wb.getDocumentProperties().getKeywords());

        //Get Excel custom document properties
        DocumentProperty property = (DocumentProperty) wb.getCustomDocumentProperties().get(0);
        //Read the name and value of the first custom document property
        System.out.println("Name: " + property.getName());
        System.out.println("Value: " + property.getValue());
    }
}

Document property read result:

[example 3] delete Excel document attribute

import com.spire.xls.*;

public class RemoveProperties {
    public static void main(String[] args) {
        //Load Excel document
        Workbook wb = new Workbook();
        wb.loadFromFile("AddProperties.xlsx");

        //Delete the built-in property by setting the value of the corresponding document property to null
        wb.getDocumentProperties().setTitle("");
        wb.getDocumentProperties().setSubject("");
        wb.getDocumentProperties().setAuthor("");
        wb.getDocumentProperties().setCompany("");
        wb.getDocumentProperties().setManager("");
        wb.getDocumentProperties().setCategory("");
        wb.getDocumentProperties().setKeywords("");
        wb.getDocumentProperties().setComments("");

        //Remove the custom document property based on its name
        wb.getCustomDocumentProperties().remove("edit");
        wb.getCustomDocumentProperties().remove("Contact number");

        //Save document
        wb.saveToFile("RemoveProperties.xlsx", ExcelVersion.Version2010);
        wb.dispose();
    }
}

The generated document can view the attribute deletion effect.

(end of this paper)

237 original articles published, 93 praised, 510000 visitors+
His message board follow

Posted by pmaiorana on Thu, 20 Feb 2020 23:17:31 -0800