【无标题】

一: 需求说明

需要将excel中某个sheet页的图表导出生成图片,要求无水印,柱状图要求有引导线连接值。

二: 解决思路

用商业版本jar包导出图表为图片,并用白底RGB覆盖掉水印的RGB
注意: 定义的lengends的RGB不在覆盖范围!!!

三: 准备阶段

1.官网下载最新的spire.xls(商业版本的jar包,没有行数限制)
2.下载地址: https://www.e-iceblue.cn/Downloads/Spire-XLS-JAVA.html
3.或者直接从项目process,resource/lib下取Spire.Xls.jar
4.在pom.xml中添加dependency

e-iceblue
spire.xls
0.0.1-SNAPSHOT
system
${project.basedir}/src/main/resources/lib/Spire.Xls.jar

四: 代码实现

Workbook workbook = new Workbook();
//加载Excel文档
workbook.loadFromFile(noProductPath);
//noProductEchartsPath为生成的echarts图表地址
File file = new File(noProductEchartsPath);
//将Excel文档图表sheet页的第一个图表保存为图片
BufferedImage image= workbook.saveChartAsImage(workbook.getWorksheets().get("图表"), 0);
try {
ImageIO.write(image,"png", file);
} catch (IOException e) {
throw new ApiException(SV.CommonEC, "非产品收入图表的附件echarts图表转图片失败");
}
//图片去除水印并替换原来图片
convertAllImages(file);
public static void convertAllImages(File file) {
try {
BufferedImage bi = ImageIO.read(file); //用ImageIO流读取像素块
if (bi != null) {
// 去除水印
removeWatermark(bi);
String formatName = file.getName().substring(file.getName().lastIndexOf(".") + 1);//生成的图片格式
ImageIO.write(bi, formatName, file);//用ImageIO流生成的处理图替换原图片
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**

  • 去除水印
    */

```java
private static void removeWatermark(BufferedImage bi) {
Color wColor = new Color(254, 254, 254);
//白底水印
for (int i = 0; i < bi.getWidth(); i++) {
for (int j = 0; j < bi.getHeight(); j++) {
int color = bi.getRGB(i, j);
Color oriColor = new Color(color);
int red = oriColor.getRed();
int greed = oriColor.getGreen();
int blue = oriColor.getBlue();
if (red == 254 && greed == 254 && blue == 254) {
continue;
}
if (red >= 254 && greed >= 100 && blue >= 100) {
bi.setRGB(i, j, wColor.getRGB());
}
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值