首先,我介紹一下POI和JFreeChart吧,大家先上官網把這兩個框架的jar包下載下來。我也打包上傳了這兩個jar放到了CSDN上 歡迎大家去下載。地址是 http://yangchao228.download.csdn.net/
Apache POI是Apache軟件基金會的開放源碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。
結構:
HSSF - 提供讀寫Microsoft Excel格式檔案的功能。
XSSF - 提供讀寫Microsoft Excel OOXML格式檔案的功能。
HWPF - 提供讀寫Microsoft Word格式檔案的功能。
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。
HDGF - 提供讀寫Microsoft Visio格式檔案的功能。
可以創建Excel 文檔等。
JFreeChart是JAVA平臺上的一個開放的圖表繪製類庫。它完全使用JAVA語言編寫,是爲applications, applets, servlets 以及JSP等使用所設計。JFreeChart可生成餅圖(pie charts)、柱狀圖(bar charts)、散點圖(scatter plots)、時序圖(time series)、甘特圖(Gantt charts)等等多種圖表,並且可以產生PNG和JPEG格式的輸出,還可以與PDF和EXCEL關聯JFreeChart截止2011年2月22日爲止的相當不錯的java圖形解決方案,基本能夠解決目前的圖形方面的需求
接下來我就展示我自己寫的代碼,我試過肯定可以運行的:
- package com.tudo.cyang.test;
- 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;
- /**
- * 由JFreeChart生成圖片放到硬盤上
- * 對於Swing程序可以由org.jfree.chart.ChartUtilities類完成圖片的生成
- *
- * @author
- *
- */
- public class JfreeChartTest3 {
- private static FileOutputStream fileOut = null;
- private static BufferedImage bufferImg = null;
- //圖片生成的路徑
- private static String pathOfPicture = "E:/workspace/ExcelDemo/company.jpeg";
- //Excel生成的路徑
- private static String pathOfExcel = "E:/workspace/ExcelDemo/test.xls";
- public static void main(String[] args) {
- //JFreeChart畫圖
- JFreeChart chart = ChartFactory.createPieChart("某公司組織結構圖", 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, 1000, 800);
- //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) 1, 1, (short) 10, 20);
- //插入圖片
- 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("管理人員", 25);
- dpd.setValue("市場人員", 10);
- dpd.setValue("開發人員", 50);
- dpd.setValue("其它人員", 15);
- return dpd;
- }
- }