您的位置:首页 > 其它

通过Excel中的数据导入到图形报表(不太完整,不推荐使用)

2012-03-09 16:43 375 查看
package com.shu.sockets;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import com.shu.model.ExcelData;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class MyTest {
public static void main(String[] args) {
try {
Workbook book=Workbook.getWorkbook(new File("E:\\my.xls"));
Sheet[] sheets=book.getSheets();
List<ExcelData> list=new ArrayList<ExcelData>();
for(Sheet she : sheets){
//先声明第一维的长度
ExcelData ed=null;
for(int i=0;i<she.getColumns();i++){
ed=new ExcelData();
Cell[] cells=she.getColumn(i);
ed.setColName(cells[0].getContents());
ed.setColValue(Double.valueOf(cells[1].getContents()));
list.add(ed);
}

//Style of the theme
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
//Style of the font
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
//font of the list
standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
//style of tye x rax
standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15));
//style of the applicaion
ChartFactory.setChartTheme(standardChartTheme);

DefaultCategoryDataset data=new DefaultCategoryDataset();
for(ExcelData cel : list){
data.addValue(cel.getColValue(),"", cel.getColName());
}
JFreeChart jfc2=ChartFactory.createLineChart(she.getName(),"班级","数据", data,PlotOrientation.VERTICAL,true,true,false);
ChartUtilities.writeChartAsJPEG(new FileOutputStream("E://"+she.getName()+".jpg"), jfc2,list.size()*40,400);
}
book.close();
System.out.println("done~");
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: