当前位置:网站首页>poi生成excel,插入图片
poi生成excel,插入图片
2022-04-23 05:38:00 【wyazyf】
package com.sbr;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class exportExcel {
public static void main(String[] args) {
String picturePath ="C:\\Users\\neusoft\\Desktop\\1.jpg";
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("test picture");
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
// 插入图片
writePicture(wb, sheet1,patriarch, picturePath,3,3);
writePicture(wb, sheet1,patriarch, picturePath,3,4);
writePicture(wb, sheet1,patriarch, picturePath,3,5);
FileOutputStream fileOut;
fileOut = new FileOutputStream("C:\\Users\\neusoft\\Desktop\\测试Excel.xls");
wb.write(fileOut);
System.out.println("----Excle文件已生成------");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param wb 文档对象
* @param sheet1 sheet页
* @param patriarch 图片对象
* @param picturePath 图片路径
* @param index 纵坐标
* @param index1 横坐标
*/
public static void writePicture(HSSFWorkbook wb,HSSFSheet sheet1,HSSFPatriarch patriarch,String picturePath,int index,int index1) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File(picturePath));
//图片后缀
String pictureType = picturePath.substring(picturePath.lastIndexOf(".")+1);
ImageIO.write(bufferImg, pictureType, byteArrayOut);
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) index, index1, (short) index, index1);
anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fileOut != null){
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
版权声明
本文为[wyazyf]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wyazyf/article/details/113752370
边栏推荐
- what is wifi6?
- 字符识别easyocr
- catkin_ What did package do
- 50 SQL exercises, answers and detailed analysis
- MySQL创建oracle练习表
- Usage and difference of shellexecute, shellexecuteex and winexec in QT
- Isosceles triangle - the 9th Lanqiao provincial competition - group C
- Shell instruction learning 1
- Generation of straightening body in 3D slicer
- 热键,界面可视化配置(界面交互)
猜你喜欢

QT displays the specified position and size of the picture

After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner

Flutter 新一代图形渲染器 Impeller

Differences between sea level anatomy and sea surface height anatomy

Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)

橙单微服务之批量导入

Understand the relationship between promise async await

弘玑|数字化时代下,HR如何进行自我变革和组织变革?

Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios

Fletter next generation graphics renderer impaller
随机推荐
Isosceles triangle - the 9th Lanqiao provincial competition - group C
Date增加天数
C language - Spoof shutdown applet
Rog attack
Write the declaration of a function to return the reference of the array, and the array contains 10 string objects (notes)
MySQL创建oracle练习表
Create process memory management copy_ Mm - processes and threads (IX)
Issue 36 summary of atcoder beginer contest 248
[machine learning] scikit learn introduction
Transposed convolution
Differences between auto and decltype inference methods (learning notes)
uni使用的一些坑
巴普洛夫与兴趣爱好
refused connection
Frequently asked interview questions - 1 (non technical)
C# ,类库
[the background color changes after clicking a line]
Wbpack configuring production development environment
Add two pointers? (legal or illegal)
TypeScript interface & type 粗略理解