1.maven依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
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();
}
}
}
}
}
