当前位置:   article > 正文

JAVA POI 图片插入excel保存导出,可多图,多种插入样式_java 把多张照片导出到一个excel单元格中

java 把多张照片导出到一个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();
		}

	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172

在这里插入图片描述
4.
在这里插入图片描述
2.
在这里插入图片描述
1.
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/273037
推荐阅读
相关标签
  

闽ICP备14008679号