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;
}
}