*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,立即购买!想要获取更多福利的朋友可以咨询在线客服哦~
为 Java 安装 Spire.XLS
首先,您需要在 Java 程序中添加 Spire.Xls.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以通过将以下代码添加到项目的 pom.xml 文件中,轻松地在应用程序中导入 JAR 文件。
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository></repositories><dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>4.9.0</version> </dependency></dependencies>
向 Excel 工作簿添加注释后,您可能希望对添加的注释进行更改。下表列出了一些用于获取现有注释并设置新文本以及注释格式的核心类和方法。
名称 | 描述 |
ExcelCommentObject 类 | 表示 Excel 文档中的注释。 |
CellRange.getComment() 方法 | 返回表示特定单元格范围内的注释的注释对象。 |
ExcelCommentObject.setText() 方法 | 设置注释文本。 |
ExcelCommentObject.setHeight() 方法 | 设置评论的高度。 |
ExcelCommentObject.setWidth() 方法 | 设置评论的宽度。 |
ExcelCommentObject.setAutoSize() 方法 | 设置是否自动更改指定对象的大小以适合其边界内的文本。 |
- 创建一个工作簿实例。
- 使用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(); }}
是E-iceblue官方友好合作伙伴,如果您对spire.xls,可以联系在线客服了解具体授权价格和使用机制
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!