import java.io.FileOutputStream
import java.io.File
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.awt.image.BufferedImage
import javax.imageio.*
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFPatriarch
import org.apache.poi.hssf.usermodel.HSSFClientAnchor
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null
BufferedImage bufferImg =null
BufferedImage bufferImg1 = null
try{
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream()
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream()
bufferImg = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"))
bufferImg1 = ImageIO.read(new File("C:/Documents and Settings/dingqi/Desktop/clip_image002.jpg"))
ImageIO.write(bufferImg,"jpg",byteArrayOut)
ImageIO.write(bufferImg1,"jpg",byteArrayOut1)
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook()
HSSFSheet sheet1 = wb.createSheet("poi picT")
//HSSFRow row = sheet1.createRow(2)
HSSFPatriarch patriarch = sheet1.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)
anchor1.setAnchorType(2)
//插入图片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG))
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG))
fileOut = new FileOutputStream("c:/workbook.xls")
//写入excel文件
wb.write(fileOut)
fileOut.close()
}catch(IOException io){
io.printStackTrace()
System.out.println("io erorr : "+ io.getMessage())
} finally
{
if (fileOut != null)
{
try {
fileOut.close()
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
}
poi中图片到到excel的方法 你需要准备poi包 试试看看
package teiimport java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.ArrayList
import java.util.List
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
public class TestExcelImage {
static List<BufferedImage>images = new ArrayList<>()
static {
try {
images.add(ImageIO.read(new File("C:/t/1.jpg")))
images.add(ImageIO.read(new File("C:/t/2.jpg")))
images.add(ImageIO.read(new File("C:/t/3.jpg")))
images.add(ImageIO.read(new File("C:/t/4.jpg")))
images.add(ImageIO.read(new File("C:/t/5.jpg")))
images.add(ImageIO.read(new File("C:/t/6.jpg")))
images.add(ImageIO.read(new File("C:/t/7.jpg")))
images.add(ImageIO.read(new File("C:/t/8.jpg")))
} catch (IOException e) {
e.printStackTrace()
}
}
public static void main(String[] args) {
FileOutputStream fileOut = null
try {
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook()
HSSFSheet sheet1 = wb.createSheet("new sheet")
// HSSFRow row = sheet1.createRow(2)
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch()
short i = 0
for (BufferedImage image : images) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream()
ImageIO.write(image, "jpg", byteArrayOut)
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1 + i, (short) 2, 2 + i)
anchor.setAnchorType(0)
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG))
i++
}
HSSFRow row = sheet1.createRow(10)
short s = 10
HSSFCell cell = row.createCell(s)
HSSFCellStyle style = wb.createCellStyle()
HSSFFont font = wb.createFont()
font.setStrikeout(true)
style.setFont(font)
cell.setCellStyle(style)
cell.setCellValue("aaaaa")
fileOut = new FileOutputStream("c:/workbook.xls")
// 写入excel文件
wb.write(fileOut)
fileOut.close()
} catch (IOException io) {
io.printStackTrace()
System.out.println("io erorr : " + io.getMessage())
} finally {
if (fileOut != null) {
try {
fileOut.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
}
简单的写了下。 我机器上可以运行
输出图片的base64编码
//imgFile是图片的路径public static void getImageStr(String imgFile) {
InputStream inputStream = null
byte[] data = null
try {
inputStream = new FileInputStream(imgFile)
data = new byte[inputStream.available()]
inputStream.read(data)
inputStream.close()
} catch (IOException e) {
e.printStackTrace()
} // 加密
BASE64Encoder encoder = new BASE64Encoder()
System.out.println(encoder.encode(data))
}