java如何读取excel中报表(柱状图)的信息

Python015

java如何读取excel中报表(柱状图)的信息,第1张

public class Report {

/**作用:报表样式的枚举,该类的getvalue方法返回对应值。<br>

* Columns_2DVer:2D柱状垂直报表。<br>

* Columns_3DVer:3D柱状垂直报表。<br>

* Columns_2DHor:2D柱状水平报表。<br>

* Columns_3DHor:3D柱状水平报表。

* */

public enum ReportType{

Columns_2DVer("Z2DV"),

Columns_3DVer("Z3DV"),

Columns_2DHor("Z2DH"),

Columns_3DHor("Z3DH"),

LineReport_2D("Line2D"),

LineReport_3D("Line3D"),

PieReport_2D("Pie2D"),

PieReport_3D("Pie3D")

private ReportType(String a){

this.str=a

}

private String str

public String getvalue(){

return this.str

}

}

/**作用:作为创建报表时的参数。<br>

* 成员1:报表的标题。<br>

* 成员2:报表的横坐标标题。<br>

* 成员3:报表的纵坐标标题。<br>

* 成员4:JSP页面的request对象。<br>

* 成员5:报表所保存的文件名。<br>

* 成员6:报表的长度。<br>

* 成员7:报表的宽度。<br>

* 成员8:报表的背景颜色,默认为白色。<br>

* 成员9:报表网格中竖线的颜色,默认为黑色。<br>

* 成员10:报表网格中横线的颜色,默认为黑色。<br>

* 成员11:报表横轴内容的数组。<br>

* 成员12:报表纵轴内容的list数组。<br>

* 成员13:提示信息。如果写入多个提示信息,报表就变成多柱状。<br>

* 成员14:报表的模式。详见reportType枚举类<br>

* 成员15:饼状图专用的数值数组。数组的和必须等于1.<br>

* 注意1:要在JSP页面引入该类型才能使用,横纵轴数组的长度必须一致。<br>

* 注意2:如果在提示信息数组中写入了多个提示信息,那么报表纵轴内容的

* list中就必须存放入和它数量一致的int数组。否则会出错。

* */

public class ReportClass{

public String ReportTitle

public String xTitle

public String yTitle

public HttpServletRequest request

public String filename

public int width

public int height

public Color BackgroundColor=Color.WHITE

public Color ylineColor=Color.BLACK

public Color xlineColor=Color.BLACK

public String[] xValues

public ArrayList<int[]>yValue=new ArrayList<int[]>()

public String[] helpstr

public String reportType

public double[] PieValue

}

/**作用:创建一个指定类型和数据的图表。<br>

* 参数1:ReportClass类型,各成员具体作用参见ReportClass说明<br>

* 返回值:String类型,在JSP页面可以直接out.println显示图形。<br>

* 注意1:ReportClass类型中,必须设置的成员主要有:<br>

* 1、ReportTitle 2、request 3、filename 4、width 5、height 6、reportType<br>

* 注意2:如果要生成饼状图,还需要设置:<br>

* 1、xValues 2、PieValue 这两个数组的长度必须一致<br>

* 注意3:如果要生成柱状图或折线图,还需要设置:<br>

* 1、xValues 2、yValue 3、helpstr<br>

* yvalue数组总和必须等于1.即100%.<br>

* 如果要生成多柱状或折线图,则需要设置helpstr长度。<br>

* yvalue列表的长度必须和helpstr数组长度一致。<br>

* yvalue中的成员数组的长度必须和xvalue数组长度一致。

* */

public String CreateReport(ReportClass rc){

String s="sf"

String str=""

JFreeChart jc=null

Font titlefont=new Font("宋体",Font.BOLD,20)

Font tickfont=new Font("宋体",0,15)

Font labelfont=new Font("宋体",Font.BOLD,15)

DefaultCategoryDataset ds=null

DefaultPieDataset pds=null

if (rc.ReportTitle!=null){

if (rc.reportType.indexOf("Pie")!=-1){//饼状图

pds=new DefaultPieDataset()

double[] ob=rc.PieValue

for (int i=0i<ob.lengthi++){

pds.setValue(rc.xValues[i], ob[i])

}

if (rc.ReportTitle!=null){

if (rc.reportType.equals("Pie2D")){

jc=ChartFactory.createPieChart(rc.ReportTitle,pds,true, true, false)

}

else if (rc.reportType.equals("Pie3D")){

jc=ChartFactory.createPieChart3D(rc.ReportTitle,pds,true, true, false)

}

jc.getTitle().setFont(titlefont)

jc.getLegend().setItemFont(labelfont)

jc.setBackgroundPaint(rc.BackgroundColor)//总背景色

jc.setBorderPaint(rc.BackgroundColor)

PiePlot plot=(PiePlot)jc.getPlot()

plot.setBackgroundPaint(rc.BackgroundColor)

plot.setLabelFont(new Font("宋体",0,15))

}

}

else { //柱状或折线图

ds=new DefaultCategoryDataset()

for (int i=0i<rc.helpstr.lengthi++){

int[] ob=rc.yValue.get(i)

for (int j=0j<ob.lengthj++){

ds.addValue(ob[j], rc.helpstr[i], rc.xValues[j])

}

}

if (rc.reportType.indexOf("Z2D")!=-1){

PlotOrientation po=null

if (rc.reportType.indexOf("V")!=-1){

po=PlotOrientation.VERTICAL

}

else if (rc.reportType.indexOf("H")!=-1){

po=PlotOrientation.HORIZONTAL

}

jc=ChartFactory.createBarChart(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false)

}

else if (rc.reportType.indexOf("Z3D")!=-1){

PlotOrientation po=null

if (rc.reportType.indexOf("V")!=-1){

po=PlotOrientation.VERTICAL

}

else if (rc.reportType.indexOf("H")!=-1){

po=PlotOrientation.HORIZONTAL

}

jc=ChartFactory.createBarChart3D(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false)

}

else if (rc.reportType.equals("Line2D")){

PlotOrientation po=PlotOrientation.VERTICAL

jc=ChartFactory.createLineChart(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false)

}

else if (rc.reportType.equals("Line3D")){

PlotOrientation po=PlotOrientation.VERTICAL

jc=ChartFactory.createLineChart3D(rc.ReportTitle, rc.xTitle, rc.yTitle,

ds, po, true, true, false)

}

jc.getTitle().setFont(titlefont)

jc.getLegend().setItemFont(labelfont)

jc.setBackgroundPaint(rc.BackgroundColor)//总背景色

CategoryPlot cp=jc.getCategoryPlot()

cp.setBackgroundPaint(rc.BackgroundColor)//图形框架背景色

cp.setDomainGridlinePaint(rc.ylineColor)//图形背景网格中竖线的颜色

cp.setDomainGridlinesVisible(true)

cp.setRangeGridlinePaint(rc.xlineColor)//图形背景网格中横线的颜色

cp.getDomainAxis().setTickLabelFont(tickfont)

cp.getDomainAxis().setLabelFont(labelfont)

cp.getRangeAxis().setTickLabelFont(tickfont)

cp.getRangeAxis().setLabelFont(labelfont)

}

try {

File ff=new File(rc.request.getRealPath("/")+"file")

if (!ff.exists()){

ff.mkdir()

}

File file=new File(rc.request.getRealPath("/")+"file/"+rc.filename)

if (!file.exists()){

file.createNewFile()

}

ChartUtilities.saveChartAsJPEG(file, jc, rc.width, rc.height)

str="<img src='"+file.getPath()+"'>"

return str

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

return null

}

}

else {

return null

}

}

}

以前写的一个报表的集成,可以生成任何形式的报表,要引入JFreeChar.你可以慢慢研究下...

JAVA POI 组件//创建HSSFWorkbook对象

HSSFWorkbook wb = new HSSFWorkbook()

//创建HSSFSheet对象

HSSFSheet sheet = wb.createSheet("sheet0")

//创建HSSFRow对象

HSSFRow row = sheet.createRow((short)0)

//创建HSSFCell对象

HSSFCell cell=row.createCell((short)0)

//用来处理中文问题

cell.setEncoding(HSSFCell.ENCODING_UTF_16)

//设置单元格的值

cell.setCellValue("单元格中的中文")

//定义你需要的输出流

OutputStream out = new FileOutputStream("viwo.xls")

//输出Excel