Java Bulk Import Pictures to Excel

Source: Internet
Author: User

1 package com.sun.office.excel;
2
3 Import Java.awt.image.BufferedImage;
4 Import Java.io.ByteArrayOutputStream;
5 Import Java.io.File;
6 Import Java.io.FileOutputStream;
7 Import java.io.IOException;
8
9 Import Javax.imageio.ImageIO;
10
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;
15
public class Excelimagedemo {
17
public static void Main (string[] args) {
FileOutputStream fileout = null;
BufferedImage bufferimg = null;
21//First put the read in a bytearrayoutputstream in order to produce ByteArray
try {
Bytearrayoutputstream bytearrayout = new Bytearrayoutputstream ();
bufferimg = Imageio.read (New File ("d:/ubuntn.jpg"));
Imageio.write (bufferimg, "JPG", bytearrayout);
26
Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet Sheet1 = wb.createsheet ("Test picture");
29//Paint top manager, one sheet can only get one (be sure to note this)
Hssfpatriarch Patriarch = Sheet1.createdrawingpatriarch ();
for (int i = 0; I < 3; i++) {
+//anchor mainly used to set the properties of a picture
Hssfclientanchor anchor = new Hssfclientanchor (0, 0, 1023, +, (short) 1, 1+i*10, (short) 5, 8+i*10);
34//Insert Picture
Patriarch.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg));
36}
PNS Hssfsheet Sheet2 = Wb.createsheet ("Test Picture2");
38//Second sheet page
Hssfpatriarch PATRIARCH2 = Sheet2.createdrawingpatriarch ();
(int i = 0; I < 3; i++) {
$//anchor is used primarily to set the properties of a picture
Hssfclientanchor anchor = new Hssfclientanchor (0, 0, 1023, +, (short) 1, 1+i*10, (short) 5, 8+i*10);
43//Insert Picture
Patriarch2.createpicture (anchor, Wb.addpicture (Bytearrayout.tobytearray (), hssfworkbook.picture_type_jpeg));
45}
Fileout = new FileOutputStream ("Excel.xls");
47//write to Excel file
Wb.write (fileout);
SYSTEM.OUT.PRINTLN ("----excle file has been generated------");
\ catch (Exception e) {
Wuyi E.printstacktrace ();
The #} finally {
if (fileout! = null) {
try {
Fileout.close ();
(IOException e) {
E.printstacktrace ();
58}
59}
60}
61}
62//About the parameters of Hssfclientanchor (DX1,DY1,DX2,DY2,COL1,ROW1,COL2,ROW2), it is necessary to explain here:
DX1: The x offset of the starting cell,
//dy1: The y offset of the starting cell,
×//DX2: The x offset of the terminating cell,
Dy2: The y offset of the terminating cell,
Col1: Column ordinal of the starting unit, calculated from 0;
Row1: The Starting cell row ordinal, calculated starting from 0,
Col2: The Terminating cell column number, calculated from 0;
Row2: Terminates the cell row ordinal, calculated from 0,
71//When adding multiple images: Multiple pic should share the same drawingpatriarch in the same sheet.
72}

Java Bulk Import Pictures to Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.