Java: Hide or Show Comments in Excel

Comments in Excel are blocks of text that can be added to cells, mainly used to provide additional explanation or supplemental information about the cell contents. Users can add comments to the specific cells to better explain the data of worksheets. However, sometimes too many comments will cause visual clutter or obstruct other content. To avoid this issue, existing comments can be hidden programmatically to make the worksheet more organized and readable. Hidden comments can also be easily displayed when necessary. This article will show you how to hide or show comments in Excel using Spire.XLS for Java.

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.6.2</version>
    </dependency>
</dependencies>
    

Hide Comments in Excel

Spire.XLS for Java provides the Worksheet.getComments().get().isVisble() method to control the visibility of comments. You can easily hide existing comments by setting the parameter of this method to "false". The following are detailed steps to hide comments in excel.

  • Create an object of Workbook class.
  • Load a sample file from disk using Workbook.loadFromFile() method.
  • Get the desired worksheet of this file by calling Workbook.getWorksheets().get() method.
  • Hide the specific comments in this sheet by setting the parameter of the Worksheet.getComments().get().isVisble() method to "false".
  • Finally, save the result file using Workbook.savaToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class HideComment {
    public static void main(String[] args){

        //Create an object of Workbook class
        Workbook workbook = new Workbook();

        //Load a sample file from disk
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet in this file
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Hide the first and the second comments in this sheet
        sheet.getComments().get(0).isVisible(false);
        sheet.getComments().get(1).isVisible(false);

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

Java: Hide or Show Comments in Excel

Show Comments in Excel

Hidden comments can also be easily displayed when necessary. If you want to show them again, please set the parameter of the Worksheet.getComments().get().isVisble() method to "true". The following are detailed steps of showing hidden comments in excel.

  • Create an object of Workbook class.
  • Load a sample file from disk using Workbook.loadFromFile() method.
  • Get the desired worksheet by calling Workbook.getWorksheets().get() method.
  • Show the specific comment in this sheet by setting the parameter of the Worksheet.getComments().get().isVisble() method to "true".
  • Finally, save the result file using Workbook.savaToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class ShowComment {
    public static void main(String[] args){

        //Create an object of Workbook class
        Workbook workbook = new Workbook();

        //Load a sample file from disk
        workbook.loadFromFile("HideComment.xlsx");

        //Get the first worksheet in this file
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Show the first comment in this sheet
        sheet.getComments().get(0).isVisible(true);

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

Java: Hide or Show Comments in 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.