代码大概三百行吧,不多。本来连接数据库做是个不错的选择,但是我刚刷了系统木有了又懒得装,从txt输入又感觉太low,最后就作死选择了以前从未尝试过的从Excel输入,并碰到了各种问题_(:зゝ∠)_比如Cannot get a String value from a numeric cell的异常错误,卡了我好久,直到无语地发现POI操作Excel时会无视Excel里的单元格设置,自判数据类型,所以还要使用setCellType.(*゜ー゜*)
(1)我用javax.swing.JFileChooser类来显示文件对话框,让用户选择一个Excel文件。
(2)使用Apache POI API从Excel文件中读取数据,对Employee类进行批量初始化。
(3)实现公司员工收入的相关统计,这里我实现了人数统计、最大/最小工龄查找、最大/最小工资查找、男女比例、平均工资、平均年龄、平均工龄等。
(4)建立Dataset。将你所想要显示的数据都放到这个库中。
(5)建立JFreeChart对象。将你的dataset填入到这个对象中。
(6)处理Chart中文显示问题
(7)设置各种JFreeChart的属性和效果。通过它提供的各种方法和接口设置相关的属性。
(8)用JFreeChart绘制图表,然后按照个人的需求进行执行。
(9)写界面,以swing形式输出。
(1)生成消息对话框,要求用户选择一个Excel文件,单击“确定”
(2)生成文件选择器,我打开了存放在D盘BUAA文件夹里的Employee.xls
(4)询问用户是否生成统计图,如果点“取消”或“否”,运行完毕;如果点是,系统将生成统计图,以员工为横轴,以年龄、工龄、工资三项数值为纵轴。
图表的显示效果会根据窗口大小自适应,改变横纵轴的比例尺。
下面是窗口最大化的效果
import java.awt.RenderingHints;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
//org.apache.poi
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//org.jfree
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import java.awt.Font;
public class EmployeeStatics {
public static void main(String[] args) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"\n您好,我是智能机器人小紫\n\n"
+ "很高兴为您提供员工数据统计分析服务\n\n"
+ "请选择一个Excel文件 ^_^\n\n",
"选择Excel文件",JOptionPane.INFORMATION_MESSAGE);
ArrayList
Employees = null;
//从Excel文件读取数据
try {
Employees = ReadFileUsingFileChooser();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//人数统计
int n = Employees.size();
//最大最小工资、工龄、工资初始化
int maxA = 0;
int minA = Integer.MAX_VALUE;
int maxWA = 0;
int minWA = Integer.MAX_VALUE;
int maxS = 0;
int minS = Integer.MAX_VALUE;
//男女性别人数
int M = 0;
int FM = 0;
double sumS, sumA, sumWA, avgS, avgA, avgWA, MFM;
sumS = sumA = sumWA = 0;
//遍历实现相关统计
for (Employee e : Employees) {
if(e.age > maxA)
maxA = (int) e.age;
if(e.age < minA)
minA = (int) e.age;
if(e.workAge > maxWA)
maxWA = (int) e.workAge;
if(e.workAge < minWA)
minWA = (int) e.workAge;
if(e.sal > maxS)
maxS = (int) e.sal;
if(e.sal < minS)
minS = (int) e.sal;
sumS += e.sal;
sumA += e.age;
sumWA += e.workAge;
if (e.sex.equals("男"))
M++;
if (e.sex.equals("女"))
FM++;
}
//计算平均值
avgS = sumS / n;
avgA = sumA / n;
avgWA = sumWA / n;
//计算男女比例
MFM = (double) M / FM;
JOptionPane.showMessageDialog(null,"员工人数为" + n + ",男女比例为" + MFM + "(男/女)\n"
+ "平均年龄为" + avgA + ",平均工龄为" + avgWA + ",平均工资为" + avgS + "\n"
+ "最高年龄为" + maxA + ",最低年龄为" + minA + ",最高工龄为" + maxWA + ",最低工龄为" + minWA + "\n"
+ "最高工资为" + maxS + "K,最低工资为" + minS + "K\n");
int option=JOptionPane.YES_OPTION;
option=JOptionPane.showConfirmDialog(null, "是否显示员工数据统计图?");
if (option==JOptionPane.YES_OPTION) {
CategoryDataset dataset = getDataSet(Employees);
//构造chart
JFreeChart chart = ChartFactory.createBarChart3D(
"员工数据统计图", // 图表标题
"员工属性", // 目录轴的显示标签--横轴
"数值", // 数值轴的显示标签--纵轴
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、
true, // 是否显示图例(对于简单的柱状图必须
false, // 是否生成工具
false // 是否生成URL链接
);
//处理chart中文显示问题
processChart(chart);
//chart 以swing形式输出
ChartFrame pieFrame = new ChartFrame("员工数据统计图", chart);
pieFrame.pack();
pieFrame.setVisible(true);
}
}
public static ArrayList
ReadFileUsingFileChooser() throws Exception {
// TODO Auto-generated method stub
JFileChooser fileChooser = new JFileChooser();
ArrayList temp = new ArrayList();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
java.io.File file = fileChooser.getSelectedFile();
FileInputStream fileIn = new FileInputStream(file);
//根据指定的文件输入流导入Excel从而产生Workbook对象
HSSFWorkbook wb0 = new HSSFWorkbook(fileIn);
//获取Excel文档中的第一个表单
HSSFSheet sht0 = wb0.getSheetAt(0);
//对Sheet中的每一行进行迭代
int r;
int rowNum = sht0.getPhysicalNumberOfRows();
for (r = 1; r <= rowNum; r++) {
Row row = sht0.getRow(r);
if (row == null) {
break;
}
//创建实体类
Employee info=new Employee();
//取出当前行第1个单元格数据,并封装在info实体stuName属性上
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
info.setID(row.getCell(0).getStringCellValue());
info.setName(row.getCell(1).getStringCellValue());
info.setSex(row.getCell(2).getStringCellValue());
info.setAge(row.getCell(3).getNumericCellValue());
info.setWorkAge(row.getCell(4).getNumericCellValue());
info.setSal(row.getCell(5).getNumericCellValue());
temp.add(info);
}
fileIn.close();
}
else {
System.out.println("No file selected");
}
return temp;
}
//获取一个演示用的组合数据集对象
private static CategoryDataset getDataSet(ArrayList
Employees) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (Employee e : Employees){
dataset.addValue(e.workAge, "工龄", e.name);
dataset.addValue(e.sal, "工资", e.name);
dataset.addValue(e.age, "年龄", e.name);
}
return dataset;
}
//解决图表汉字显示问题
private static void processChart(JFreeChart chart) {
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
ValueAxis rAxis = plot.getRangeAxis();
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
// renderer.setItemLabelGenerator(new LabelGenerator(0.0));
// renderer.setItemLabelFont(new Font("宋体", Font.PLAIN, 12));
// renderer.setItemLabelsVisible(true);
}
}
public class Employee {
String ID;
String name;
String sex;
double age;
double workAge;
double sal;
public void setID(String ID) {
this.ID = ID;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(double age) {
this.age = age;
}
public void setWorkAge(double workAge) {
this.workAge = workAge;
}
public void setSal(double sal) {
this.sal = sal;
}
}
一共有两个:Employee.java和EmployeeStatic.java
importjava.awt.RenderingHints;
importjava.io.FileInputStream;
importjava.util.ArrayList;
import java.util.List;
importjavax.swing.JFileChooser;
importjavax.swing.JOptionPane;
//org.apache.poi
importorg.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.ss.usermodel.Cell;
importorg.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//org.jfree
importorg.jfree.chart.ChartFactory;
importorg.jfree.chart.ChartFrame;
importorg.jfree.chart.JFreeChart;
importorg.jfree.chart.axis.CategoryAxis;
importorg.jfree.chart.axis.ValueAxis;
importorg.jfree.chart.plot.CategoryPlot;
importorg.jfree.chart.plot.PlotOrientation;
importorg.jfree.chart.title.TextTitle;
importorg.jfree.data.category.CategoryDataset;
importorg.jfree.data.category.DefaultCategoryDataset;
importjava.awt.Font;
public class EmployeeStatics {
public static void main(String[] args) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"\n您好,我是智能机器人小紫\n\n"
+ "很高兴为您提供员工数据统计分析服务\n\n"
+ "请选择一个Excel文件 ^_^\n\n",
"选择Excel文件",JOptionPane.INFORMATION_MESSAGE);
ArrayList
//从Excel文件读取数据
try {
Employees = ReadFileUsingFileChooser();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//人数统计
int n = Employees.size();
//最大最小工资、工龄、工资初始化
int maxA = 0;
int minA = Integer.MAX_VALUE;
int maxWA = 0;
int minWA = Integer.MAX_VALUE;
int maxS = 0;
int minS = Integer.MAX_VALUE;
//男女性别人数
int M = 0;
int FM = 0;
double sumS, sumA, sumWA, avgS, avgA, avgWA, MFM;
sumS = sumA = sumWA = 0;
//遍历实现相关统计
for (Employee e : Employees) {
if(e.age > maxA)
maxA = (int) e.age;
if(e.age < minA)
minA = (int) e.age;
if(e.workAge > maxWA)
maxWA = (int) e.workAge;
if(e.workAge < minWA)
minWA = (int) e.workAge;
if(e.sal > maxS)
maxS = (int) e.sal;
if(e.sal < minS)
minS = (int) e.sal;
sumS += e.sal;
sumA += e.age;
sumWA += e.workAge;
if (e.sex.equals("男"))
M++;
if (e.sex.equals("女"))
FM++;
}
//计算平均值
avgS = sumS / n;
avgA = sumA / n;
avgWA = sumWA / n;
//计算男女比例
MFM = (double) M / FM;
JOptionPane.showMessageDialog(null,"员工人数为" + n + ",男女比例为" + MFM + "(男/女)\n"
+ "平均年龄为" + avgA + ",平均工龄为" + avgWA + ",平均工资为" + avgS + "\n"
+ "最高年龄为" + maxA + ",最低年龄为" + minA + ",最高工龄为" + maxWA + ",最低工龄为" + minWA + "\n"
+ "最高工资为" + maxS + "K,最低工资为" + minS + "K\n");
int option=JOptionPane.YES_OPTION;
option=JOptionPane.showConfirmDialog(null, "是否显示员工数据统计图?");
if (option==JOptionPane.YES_OPTION) {
CategoryDataset dataset = getDataSet(Employees);
//构造chart
JFreeChart chart = ChartFactory.createBarChart3D(
"员工数据统计图", // 图表标题
"员工属性", // 目录轴的显示标签--横轴
"数值", // 数值轴的显示标签--纵轴
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、
true, // 是否显示图例(对于简单的柱状图必须
false, // 是否生成工具
false // 是否生成URL链接
);
//处理chart中文显示问题
processChart(chart);
//chart 以swing形式输出
ChartFrame pieFrame = new ChartFrame("员工数据统计图", chart);
pieFrame.pack();
pieFrame.setVisible(true);
}
}
public static ArrayList
// TODO Auto-generated method stub
JFileChooser fileChooser = new JFileChooser();
ArrayList temp = new ArrayList();
if (fileChooser.showOpenDialog(null) ==JFileChooser.APPROVE_OPTION) {
java.io.File file = fileChooser.getSelectedFile();
FileInputStream fileIn = new FileInputStream(file);
//根据指定的文件输入流导入Excel从而产生Workbook对象
HSSFWorkbook wb0 = new HSSFWorkbook(fileIn);
//获取Excel文档中的第一个表单
HSSFSheet sht0 = wb0.getSheetAt(0);
//对Sheet中的每一行进行迭代
int r;
int rowNum = sht0.getPhysicalNumberOfRows();
for (r = 1; r <= rowNum; r++) {
Row row = sht0.getRow(r);
if (row == null) {
break;
}
//创建实体类
Employee info=new Employee();
//取出当前行第1个单元格数据,并封装在info实体stuName属性上
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
info.setID(row.getCell(0).getStringCellValue());
info.setName(row.getCell(1).getStringCellValue());
info.setSex(row.getCell(2).getStringCellValue());
info.setAge(row.getCell(3).getNumericCellValue());
info.setWorkAge(row.getCell(4).getNumericCellValue());
info.setSal(row.getCell(5).getNumericCellValue());
temp.add(info);
}
fileIn.close();
}
else {
System.out.println("No file selected");
}
return temp;
}
//获取一个演示用的组合数据集对象
private static CategoryDatasetgetDataSet(ArrayList
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (Employee e : Employees){
dataset.addValue(e.workAge, "工龄", e.name);
dataset.addValue(e.sal, "工资", e.name);
dataset.addValue(e.age, "年龄", e.name);
}
return dataset;
}
//解决图表汉字显示问题
private static void processChart(JFreeChart chart) {
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
ValueAxis rAxis = plot.getRangeAxis();
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
rAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
// renderer.setItemLabelGenerator(newLabelGenerator(0.0));
// renderer.setItemLabelFont(newFont("宋体", Font.PLAIN, 12));
//renderer.setItemLabelsVisible(true);
}
}
public class Employee {
String ID;
String name;
String sex;
double age;
double workAge;
double sal;
public void setID(String ID) {
this.ID = ID;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(double age) {
this.age = age;
}
public void setWorkAge(double workAge) {
this.workAge = workAge;
}
public void setSal(double sal) {
this.sal = sal;
}
}