当前位置:网站首页>POI generates excel and inserts pictures
POI generates excel and inserts pictures
2022-04-23 05:42: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");
// Top level manager for drawing , One sheet You can only get one ( Be sure to pay attention to this )
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
// Insert a picture
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\\ test Excel.xls");
wb.write(fileOut);
System.out.println("----Excle File generated ------");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param wb Document object
* @param sheet1 sheet page
* @param patriarch Image objects
* @param picturePath Picture path
* @param index Ordinate
* @param index1 Abscissa
*/
public static void writePicture(HSSFWorkbook wb,HSSFSheet sheet1,HSSFPatriarch patriarch,String picturePath,int index,int index1) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
// First put the pictures you read into a ByteArrayOutputStream in , In order to produce ByteArray
try {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File(picturePath));
// Picture suffix
String pictureType = picturePath.substring(picturePath.lastIndexOf(".")+1);
ImageIO.write(bufferImg, pictureType, byteArrayOut);
//anchor It is mainly used to set the properties of the picture
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) index, index1, (short) index, index1);
anchor.setAnchorType(3);
// Insert a picture
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://yzsam.com/2022/04/202204230538215170.html
边栏推荐
- Deep learning object detection
- freemark中插入图片
- ‘EddiesObservations‘ object has no attribute ‘filled‘
- Similarities and differences between vector and array (notes)
- 弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
- refused connection
- Radar equipment (greedy)
- Mysql 查询使用\G,列转行
- Character recognition easyocr
- MySQL query uses \ g, column to row
猜你喜欢
uni使用的一些坑
‘EddiesObservations‘ object has no attribute ‘filled‘
Understand the relationship between promise async await
Isosceles triangle - the 9th Lanqiao provincial competition - group C
创建进程内存管理copy_mm - 进程与线程(九)
Duplicate key update in MySQL
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
Data mining -- understanding data
deep learning object detection
Differences between sea level anatomy and sea surface height anatomy
随机推荐
Map对象 map.get(key)
Differences between sea level anatomy and sea surface height anatomy
CMake基础教程(39)pkgconfig
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
Create process memory management copy_ Mm - processes and threads (IX)
基于ssm 包包商城系统
refused connection
Summary of redis classic interview questions 2022
On the use of constant pointer and pointer constant -- exercise (record)
Hongji | how does HR carry out self change and organizational change in the digital era?
Fast application fuzzy search
Formal parameters, local variables and local static variables
3d slicer中拉直体的生成
Contract lock loophole
[the background color changes after clicking a line]
[triangle Yang Hui triangle printing odd even cycle JS for break cycle]
[no title] Click the classification jump page to display the details
catkin_package到底干了什么
Utf8 to STD: string and STD: string to utf8
poi导出excel,行相同数据自动合并单元格