取消setfill_Java 添加、修改、读取、复制、删除Excel批注的实现

批注,是作者或审阅者给文档添加的注释或注解。通过查看批注,可以更加详细地了解某些文字的背景。除了直接添加文本信息外,还可为文本信息填充背景图片使其更具丰富性和美观性。本文将通过使用Java程序来演示如何在Excel文档中添加、修改、读取、复制和删除批注。

Jar文件获取及导入:

方法1:通过官方网站下载获取jar包。解压后将lib文件夹下的Spire.Xls.jar文件导入Java程序。(如下图)

方法2:通过maven仓库安装导入。具体安装详解参见此网页。

【示例1】添加批注

import com.spire.xls.*;

import java.awt.*;

public class AddComments {

public static void main(String[] args) {

//加载Excel文档

Workbook wb = new Workbook();

wb.loadFromFile("D:\\Desktop\\Sample.xlsx");

//获取工作表

Worksheet sheet = wb.getWorksheets().get(0);

//创建字体

ExcelFont font = wb.createFont();

font.setFontName("Arial");

font.setSize(11);

font.setKnownColor(ExcelColors.Orange);

ExcelFont fontBlue = wb.createFont();

fontBlue.setKnownColor(ExcelColors.LightBlue);

ExcelFont fontGreen = wb.createFont();

fontGreen.setKnownColor(ExcelColors.LightGreen);

//给指定的Excel单元格添加普通批注

CellRange range = sheet.getCellRange("G3");

range.getComment().setText("正式员工");

range.getComment().setTextRotation(TextRotationType.TopToBottom);

range.autoFitColumns();

range.getComment().setVisible(true);//设置批注是否隐藏

range.getComment().getFill().customPicture("D:\\Desktop\\Image.jpg");//背景图片填充

//range.getComment().getFill().setForeColor(new Color(255,228,225));//颜色填充

// 给指定的Excel单元格添加富文本批注

range = sheet.getCellRange("G7");

range.getRichText().setFont(0, 8, font);

range.autoFitColumns();

range.getComment().getRichText().setText("试用员工");

range.getComment().getRichText().setFont(0, 4, fontGreen);

range.getComment().getRichText().setFont(3, 4, fontBlue);

//保存结果文档

wb.saveToFile("output/AddComments.xlsx", ExcelVersion.Version2013);

wb.dispose();

}

}

批注添加效果:

【示例2】修改批注

import com.spire.xls.*;

public class ModifyComments {

public static void main(String[] args) {

//加载excel文档

Workbook wb = new Workbook();

wb.loadFromFile("D:\\Desktop\\AddComments.xlsx");

//获取工作表

Worksheet sheet = wb.getWorksheets().get(0);

//获取指定单元格中的批注,设置新的批注文本、填充色

sheet.getRange().get("G3").getComment().setText("个人使用");

sheet.getRange().get("G7").getComment().setText("团队使用");

//保存文档

wb.saveToFile("output/ModifyComment.xlsx",ExcelVersion.Version2013);

wb.dispose();

}

}

批注修改效果:

【示例3】读取批注

import com.spire.xls.*;

import javax.imageio.ImageIO;

import java.awt.*;import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

public class ReadComments {

public static void main(String[] args) throws IOException {

//加载excel文档

Workbook wb = new Workbook();

wb.loadFromFile("D:\\Desktop\\AddComments.xlsx");

//获取工作表

Worksheet sheet = wb.getWorksheets().get(0);

//打印指定单元格的文本批注内容

System.out.println("G3 的批注文本 = " + sheet.getCellRange("G3").getComment().getText());

System.out.println("G7 的批注文本 = " + sheet.getCellRange("G7").getComment().getRichText().getRtfText());

//获取指定单元格中批注背景颜色

Color color = sheet.getRange().get("G3").getComment().getFill().getForeColor();

System.out.print(color);

//获取指定单元格中的批注背景图片

BufferedImage image = sheet.getRange().get("G3").getComment().getFill().getPicture();

ImageIO.write(image,"png",new File("output/ExtractedImage.png"));

}

}

