您的位置:首页 > 其它

教你如何利用POI和JFreeChart框架实现生成Excel文件(生成 统计图)

2014-12-16 10:18 435 查看
 首先,我介绍一下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图形解决方案,基本能够解决目前的图形方面的需求

 

 

接下来我就展示我自己写的代码,我试过肯定可以运行的:

[java] view
plaincopyprint?

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;  

    }  

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jfreechart