当前位置:网站首页>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
边栏推荐
- 弘玑|数字化时代下,HR如何进行自我变革和组织变革?
- Batch import of orange single micro service
- Xiuxian real world and game world
- Utf8 to STD: string and STD: string to utf8
- 五一劳动节期间什么理财产品会有收益?
- Executable program execution process
- CMake基础教程(39)pkgconfig
- 创建进程内存管理copy_mm - 进程与线程(九)
- Pytorch deep learning practice_ 11 convolutional neural network
- idea插件---背景播放歌曲
猜你喜欢

STL learning notes 0x0001 (container classification)

Flutter 新一代图形渲染器 Impeller

C语言——恶搞关机小程序
![[untitled] Notepad content writing area](/img/0a/4a3636025c3e0441f45c99e3c67b67.png)
[untitled] Notepad content writing area

Common protocols of OSI layer

Radar equipment (greedy)

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

AcWing 836. Merge set (merge set)

橙单微服务之批量导入

open3d材质设置参数分析
随机推荐
Processus d'exécution du programme exécutable
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
第36期《AtCoder Beginner Contest 248 打比赛总结》
可執行程序執行流程
Understand the relationship between promise async await
X86 assembly syntax: at & T and Intel
50 SQL exercises, answers and detailed analysis
Flutter nouvelle génération de rendu graphique Impeller
Contract lock loophole
提升Facebook触及率和互动率攻略 | 智能客服帮您抓住用户的心
Introduction to qqueue
AcWing 836. Merge set (merge set)
Several examples of pointer transfer, parameter transfer, value transfer, etc
[machine learning] scikit learn introduction
QT drawpixmap and DrawImage blur problem
Establish excel bookkeeping book through setting context menu
QT displays the specified position and size of the picture
‘EddiesObservations‘ object has no attribute ‘filled‘
Create process memory management copy_ Mm - processes and threads (IX)
Breadth first search topics (BFS)