在实际中,经常需要生成报表以及导出功能,但是自己实现代价太大,因此使用现成的工具是一个很好的方法,接触到了JavaReport,记录下自己的使用过程 使用的是国产的第三方组件JavaReport,自行百度,下载,添加到buildpath 传送门,下载JavaReport的包
JavaReport优点1、支持实时动态的Web统计报表 2、接口丰富,可以实现良好的图表呈现 3、可以导出多种文档格式(Word、Excel、PDF等) 4、开发简便
JavaReport常用类介绍 WebReportEngine类WebReportEngine就是com.javareport.http.WebReportEngine,是整个JavaReport的开发接口,所有的JSP或者Servlet从这个类继承下来,覆盖createReport()方法就可以实现开发工作
public Report createReport(HttpServletRequest request) throws Exception{}除此以外 还有其他几种方法,这里就不一一记录了
public String validate(HttpServletRequest request);public String getStartScript(HttpServletRequest request);public String getEndScript(HttpServletRequest request) ;…… Report类报表类,这个类代表的是一张报表,所有其他报表元素存在于这个容器中。
添加报表头Header可以通过addHeader…()来实现页眉的相关信息的添加
//页眉中添加标题report.addHeaderText("学员成绩单页眉");//页眉中添加分割线report.addHeaderSeparator(1);//...... 添加报表尾/页脚(Footer)操作方法和也没一致
添加报表内容 //提供方法按先后顺序向报表中添加元素report.addChart(chart);report.addTable(table);report.addBreak(num);report.addText(text) Table类是Report对象中的元素,用来表示一个表格,用来排列数据
//数据自己填充String [][]data = {,,,,};//函数原型是:Table(Object[][] data);可以使用所有对象Table table = new Table(data);//设置表格样式 Table中存在常量,用来表示样式table.setColBorder(Table.H_CENTER);table.setRowBorder(0, 1);table.setRowBackground(0, Color.BLUE);table.setRowForeground(0, Color.RED);table.setRowBackground(1, Color.WHITE);table.setRowForeground(1, Color.BLACK);table.setRowBackground(2, Color.WHITE);table.setRowForeground(2, Color.BLACK);table.setRowBackground(3, Color.WHITE);table.setRowForeground(3, Color.BLACK);//分别表示坐标的x,y 以及横向以及纵向的扩展单元格数table.setCellSpan(4, 0, new Dimension(3,1)); Chart类图表类
//设置标签chart.setLabel(i, label);chart.setLabels(labels);chart.setData(i, j, data);设置图表样式chart.setStyle(Chart.CHART_BAR)chart.setStyle(Chart.CHART_BUBBLE)期中setStyle()的参数存在于Chart,以常量形式存在,可以实现曲线图、百分比图、柱状图等,根据实际情况使用。
RSTableresult作为数据源,快速生成的表格
通过JSP的实现 //正文"甲","20120101","SC","计算机基础","88"}, {"乙","20120102","EN","欧洲文化","99"}, {"丙","20120103","AI","人工智能","100"}, {"跨单元格演示跨单元格演示","","","100"}, }; //函数原型是:Table(Object[][] data);可以使用所有对象 Table table = new Table(data); //设置表格样式 table.setColBorder(Table.H_CENTER); table.setRowBorder(0, 1); table.setRowBackground(0, Color.BLUE); table.setRowForeground(0, Color.RED); table.setRowBackground(1, Color.WHITE); table.setRowForeground(1, Color.BLACK); table.setRowBackground(2, Color.WHITE); table.setRowForeground(2, Color.BLACK); table.setRowBackground(3, Color.WHITE); table.setRowForeground(3, Color.BLACK); //分别表示坐标的x,y 以及横向以及纵向的扩展单元格数 table.setCellSpan(4, 0, new Dimension(3,1)); return table;}%>上效果图!
使用servlet实现
//导入相关包import com.javareport.http.WebReportEngine;//该Servlet继承自WebReportEnginepublic class ExportFile extends WebReportEngine{}然后重写createReport方法即可
@Overridepublic Report createReport(HttpServletRequest request) throws Exception { Report report = new Report(); /* * 具体实现代码 和JSP中的createReport相同 */ return report;} 结束语初步学会了使用该类,大大减少了平时的编码量,提高了效率