java 对excel 截图_java利用POI向Excel(xls)写入图片,并对图片引用超链接

本文介绍了如何使用Java的Apache POI库向Excel(xls)文件中写入图片,并为图片添加超链接。通过示例代码展示了如何创建超链接、设置图片样式以及插入图片到指定单元格的过程。
摘要由CSDN通过智能技术生成

1.maven依赖

org.apache.poi

poi

3.9

2.代码示例

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFHyperlink;

import org.apache.poi.hssf.usermodel.HSSFPatriarch;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.ss.usermodel.Hyperlink;

public class WriteImgUtil {

/**

* 写入图片,并插入链接

* @param cell 要插入链接的单元格位置

* @param sheetName 插入的图片所在的工作表

* @param patriarch 画图的顶级管理器,一个sheet只能获取一次,多次插入图片请使用同一个patriarch对象

* @param wb HSSFWorkbook对象

* @param file 图片文件

* @param cellPoint 自定义的对象,指定要插入图片的坐标(x, y)

* @return cellPoint 自定义的对象,返回下一个要插入图片的坐标(x, y)

* @throws IOException

*/

public static CellPoint whiteImg(HSSFCell cell, String sheetName, HSSFPatriarch patriarch, HSSFWorkbook wb, File file, CellPoint cellPoint) throws IOException {

Hyperlink hyperlink = new HSSFHyperlink(Hyperlink.LINK_DOCUMENT);

// "'18 Q2截图'"表示sheet页名称 "A10"表示第几列第几行

hyperlink.setAddress("'18 Q2截图'!A" + (cellPoint.getY() + 1));

cell.setHyperlink(hyperlink);

/* 设置为超链接的样式*/

HSSFCellStyle linkStyle = wb.createCellStyle();

HSSFFont cellFont= wb.createFont();

cellFont.setUnderline((byte) 1);

cellFont.setColor(HSSFColor.BLUE.index);

linkStyle.setFont(cellFont);

cell.setCellStyle(linkStyle);

BufferedImage bufferImg = null;

ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

bufferImg = ImageIO.read(file);

ImageIO.write(bufferImg, "png", byteArrayOut);

int x1 = cellPoint.getX();

int y1 = cellPoint.getY();

int width = bufferImg.getWidth() / 64;

int height = bufferImg.getHeight() / 18;

System.out.println(width + "..." + height);

int x2 = x1 + width;

int y2 = y1 + height;

// anchor主要用于设置图片的属性

HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 0, 0, (short) x1, y1, (short) x2, y2);

// 插入图片

patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

cellPoint = new CellPoint(x1, y2 + 1);

return cellPoint;

}

public static class CellPoint {

private int x;

private int y;

public CellPoint(int x, int y) {

super();

this.x = x;

this.y = y;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

public static void main(String[] args) {

FileOutputStream fileOut = null;

// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray

try {

HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet1 = wb.createSheet("test href");

String sheetName = "18 Q2截图";

HSSFSheet sheet2 = wb.createSheet(sheetName);

// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)

HSSFPatriarch patriarch = sheet2.createDrawingPatriarch();

CellPoint cellPoint = new CellPoint(0, 1);

for (int i = 0; i < 10; i++) {

HSSFRow row = sheet1.createRow(i);

HSSFCell cell = row.createCell(0);

// 点击进行跳转

cell.setCellValue("第" + (i + 1) + "个图片链接");

File file = null;

if (i%2 == 0) {

file = new File("C:/Users/dulinan/Desktop/3333.png");

} else {

file = new File("C:/Users/dulinan/Desktop/Desert.jpg");

}

cellPoint = whiteImg(cell, sheetName, patriarch, wb, file, cellPoint);

}

fileOut = new FileOutputStream("F:/测试Excel3.xls");

// 写入excel文件

wb.write(fileOut);

System.out.println("----Excle文件已生成------");

}

catch (Exception e) {

e.printStackTrace();

}

finally {

if (fileOut != null) {

try {

fileOut.close();

}

catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

8e0f95608f5f394d7f48d247cbd2c2f2.png

e694f85025c86429c4442e2cb8cd860f.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值