Spire.XLS使用教程:在Java中编辑或删除注释

目录

在 Excel 中编辑批注

删除 Excel 中的注释

Spire.XLS for Java是专业的Java Excel API,使开发人员无需使用Microsoft Office或Microsoft Excel即可创建,管理,操作,转换和打印Excel工作表。本文将介绍如何使用Spire.XLS for Java 以编程方式编辑现有注释以及删除注释。

在 Excel 中编辑批注

向 Excel 工作簿添加注释后,您可能希望对添加的注释进行更改。下表列出了一些用于获取现有注释并设置新文本以及注释格式的核心类和方法。

名称 描述
ExcelCommentObject 类 表示 Excel 文档中的注释。
CellRange.getComment() 方法 返回表示特定单元格范围内的注释的注释对象。
ExcelCommentObject.setText() 方法 设置注释文本。
ExcelCommentObject.setHeight() 方法 设置评论的高度。
ExcelCommentObject.setWidth() 方法 设置评论的宽度。
ExcelCommentObject.setAutoSize() 方法 设置是否自动更改指定对象的大小以适合其边界内的文本。

以下是在 Excel 中编辑注释的步骤:

  • 创建一个工作簿实例。
  • 使用Workbook.loadFromFile()方法加载 Excel 文件。
  • 使用WorksheetsCollection.get()方法获取 Excel 文件的第一个工作表。
  • 使用CellRange.getComment()方法获取特定单元格范围内的注释,然后使用ExcelCommentObject类提供的方法为现有注释设置新文本和高度/宽度或自动大小。
  • 使用Workbook.saveToFile()方法将文档保存到另一个文件。
import com.spire.xls.*;

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

        //Create a Workbook instance
        Workbook wb = new Workbook();
        
        //Load an Excel file
        wb.loadFromFile("Comments.xlsx");

        //Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Get comments in specific cells and set new comments
        sheet.getRange().get("A8").getComment().setText("Frank has left the company.");
        sheet.getRange().get("F6").getComment().setText("Best sales.");

        //Set the height and width of the new comments
        sheet.getRange().get("A8").getComment().setHeight(50);
        sheet.getRange().get("A8").getComment().setWidth(100);
        sheet.getRange().get("F6").getComment().setAutoSize(true);

        //Save to file.
        wb.saveToFile("ModifyComment.xlsx",ExcelVersion.Version2013);
        wb.dispose();
    }
}

删除 Excel 中的注释

以下是在 Excel 中删除注释的步骤:

  • 创建一个工作簿实例。
  • 使用Workbook.loadFromFile()方法加载 Excel 文件。
  • 使用WorksheetsCollection.get()方法获取 Excel 文件的第一个工作表。
  • 使用CellRange.getComment()方法获取特定单元格范围内的注释,然后使用ExcelCommentObject.remove()方法删除注释。
  • 使用Workbook.saveToFile()方法将文档保存到另一个文件。
import com.spire.xls.*;

public class DeleteComment {
    public static void main(String[] args) {
        
        //Create a Workbook instance
        Workbook wb = new Workbook();
        
        //Load an Excel file
        wb.loadFromFile("Comments.xlsx");

        //Get the first worksheet
        Worksheet sheet = wb.getWorksheets().get(0);

        //Get the comment in a specific cell and remove it
        sheet.getRange().get("F6").getComment().remove();

        //Save to file.
        wb.saveToFile("DeleteComment.xlsx", ExcelVersion.Version2013);
        wb.dispose();
    }
}

Guess you like

Origin blog.csdn.net/Augenstern__zyx/article/details/120784354
Recommended