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;
}
}
分享到:
相关推荐
java导出excel POI jar包 java代码地址http://blog.csdn.net/awenluck/article/details/51488537
java导出excel方式,含POI3.10jar包,都可以正常应用到项目中。
java,poi方式解析导出excel需要使用的三个jar包。
导出excel文档所需要的poi的jar包,包含poi-3.8.jar,poi-ooxml-3.8.jar,poi-ooxml-schemas-3.8.jar
基于poi导出Excel所需要的jar包,做Excel时候没少寻找
使用poi生成Excel所需要的jar包,poi-3.9.jar
利用POI技术将图片插入到excel中,希望对大家有用,直接运行ExportExcel类,即可看到效果!!!
POI操作excel的需要的所有jar包,包含XSSF和SXSSF操作2007.xlsx文档所依赖的jar包(3.7-3.10)+dom4j.jar+xmlbean.jar
springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式Excel模板,完整项目,导出即用。springboot+poi导出指定格式...
POI导出EXCEL经典实现必备jar包,利用poi可以轻松使用java语言将Excle导入导出数据库
主要介绍java poi如何导出图片到excel,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
poi导入导出Excel表格的所用jar包,包括poi-3.8-20120326 等jar包
里面包含了3.8版3.9版3.10版的所有的poi的jar包 这只是3.9版的 poi-3.9.jar poi-ooxml-3.9.jar dom4j-1.6.1.jar poi-ooxml-schemas-3.9.jar xmlbeans-2.3.0.jar poi-ooxml-3.9-sources.jar
poi操作excel 实现导入导出
java poi组件导出word和excel,都是main方法写的例子,直接运行即可,附带最新版3.10必须的3个jar包
文件包含了 1.POI导入导出的工具类; 2.图片URL导出成图片文件; 3.导出excel和图片压缩包格式文件。 如果下载后有任何问题,可以联系我哦!
poi就是批量的操作文件或数据的导入以及导出。,poi导入excel表需要的jar
使用java语言导出数据到excel表(xls格式)所必须的jar包
java使用poi操作excel需要的所有jar包(poi3.8版本),里面有poi-3.8-20120326.jar、poi-ooxml-3.8-20120326.jar、poi-ooxml-schemas-3.8-20120326.jar、dom4j-1.6.1.jar、xmlbeans-2.3.0.jar
poi导出excel需要的jar