栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328327.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号