JXLS 图片

1.java代码

package com.knife.jxls;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

public class TestJxlsImage {

	private static byte[] toByteArray(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024 * 4];
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
		}
		return output.toByteArray();
	}
	
	public static void main(String args[]) throws IOException {
		
		InputStream imageInputStream = new FileInputStream("H:\\kuka\\test.png");
        byte[] imageBytes = toByteArray(imageInputStream);
        
        InputStream in = new FileInputStream("H:\\template.xlsx");
        OutputStream out = new FileOutputStream("H:\\test1.xlsx");
        Context context = new Context();
        context.putVar("imageBytes",imageBytes);
        
        JxlsHelper.getInstance().processTemplate(in, out, context);
		
	}

}

2.

编辑批注

jx:area(lastCell = "F15")
jx:image(lastCell="F15" src="imageBytes"  imageType="PNG")

3.结果

模板:

JXLS 图片_第1张图片

输出文件:

JXLS 图片_第2张图片

你可能感兴趣的:(java)