批注读取效果:

【示例4】复制批注

import com.spire.xls.*;

import java.awt.*;

import java.awt.image.BufferedImage;

public class CopyComments {

public static void main(String[] args) {

//加载Excel文档

Workbook wb = new Workbook();

wb.loadFromFile("D:\\Desktop\\AddComments.xlsx");

//获取指定工作表

Worksheet sheet = wb.getWorksheets().get(0);

//获取源单元格批注内容

CellRange range = sheet.getRange().get("G3");//获取指定单元格

String commenttext = range.getComment().getText();//获取批注文本

BufferedImage image = range.getComment().getFill().getPicture();//获取批注填充图片

// Object object = range.getComment().getFill().getForeColor();//获取批注填充色

// 获取新的单元格,添加批注文本和图片(颜色)填充

CellRange range1 = sheet.getRange().get("G12");

range1.getComment().setText(commenttext);

range1.getComment().getFill().customPicture(image," ");

//range1.getComment().getFill().setForeColor((Color) object);

// 保存文档

wb.saveToFile("output/CopyComment.xlsx",ExcelVersion.Version2013);

wb.dispose();

}

}

批注复制效果:

【示例5】删除批注

import com.spire.xls.ExcelVersion;

import com.spire.xls.FileFormat;

import com.spire.xls.Workbook;

import com.spire.xls.Worksheet;

public class DeleteComments {

public static void main(String[] args) {

//加载Excel文档

Workbook wb = new Workbook();

wb.loadFromFile("D:\\Desktop\\AddComments.xlsx");

//获取工作表

Worksheet sheet = wb.getWorksheets().get(0);

//获取指定单元格中的批注,并删除

sheet.getRange().get("G3").getComment().remove();

//保存文档

wb.saveToFile("output/DeleteComment.xlsx", ExcelVersion.Version2013);

wb.dispose();

}

}

批注删除效果:

Java POI设置Excel指定单元格添加批注、背景色

Cell cell = worksheet.getRow(row).getCell(column);

Drawing draw = worksheet.createDrawingPatriarch();

Comment comment = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, row, column, 9, 7));

comment.setString(new XSSFRichTextString(message));//设置批注内容

cell.setCellComment(comment);

CellStyle cellStyle=workbook.createCellStyle();

cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); // 背景色

cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

cell.setCellStyle(cellStyle);

到此这篇关于Java 添加、修改、读取、复制、删除Excel批注的实现的文章就介绍到这了,更多相关Java  添加删除Excel批注内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

Java实现爱心跳动可以使用图形库来绘制动画效果。你可以使用JavaFX或者Swing来实现这个效果。下面是一个使用JavaFX实现爱心跳动的简单示例代码: ``` import javafx.animation.Animation; import javafx.animation.Interpolator; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.CubicCurveTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage; import javafx.util.Duration; public class HeartbeatAnimation extends Application { @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 400, 400, Color.WHITE); Path path = new Path(); path.getElements().add(new MoveTo(200, 200)); path.getElements().add(new CubicCurveTo(200, 120, 100, 120, 100, 200)); path.getElements().add(new CubicCurveTo(100, 280, 200, 280, 200, 200)); path.setStroke(Color.RED); path.setFill(Color.TRANSPARENT); final Timeline timeline = new Timeline(); timeline.setCycleCount(Animation.INDEFINITE); timeline.setAutoReverse(true); final KeyValue kv1 = new KeyValue(path.scaleXProperty(), 1); final KeyValue kv2 = new KeyValue(path.scaleYProperty(), 1); final KeyFrame kf1 = new KeyFrame(Duration.millis(500), kv1, kv2); timeline.getKeyFrames().add(kf1); timeline.play(); root.getChildren().add(path); primaryStage.setTitle("Heartbeat Animation"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 这段代码使用了JavaFX的Path类来绘制爱心形状,并通过Timeline和KeyFrame来实现循环缩放动画效果。你可以将这段代码复制到一个Java项目中,并运行它来看到爱心跳动的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值