JAVA POI 图片插入excel保存导出,可多图,多种插入样式
import java
.awt
.image
.BufferedImage
;
import java
.io
.ByteArrayOutputStream
;
import java
.io
.File
;
import java
.io
.FileOutputStream
;
import java
.util
.Date
;
import javax
.imageio
.ImageIO
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFClientAnchor
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFPatriarch
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFRow
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFSheet
;
import org
.apache
.poi
.hssf
.usermodel
.HSSFWorkbook
;
import org
.apache
.poi
.xssf
.usermodel
.XSSFWorkbook
;
public class ExcelByImg {
public static void main(String
[] args
) {
exportExcelOne();
exportExcelTwo();
exportExcelThree();
exportExcelFour();
}
public static void exportExcelOne() {
String
[] imgs
= { "D:\\888.jpg", "D:\\666.jpg", "D:\\777.jpg" };
HSSFWorkbook workBook
= new HSSFWorkbook();
HSSFSheet sheet
= workBook
.createSheet();
sheet
.setColumnWidth(0, 4800);
BufferedImage bufferedImage
= null
;
HSSFPatriarch hSSFPatriarch
= sheet
.createDrawingPatriarch();
try {
for (int i
= 0; i
< imgs
.length
; i
++) {
HSSFRow imgRow
= sheet
.createRow(i
);
File file
= new File(imgs
[i
]);
imgRow
.setHeight((short) 1000);
bufferedImage
= ImageIO
.read(file
);
ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
ImageIO
.write(bufferedImage
, "jpg", byteArrayOutputStream
);
HSSFClientAnchor hSSFClientAnchor
= new HSSFClientAnchor(
20,
10,
1003,
245,
(short) 0,
i
,
(short) 0,
i
);
hSSFPatriarch
.createPicture(hSSFClientAnchor
,
workBook
.addPicture(byteArrayOutputStream
.toByteArray(), XSSFWorkbook
.PICTURE_TYPE_JPEG
));
}
FileOutputStream outputStream
= new FileOutputStream("D://" + new Date().getTime() + ".xls");
workBook
.write(outputStream
);
outputStream
.close();
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static void exportExcelTwo() {
String
[] imgs
= { "D:\\888.jpg", "D:\\666.jpg", "D:\\777.jpg" };
HSSFWorkbook workBook
= new HSSFWorkbook();
HSSFSheet sheet
= workBook
.createSheet();
BufferedImage bufferedImage
= null
;
HSSFPatriarch hSSFPatriarch
= sheet
.createDrawingPatriarch();
try {
HSSFRow imgRow
= sheet
.createRow(0);
imgRow
.setHeight((short) 1000);
for (int i
= 0; i
< imgs
.length
; i
++) {
sheet
.setColumnWidth(i
, 4800);
File file
= new File(imgs
[i
]);
bufferedImage
= ImageIO
.read(file
);
ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
ImageIO
.write(bufferedImage
, "jpg", byteArrayOutputStream
);
HSSFClientAnchor hSSFClientAnchor
= new HSSFClientAnchor(20, 10, 1003, 245, (short) i
, 0, (short) i
, 0);
hSSFPatriarch
.createPicture(hSSFClientAnchor
,
workBook
.addPicture(byteArrayOutputStream
.toByteArray(), XSSFWorkbook
.PICTURE_TYPE_JPEG
));
}
FileOutputStream outputStream
= new FileOutputStream("D://" + new Date().getTime() + ".xls");
workBook
.write(outputStream
);
outputStream
.close();
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static void exportExcelThree() {
String
[] imgs
= { "D:\\888.jpg", "D:\\666.jpg", "D:\\777.jpg" };
HSSFWorkbook workBook
= new HSSFWorkbook();
HSSFSheet sheet
= workBook
.createSheet();
sheet
.setColumnWidth(0, 4800 * imgs
.length
);
BufferedImage bufferedImage
= null
;
HSSFPatriarch hSSFPatriarch
= sheet
.createDrawingPatriarch();
try {
HSSFRow imgRow
= sheet
.createRow(0);
imgRow
.setHeight((short) 1000);
int mar
= 10 + 10 + (imgs
.length
- 1) * 10;
int ave
= (1023 - mar
) / imgs
.length
;
for (int i
= 0; i
< imgs
.length
; i
++) {
File file
= new File(imgs
[i
]);
bufferedImage
= ImageIO
.read(file
);
ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
ImageIO
.write(bufferedImage
, "jpg", byteArrayOutputStream
);
HSSFClientAnchor hSSFClientAnchor
= new HSSFClientAnchor(10 * (i
+ 1) + ave
* i
, 10,
(10 + ave
) * (i
+ 1), 245, (short) 0, 0, (short) 0, 0);
hSSFPatriarch
.createPicture(hSSFClientAnchor
,
workBook
.addPicture(byteArrayOutputStream
.toByteArray(), XSSFWorkbook
.PICTURE_TYPE_JPEG
));
}
FileOutputStream outputStream
= new FileOutputStream("D://" + new Date().getTime() + ".xls");
workBook
.write(outputStream
);
outputStream
.close();
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static void exportExcelFour() {
String
[] imgs
= { "D:\\888.jpg", "D:\\666.jpg", "D:\\777.jpg" };
HSSFWorkbook workBook
= new HSSFWorkbook();
HSSFSheet sheet
= workBook
.createSheet();
sheet
.setColumnWidth(0, 4800);
BufferedImage bufferedImage
= null
;
HSSFPatriarch hSSFPatriarch
= sheet
.createDrawingPatriarch();
try {
HSSFRow imgRow
= sheet
.createRow(0);
imgRow
.setHeight((short) (1000 * imgs
.length
));
int mar
= 5 + 5 + (imgs
.length
- 1) * 5;
int ave
= (255 - mar
) / imgs
.length
;
for (int i
= 0; i
< imgs
.length
; i
++) {
File file
= new File(imgs
[i
]);
bufferedImage
= ImageIO
.read(file
);
ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
ImageIO
.write(bufferedImage
, "jpg", byteArrayOutputStream
);
HSSFClientAnchor hSSFClientAnchor
= new HSSFClientAnchor(20, 5 * (i
+ 1) + ave
* i
, 1003,
(5 + ave
) * (i
+ 1), (short) 0, 0, (short) 0, 0);
hSSFPatriarch
.createPicture(hSSFClientAnchor
,
workBook
.addPicture(byteArrayOutputStream
.toByteArray(), XSSFWorkbook
.PICTURE_TYPE_JPEG
));
}
FileOutputStream outputStream
= new FileOutputStream("D://" + new Date().getTime() + ".xls");
workBook
.write(outputStream
);
outputStream
.close();
} catch (Exception e
) {
e
.printStackTrace();
}
}
}
4. 2. 1.