POI导出图片到Excel 用的Jar包是POI3.10 & JFreechart1.0.17

package test.peter.main;

import java.awt.Font;
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 java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

/**      
* 项目名称: Test 
* 类名称:    JfreeChartTest3   
* 类描述:    POI导出图片到Excel 用的Jar包是POI3.10 & JFreechart1.0.17
* 创建人:    Peter.Qiu   
* 创建时间:2014-3-18 下午03:50:09   
* 修改人:    Peter.Qiu   
* 修改时间:2014-3-18 下午03:50:09   
* 修改备注:     
*/
public class JfreeChartTest3 {
	private static FileOutputStream fileOut = null;
	private static BufferedImage bufferImg = null;
	//图片生成的路径
	private static String pathOfPicture = "D:/program.jpeg";
	//Excel生成的路径
	private static String pathOfExcel = "D:/test.xls";
	public static void main(String[] args) {
		//JFreeChart画图
		JFreeChart chart = ChartFactory.createPieChart3D("编程语言分布图", getDataset(), true, false, false);
		chart.setTitle(new TextTitle("编程语言分布图", new Font("仿宋", Font.BOLD, 20)));
		LegendTitle legend = chart.getLegend(0);
		legend.setItemFont(new Font("隶书", Font.TYPE1_FONT, 16));
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setLabelFont(new Font("宋体", Font.HANGING_BASELINE, 12));
		try {
			OutputStream os = new FileOutputStream(pathOfPicture);
			//OutputStream osExl = new FileOutputStream("测试.xls");
			try {
				// 由ChartUtilities生成文件到一个体outputStream中去
				ChartUtilities.writeChartAsJPEG(os, chart, 800, 600);
				//ChartUtilities.writeChartAsJPEG(osExl, chart, 100, 80);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//处理图片文件,以便产生ByteArray
		ByteArrayOutputStream handlePicture = new ByteArrayOutputStream();
		handlePicture = handlePicture(pathOfPicture);
		//创建一个工作簿
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("picture sheet");
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 50, (short) 0, 0, (short) 15, 30);
		//插入图片
		patriarch.createPicture(anchor, wb.addPicture(handlePicture.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
		//写入excel文件
		try {
			fileOut = new FileOutputStream(pathOfExcel);
			wb.write(fileOut);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fileOut != null) {
				try {
					fileOut.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	private static ByteArrayOutputStream handlePicture(String pathOfPicture) {
		ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
		try {
			bufferImg = ImageIO.read(new File(pathOfPicture));
			ImageIO.write(bufferImg, "jpeg", byteArrayOut);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return byteArrayOut;
	}
	private static DefaultPieDataset getDataset() {
		DefaultPieDataset dpd = new DefaultPieDataset();
		dpd.setValue("Java", 55);
		dpd.setValue("C/C++", 30);
		dpd.setValue("PHP", 10);
		dpd.setValue("其它", 15);
		return dpd;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为AI语言模型,我无法进行POI库的开发和使用,但是我可以提供Java代码的参考: 1. 导出图片Excel ```java // 创建Excel文档 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("图片示例"); // 加载图片 InputStream inputStream = new FileInputStream("image.jpg"); byte[] bytes = IOUtils.toByteArray(inputStream); int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); // 插入图片 CreationHelper helper = workbook.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(0); anchor.setRow1(0); Picture picture = drawing.createPicture(anchor, pictureIdx); // 输出Excel文件 FileOutputStream fos = new FileOutputStream("excel.xlsx"); workbook.write(fos); ``` 2. 压缩图片 ```java // 加载图片 InputStream inputStream = new FileInputStream("image.jpg"); byte[] bytes = IOUtils.toByteArray(inputStream); // 压缩图片 BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(bytes)); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); double scale = 0.5; width *= scale; height *= scale; BufferedImage compressedImage = new BufferedImage(width, height, bufferedImage.getType()); Graphics2D g = compressedImage.createGraphics(); g.drawImage(bufferedImage, 0, 0, width, height, null); g.dispose(); // 输出压缩后的图片 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(compressedImage, "jpg", baos); byte[] compressedBytes = baos.toByteArray(); FileOutputStream fos = new FileOutputStream("compressed.jpg"); fos.write(compressedBytes); ``` 你可以将以上代码结合起来,先将图片压缩,再将压缩后的图片插入Excel中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值