随笔 - 225  文章 - 98  trackbacks - 0
<2009年4月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

随笔分类

相册

报表工具厂商们

搜索

  •  

最新评论

阅读排行榜

 

下面是本人得到的关于FineReport三个典型报表图的代码制作过程。有兴趣的可以参考一下,很有帮助!

报表一:
 


代码如下:
package ms; import java.io.File; import com.fr.base.Constants; import com.fr.base.FRContext; import com.fr.base.dav.LocalEnv; import com.fr.demo.ArrayTableDataDemo; import com.fr.report.CellElement; import com.fr.report.Report; import com.fr.report.WorkBook; import com.fr.report.WorkSheet; import com.fr.report.cellElement.CellExpandAttr; import com.fr.report.cellElement.TableDataColumn; import com.fr.report.cellElement.core.DSColumn; import com.fr.report.cellElement.core.FunctionGrouper; import com.fr.report.io.TemplateImporter; import com.fr.web.Reportlet; import com.fr.web.ReportletException; import com.fr.web.ReportletRequest; public class ListReportlet implements Reportlet{ public Report createReport(ReportletRequest req) throws ReportletException { WorkBook workBook = null; try { File cptFile = new File("D:\\listreport.cpt"); TemplateImporter templateImporter = new TemplateImporter(cptFile); workBook = (WorkBook)templateImporter.generateReport(); // 定义程序数据集 String[] columnNames = {"月份", "产品", "销售额"}; Object[][] rowData = { {"五月", "手机", new Integer(500)}, {"五月", "相机", new Integer(600)}, {"五月", "手表", new Integer(800)}, {"六月", "手机", new Integer(300)}, {"六月", "相机", new Integer(100)} }; ArrayTableDataDemo tableData = new ArrayTableDataDemo(columnNames, rowData); WorkSheet workSheet = (WorkSheet)workBook.getReport(0); // 将数据集添加到workSheet中, Employee为表名 workSheet.putTableData("Employee", tableData); // 把月份数据列放在单元格A2 DSColumn dsColumn = new DSColumn(); dsColumn.setDSName("Employee"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("月份")); CellElement cellElement = workSheet.getCellElement(0, 1); cellElement.setValue(dsColumn); // 把产品填入到B2 dsColumn = new DSColumn(); dsColumn.setDSName("Employee"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("产品")); cellElement = workSheet.getCellElement(1, 1); cellElement.setValue(dsColumn); // 把销售额填入到c2 // 把产品填入到B2 dsColumn = new DSColumn(); dsColumn.setDSName("Employee"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("销售额")); cellElement = workSheet.getCellElement(2, 1); cellElement.setValue(dsColumn); } catch (Exception exp) { exp.printStackTrace(); } return workBook; } }



报表二:
 


代码如下:
package ms; import java.io.File; import com.fr.demo.ArrayTableDataDemo; import com.fr.report.CellElement; import com.fr.report.Report; import com.fr.report.WorkBook; import com.fr.report.WorkSheet; import com.fr.report.cellElement.TableDataColumn; import com.fr.report.cellElement.core.DSColumn; import com.fr.report.cellElement.core.FunctionGrouper; import com.fr.report.io.TemplateImporter; import com.fr.web.Reportlet; import com.fr.web.ReportletException; import com.fr.web.ReportletRequest; public class CrossReportlet implements Reportlet{ public Report createReport(ReportletRequest req) throws ReportletException { WorkBook workBook = null; try { File cptFile = new File("D:\\crossreport.cpt"); TemplateImporter templateImporter = new TemplateImporter(cptFile); workBook = (WorkBook)templateImporter.generateReport(); // 定义程序数据集 String[] columnNames = {"月份", "产品", "销售额"}; Object[][] rowData = { {"五月", "手机", new Integer(500)}, {"五月", "相机", new Integer(600)}, {"五月", "手表", new Integer(800)}, {"六月", "手机", new Integer(300)}, {"六月", "相机", new Integer(100)} }; ArrayTableDataDemo tableData = new ArrayTableDataDemo(columnNames, rowData); WorkSheet workSheet = (WorkSheet)workBook.getReport(0); // 将数据集添加到workSheet中, Employee为表名 workSheet.putTableData("Employee", tableData); // 把月份数据列放在单元格A2 DSColumn dsColumn = new DSColumn(); dsColumn.setDSName("Employee"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("月份")); CellElement cellElement = workSheet.getCellElement(0, 1); cellElement.setValue(dsColumn); // 把产品填入到B2 dsColumn = new DSColumn(); dsColumn.setDSName("Employee"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("产品")); cellElement = workSheet.getCellElement(1, 0); cellElement.setValue(dsColumn); // 把销售额填入到c2 // 把产品填入到B2 dsColumn = new DSColumn(); dsColumn.setDSName("Employee"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("销售额")); cellElement = workSheet.getCellElement(1, 1); cellElement.setValue(dsColumn); } catch (Exception exp) { exp.printStackTrace(); } return workBook; } }



报表三:
 


 代码如下:
package ms; import java.awt.Color; import java.awt.Font; import java.io.File; import com.fr.base.ColumnRow; import com.fr.base.Constants; import com.fr.base.FRFont; import com.fr.base.Style; import com.fr.base.background.ColorBackground; import com.fr.data.condition.CommonCondition; import com.fr.data.core.Compare; import com.fr.demo.ArrayTableDataDemo; import com.fr.report.CellElement; import com.fr.report.Report; import com.fr.report.WorkBook; import com.fr.report.WorkSheet; import com.fr.report.cellElement.TableDataColumn; import com.fr.report.cellElement.core.DSColumn; import com.fr.report.cellElement.core.FunctionGrouper; import com.fr.report.io.TemplateImporter; import com.fr.web.Reportlet; import com.fr.web.ReportletException; import com.fr.web.ReportletRequest; public class CompareReportlet implements Reportlet { public Report createReport(ReportletRequest req) throws ReportletException { WorkBook workBook = null; try { File cptFile = new File("D:\\comparereport.cpt"); TemplateImporter templateImporter = new TemplateImporter(cptFile); workBook = (WorkBook)templateImporter.generateReport(); // 定义程序数据集 String[] columnNames = {"产品", "销售额"}; Object[][] rowData1 = { {"手机", new Integer(500)}, {"相机", new Integer(600)}, {"手表", new Integer(800)} }; Object[][] rowData2 = { {"手机", new Integer(300)}, {"相机", new Integer(100)} }; ArrayTableDataDemo tableData1 = new ArrayTableDataDemo(columnNames, rowData1); ArrayTableDataDemo tableData2= new ArrayTableDataDemo(columnNames, rowData2); WorkSheet workSheet = (WorkSheet)workBook.getReport(0); //marks:调整报表的列宽 workSheet.setColumnWidth(2, 200); // 将数据集添加到workSheet中, Employee为表名 workSheet.putTableData("Employee1", tableData1); workSheet.putTableData("Employee2", tableData2); // 把产品填入到A2 DSColumn dsColumn = new DSColumn(); dsColumn.setDSName("Employee1"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("产品")); CellElement cellElement = workSheet.getCellElement(0, 1); cellElement.setValue(dsColumn); // 把产品填入到B2 dsColumn = new DSColumn(); dsColumn.setDSName("Employee2"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("销售额")); //marks:设置条件,产品=A2 dsColumn.setCondition(new CommonCondition("产品",Compare.EQUALS,ColumnRow.valueOf(0,1))); cellElement = workSheet.getCellElement(1, 1); cellElement.setValue(dsColumn); // 把产品填入到C2 dsColumn = new DSColumn(); dsColumn.setDSName("Employee1"); dsColumn.setGrouper(new FunctionGrouper()); dsColumn.setColumn(TableDataColumn.createColumn("销售额")); cellElement = workSheet.getCellElement(2, 1); cellElement.setValue(dsColumn); //marks:设置单元格字体, // 得到CellElement的样式,如果没有新建默认样式 Style style = cellElement.getStyle(); if(style == null) { style = Style.getInstance(); } // 设置字体和前景的颜色 FRFont frFont = FRFont.getInstance("Dialog", Font.BOLD, 14); frFont = frFont.applyForeground(new Color(21, 76, 160)); style = style.deriveFRFont(frFont); // 设置背景 ColorBackground background = ColorBackground.getInstance(new Color(255, 255, 177)); style = style.deriveBackground(background); // 设置水平居中 style = style.deriveHorizontalAlignment(Constants.CENTER); // 设置边框 style = style.deriveBorder(Constants.LINE_DASH_DOT, Color.red, Constants.LINE_DASH_DOT, Color.yellow, Constants.LINE_DASH_DOT, Color.BLUE, Constants.LINE_DASH_DOT, Color.CYAN); cellElement.setStyle(style); } catch (Exception exp) { exp.printStackTrace(); } return workBook; } }



了解Java报表工具就从这里开始
posted on 2009-04-15 16:49 season 阅读(987) 评论(3)  编辑  收藏 所属分类: Java报表—技术知识

FeedBack:
# re: FineReport三个典型报表图制作代码全分享 2010-09-09 16:22 lovemark2000
这还是有点难度,加油学习中  回复  更多评论
  
# re: FineReport三个典型报表图制作代码全分享 2010-09-10 11:14 我爱大喵喵
专业  回复  更多评论
  
# re: FineReport三个典型报表图制作代码全分享 2010-09-12 11:24 桑桑
LZ的解释好详细~~抱走了  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: