一共写了三个类:Tux.java,MyExcel.java,Zhift.java.
package mul;
import java.awt.Color;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Tux extends JFrame implements ActionListener{
JTextField wenb = null;
private FileDialog openFileDialog = null;
MenuBar menuBar = new MenuBar();
Menu file = new Menu("文件");
MenuItem[] menuItem = {
new MenuItem("打开"),
new MenuItem("退出")
};
String fileName = null;
MyExcel myExcel = null;
Image tup = null;
public Tux(){
Container con = this.getContentPane();
setTitle("生成直方图");
GridBagLayout gr = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
con.setLayout(gr);
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 11; gc.weighty = 11;
openFileDialog = new FileDialog(this, "打开excel文件",FileDialog.LOAD);
setFont(new Font("宋体",Font.PLAIN,15));
setBackground(Color.WHITE);
setMenuBar(menuBar);
menuBar.add(file);
for(int i = 0; i < 2; i++)
file.add(menuItem[i]);
for(int j=0;j<2;j++)
menuItem[j].addActionListener(this);
wenb = new JTextField(20);
wenb.setText("test");
gc.gridx = 0;
gc.gridy = 11;
gc.fill = GridBagConstraints.RELATIVE;
gr.setConstraints(wenb, gc);
// JTextField xint = new JTextField(30);
// xint.setText("图表");
// gc.fill = GridBagConstraints.REMAINDER;
//
// gr.setConstraints(xint, gc);
add(wenb);
// add(xint);
setLocation(100,100);
setSize(500, 500);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Tux();
}
@Override
public void actionPerformed(ActionEvent e) {
Object eventSource = e.getSource();
if(eventSource == menuItem[0]){
openFileDialog.setVisible(true);
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
openFile(fileName);
}else if(eventSource == menuItem[1]){
System.exit(0);
}
repaint();
}
public void openFile(String wenjm) {
System.out.println(wenjm);
File f = new File(wenjm);
if(f != null){
this.myExcel = new MyExcel(f);
myExcel.guanb();
}
}
public void repaint(){
super.repaint();
if(myExcel!=null)this.tup = myExcel.zhift.img;
Container con = this.getContentPane();
setTitle("生成直方图");
GridBagLayout gr = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
con.setLayout(gr);
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 11; gc.weighty = 11;
openFileDialog = new FileDialog(this, "打开excel文件",FileDialog.LOAD);
setFont(new Font("宋体",Font.PLAIN,15));
setBackground(Color.WHITE);
setMenuBar(menuBar);
menuBar.add(file);
for(int i = 0; i < 2; i++)
file.add(menuItem[i]);
for(int j=0;j<2;j++)
menuItem[j].addActionListener(this);
wenb = new JTextField(20);
wenb.setText("test");
gc.gridx = 0;
gc.gridy = 11;
gc.fill = GridBagConstraints.RELATIVE;
gr.setConstraints(wenb, gc);
add(wenb);
JLabel tup_biaoq = new JLabel(new ImageIcon(this.tup));
add(tup_biaoq);
setLocation(100,100);
setSize(800, 800);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package mul;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class MyExcel {
/**
* @param args
*/
File wenj = null;
FileInputStream fis = null;
String[] rowInfo = null;
String[] colInfo = null;
double[][] dataInfo = null;
public Zhift zhift = null;
public MyExcel(File f){
wenj = f;
try {
fis = new FileInputStream(wenj);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HSSFSheet s = null;
s = wb.getSheetAt(0);
HSSFRow r = null;
r = s.getRow(0);
int cols = r.getLastCellNum();
cols = cols -1;
HSSFCell danyg = null;
rowInfo = new String[cols];
int rows = s.getLastRowNum();
System.out.println("rows="+rows+" cols="+cols);
// System.exit(0);
for(short i = 1; i <= cols; i++){
danyg = r.getCell(i);
if(danyg.getCellType()==HSSFCell.CELL_TYPE_STRING){
// System.out.println(danyg.getRichStringCellValue());
rowInfo[i-1] = danyg.getRichStringCellValue().getString();
System.out.println(rowInfo[i-1]);
}
}
colInfo = new String[rows];
for(short j = 1; j <= rows; j++){
r = s.getRow(j);
danyg = r.getCell((short)0);
if(danyg.getCellType()==HSSFCell.CELL_TYPE_STRING){
// System.out.println(danyg.getRichStringCellValue());
colInfo[j-1]=danyg.getRichStringCellValue().getString();
System.out.println(colInfo[j-1]);
}
}
Double shuj = null;
System.out.println("rows:"+rows+" cols:"+cols);
dataInfo = new double[rows][];
for(short ii = 0; ii < rows; ii++)
dataInfo[ii] = new double[cols];
for(short p = 1; p <= rows; p++){
for(short q = 1; q <= cols;q++){
r = s.getRow(p);
danyg = r.getCell(q);
if(danyg.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
shuj = new Double((Double)danyg.getNumericCellValue());
dataInfo[p-1][q-1] = shuj;
System.out.println(dataInfo[p-1][q-1]);
}
}
}
this.zhift = new Zhift(rowInfo,rows,colInfo,cols,dataInfo);
// HSSFCell cell = r.getCell((short)0);
//
// if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
// System.out.println(cell.getRichStringCellValue());
}
public void guanb(){
try {
if(fis != null)
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package mul;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
public class Zhift extends JFrame{
String[] rowInfo = null;
String[] colInfo = null;
double[][] dataInfo = null;
ChartFrame frame = null;
Image img = null;
public Zhift(String[] row_cans,int hangs, String[] col_cans,int lies, double[][] data_cans){
if(row_cans!=null){
rowInfo = new String[lies];
rowInfo=row_cans;
}
if(col_cans!=null){
colInfo = new String[hangs];
colInfo=col_cans;
}
if(data_cans!=null){
dataInfo = new double[hangs][];
for(int i=0;i<hangs;i++)
dataInfo[i] = new double[lies];
dataInfo = data_cans;
}
System.out.println("-------------------------------------------");
System.out.println("hangs="+hangs+" lies="+lies);
for(int i = 0;i<lies; i++)
System.out.println(rowInfo[i]);
for(int j=0;j<hangs;j++)
System.out.println(colInfo[j]);
for(int p=0;p<hangs;p++)
for(int q=0;q<lies;q++)
System.out.println(dataInfo[p][q]);
CategoryDataset data = DatasetUtilities.createCategoryDataset(rowInfo, colInfo, dataInfo);
JFreeChart chart = ChartFactory.createBarChart3D("测试直方图", "row", "col", data, PlotOrientation.VERTICAL, true, false, false);
chart.setAntiAlias(true);
chart.setBackgroundPaint(Color.WHITE);
chart.setBorderVisible(true);
CategoryPlot plot = chart.getCategoryPlot();
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.RED);
plot.setBackgroundPaint(Color.CYAN);
CategoryAxis axis = plot.getDomainAxis();
axis.setAxisLinePaint(Color.RED);
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
axis.setTickMarksVisible(true);
axis.setAxisLineVisible(true);
BarRenderer3D render = new BarRenderer3D();
//render.setItemLabelsVisible(true);
render.setBaseOutlinePaint(Color.BLACK);
plot.setRenderer(render);
plot.setForegroundAlpha(0.8f);
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体",Font.PLAIN,20));
// this.frame = new ChartFrame("导出直方图", chart, true);
// frame.pack();
//
// frame.setVisible(true);
String fileName = "E:\\zhift.jpeg";
try {
ChartUtilities.saveChartAsJPEG(new File(fileName), chart, 600,600);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
img = ImageIO.read(new File(fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("直方图生成成功");
}
}