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; } } |