Java POI writes pictures to excel
<span style="color: #0000ff;">import<span style="color: #000000;"> javax.imageio.ImageIO; <span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.poi.hssf.usermodel.HSSFClientAnchor;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.poi.hssf.usermodel.HSSFPatriarch;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.poi.hssf.usermodel.HSSFSheet;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.apache.poi.hssf.usermodel.HSSFWorkbook;
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> Picture {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void<span style="color: #000000;"> main(String[] args) {
<span style="color: #008000;">//<span style="color: #008000;">String filepath = SpringConfigUtil.getValue("filepath");
<span style="color: #008000;">//<span style="color: #008000;">String picturepath = SpringConfigUtil.getValue("picturepath");
<span style="color: #008000;">//<span style="color: #008000;">int rowNum = Integer.parseInt(SpringConfigUtil.getValue("rowNum"));
<span style="color: #008000;">//<span style="color: #008000;"> int cellNum = Integer.parseInt(SpringConfigUtil.getValue("cellNum"));
<span style="color: #0000ff;">boolean result = insertPicture("E:\data\file\201509\2015.xls","E:\data\file\201509\test.jpg",3,5);<span style="color: #008000;">//<span style="color: #008000;"> 1代表插入的行数-1
<span style="color: #008000;">//<span style="color: #008000;"> ,2代表插入的列数-1
<span style="color: #000000;">
System.out.println("图片插入结果为==" +<span style="color: #000000;"> result);
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>地址 <a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>地址 插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>位置的关键字 <a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>类型 </span><span style="color: #008000;">*/</span>
<span style="color: #008000;">/**</span><span style="color: #008000;">
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> filePath
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> picturePath
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> rowNum
* </span><span style="color: #808080;">@param</span><span style="color: #008000;"> cellNum
* </span><span style="color: #808080;">@return</span>
<span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">boolean</span> insertPicture(String filePath,String picturePath,<span style="color: #0000ff;">int</span> rowNum,<span style="color: #0000ff;">int</span><span style="color: #000000;"> cellNum) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 初始化IO流<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a></span>
<span style="color: #000000;">
FileOutputStream fileOut = <span style="color: #0000ff;">null<span style="color: #000000;">;
BufferedImage bufferImg = <span style="color: #0000ff;">null<span style="color: #000000;">;
<span style="color: #0000ff;">int rowN = rowNum;<span style="color: #008000;">//<span style="color: #008000;"> 图片插入行的初始化
<span style="color: #0000ff;">int cellN = cellNum;<span style="color: #008000;">//<span style="color: #008000;"> 图片插入列的初始化
<span style="color: #0000ff;">try<span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 先把读进来的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>放到<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>ByteArrayOutputStream中,以便产生ByteArray</span>
ByteArrayOutputStream byteArrayOut = <span style="color: #0000ff;">new</span><span style="color: #000000;"> ByteArrayOutputStream();
bufferImg </span>= ImageIO.read(<span style="color: #0000ff;">new</span> File(picturePath));<span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>地址</span>
<span style="color: #000000;">
ImageIO.write(bufferImg,"png"<span style="color: #000000;">,byteArrayOut);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>工作薄</span>
HSSFWorkbook wb = <span style="color: #0000ff;">new</span> HSSFWorkbook(<span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(filePath));
HSSFSheet sheet1 </span>= wb.getSheetAt(0<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>需要的容器</span>
HSSFPatriarch patriarch =<span style="color: #000000;"> sheet1.createDrawingPatriarch();
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
* HSSFClientAnchor几个数字解释:3:是x轴的开始节点, 0:
* 是y轴的开始节点,1023:是x轴的结束节点,255:是y轴的结束节点
* ,1:是从Excel的2列开始插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>,10:是从excel的第11行开始插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>,
* 11:<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>占用11列的位置,25:<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>结束在excel的26行
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
HSSFClientAnchor anchor </span>= <span style="color: #0000ff;">new</span> HSSFClientAnchor(0,1023,255,(<span style="color: #0000ff;">short</span>) ((<span style="color: #0000ff;">short</span><span style="color: #000000;">) cellN),(rowN),(</span><span style="color: #0000ff;">short</span>) ((<span style="color: #0000ff;">short</span>) cellN + 1<span style="color: #000000;">),(rowN));
anchor.setAnchorType(</span>2<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a></span>
<span style="color: #000000;">
patriarch.createPicture(anchor,wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileOutputStream(filePath);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 写入excel<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a></span>
<span style="color: #000000;"> wb.write(fileOut);
fileOut.close();
<span style="color: #0000ff;">return <span style="color: #0000ff;">true<span style="color: #000000;">;
} <span style="color: #0000ff;">catch<span style="color: #000000;"> (IOException io) {
io.printStackTrace();
Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(</span>"io erorr : " +<span style="color: #000000;"> io.getMessage());
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
} </span><span style="color: #0000ff;">finally</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">if</span> (fileOut != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
fileOut.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
e.printStackTrace();
}
}
}
}
}