引入的包
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.ss.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
测试类
public class MyPoi {
public void test() {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
BufferedImage bufferImg1 = null;
try {
// 读取图片
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("e:/nanjing.jpg"));
bufferImg1 = ImageIO.read(new File("e:/nanjing.jpg"));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
ImageIO.write(bufferImg1, "jpg", byteArrayOut1);
// 创建excel
HSSFWorkbook excelfile = new HSSFWorkbook();
HSSFSheet excelsheet = excelfile.createSheet();
excelfile.setSheetName(0, "aaa");
// 设置图片位置
HSSFPatriarch patriarch = excelsheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,
(short) 1, 1, (short) 10, 20);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 512, 255,
(short) 2, 30, (short) 10, 60);
patriarch
.createPicture(anchor, excelfile.addPicture(
byteArrayOut.toByteArray(),
HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1,
excelfile.addPicture(byteArrayOut1.toByteArray(),
HSSFWorkbook.PICTURE_TYPE_JPEG));
// 输出到磁盘
fileOut = new FileOutputStream("E:/1/t"
+ System.currentTimeMillis() + ".xls");
excelfile.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
}
public void testChart() {
}
public static void main(String[] args) {
new MyPoi().test();
}
}
效果图