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;"&gt;/*</span><span style="color: #008000;"&gt; 插入<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;"&gt;*/</span>

<span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt;
 * </span><span style="color: #808080;"&gt;@param</span><span style="color: #008000;"&gt; filePath
 * </span><span style="color: #808080;"&gt;@param</span><span style="color: #008000;"&gt; picturePath
 * </span><span style="color: #808080;"&gt;@param</span><span style="color: #008000;"&gt; rowNum
 * </span><span style="color: #808080;"&gt;@param</span><span style="color: #008000;"&gt; cellNum
 * </span><span style="color: #808080;"&gt;@return</span>
 <span style="color: #008000;"&gt;*/</span>
<span style="color: #0000ff;"&gt;public</span> <span style="color: #0000ff;"&gt;static</span> <span style="color: #0000ff;"&gt;boolean</span> insertPicture(String filePath,String picturePath,<span style="color: #0000ff;"&gt;int</span> rowNum,<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; cellNum) {
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 初始化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;"&gt;//</span><span style="color: #008000;"&gt; 先把读进来的<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;"&gt;new</span><span style="color: #000000;"&gt; ByteArrayOutputStream();

        bufferImg </span>= ImageIO.read(<span style="color: #0000ff;"&gt;new</span> File(picturePath));<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; <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;"&gt;//</span><span style="color: #008000;"&gt; 创建<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a>工作薄</span>
        HSSFWorkbook wb = <span style="color: #0000ff;"&gt;new</span> HSSFWorkbook(<span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; FileInputStream(filePath));
        HSSFSheet sheet1 </span>= wb.getSheetAt(0<span style="color: #000000;"&gt;);

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 创建插入<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>需要的容器</span>
        HSSFPatriarch patriarch =<span style="color: #000000;"&gt; sheet1.createDrawingPatriarch();
        </span><span style="color: #008000;"&gt;/*</span><span style="color: #008000;"&gt;
         * 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;"&gt;*/</span><span style="color: #000000;"&gt;
        HSSFClientAnchor anchor </span>= <span style="color: #0000ff;"&gt;new</span> HSSFClientAnchor(0,1023,255,(<span style="color: #0000ff;"&gt;short</span>) ((<span style="color: #0000ff;"&gt;short</span><span style="color: #000000;"&gt;) cellN),(rowN),(</span><span style="color: #0000ff;"&gt;short</span>) ((<span style="color: #0000ff;"&gt;short</span>) cellN + 1<span style="color: #000000;"&gt;),(rowN));

        anchor.setAnchorType(</span>2<span style="color: #000000;"&gt;);

        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 插入<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;"&gt;new</span><span style="color: #000000;"&gt; FileOutputStream(filePath);
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 写入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;"&gt; io.getMessage());
        </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #0000ff;"&gt;false</span><span style="color: #000000;"&gt;;

    } </span><span style="color: #0000ff;"&gt;finally</span><span style="color: #000000;"&gt; {

        </span><span style="color: #0000ff;"&gt;if</span> (fileOut != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {

            </span><span style="color: #0000ff;"&gt;try</span><span style="color: #000000;"&gt; {

                fileOut.close();

            } </span><span style="color: #0000ff;"&gt;catch</span><span style="color: #000000;"&gt; (IOException e) {

                e.printStackTrace();

            }
        }
    }

}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>