JAVA POI 图片插入excel保存导出,可多图,多种插入样式
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) {
// 1.竖向排列
exportExcelOne();
// 2.横向排列
exportExcelTwo();
// 3. 同一个单元格横向排列
exportExcelThree();
// 4.同一个单元格竖向排列
exportExcelFour();
}
/**
* 1.竖向排列
*/
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, /* 开始点 x坐标,范围 0-1023 */
10, /* 开始点y坐标范.围 0-255 */
1003, /* 结束点x坐标,范围 0-1023 */
245, /* 结束点y坐标,范围 0-255 */
(short) 0, /* 开始单元格列号,范围0-255 */
i, /* 开始单元格行号,范围0-255 * 256 */
(short) 0, /* 结束单元格列号,范围0-255 */
i/* 结束单元格行号,范围0-255 * 256 */
);
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();
}
}
/**
* 2.横向排列
*/
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();
}
}
/**
* 3. 同一个单元格横向排列
*/
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();
}
}
/**
* 4.同一个单元格竖向排列
*/
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.