为 bean 方式发布的报表添加随机名称

 

润乾报表除了正常的读取 raq 发布报表外,为了满足不同层次的需要,润乾 tag 还支持 definebean 的方式发布报表、支持传递 context 对象,便于用户自定义数据源、自定义参数和宏等。使用 bean 的形式发布报表需要生存 definebean 或者 ireport 对象,如果要动态的生成报表,必须随机生存报表对象才能发布。

步骤一,利用 api 创建报表

<%@ page contentType=”text/html;charset=GBK” %>

<%@ taglib uri=”/WEB-INF/runqianReport4.tld” prefix=”report” %>

<%@ page import=”java.io.*”%>

<%@ page import=”com.runqian.report4.usermodel.*”%>

<%@ page import=”com.runqian.report4.model.*”%>

<%@ page import=”com.runqian.report4.util.*”%>

<%

// 新建一个默认的空报表

ReportDefine rd = new ReportDefine2(2, 3);

// 设置数据集

SQLDataSetConfig dsc = new SQLDataSetConfig();

dsc.setName(“ds1″);

dsc.setSQL(“SELECT * FROM customer”);

DataSetMetaData dsmt = new DataSetMetaData();

dsmt.addDataSetConfig(dsc);

rd.setDataSetMetaData(dsmt);

// 设置单元格值

INormalCell icell_11 = rd.getCell(1, (short) 1);

INormalCell icell_12 = rd.getCell(1, (short) 2);

INormalCell icell_13 = rd.getCell(1, (short) 3);

icell_11.setValue(“客户类型“);

icell_12.setValue(“客户名“);

icell_13.setValue(“电话“);

// 设置单元格表达式

INormalCell icell_21 = rd.getCell(2, (short) 1);

ByteMap exp = new ByteMap();

exp.put(INormalCell.VALUE, “ds1.Group(customertype)”);

//exp.put(INormalCell.DISPVALUE, “ds2.select1(typename,typeid==value())”);// 设置显示格式

icell_21.setExpMap(exp);

INormalCell icell_22 = rd.getCell(2, (short) 2);

ByteMap exp2 = new ByteMap();

exp2.put(INormalCell.VALUE, “ds1.select(customer_name)”);

icell_22.setExpMap(exp2);

INormalCell icell_23 = rd.getCell(2, (short) 3);

ByteMap exp3 = new ByteMap();

exp3.put(INormalCell.VALUE, “ds1.tel”);

icell_23.setExpMap(exp3);

// 设置边框样式

for (int i = 1; i <= rd.getRowCount(); i++) {

for (short j = 1; j <= rd.getColCount(); j++) {

rd.setBBColor(i, j, -16777216);//

rd.setBBStyle(i, j, INormalCell.LINE_SOLID);

rd.setBBWidth(i, j, (float) 0.75);

rd.setLBColor(i, j, -16777216);

rd.setLBStyle(i, j, INormalCell.LINE_SOLID);

rd.setLBWidth(i, j, (float) 0.75);

rd.setRBColor(i, j, -16777216);

rd.setRBStyle(i, j, INormalCell.LINE_SOLID);

rd.setRBWidth(i, j, (float) 0.75);

rd.setTBColor(i, j, -16777216);

rd.setTBStyle(i, j, INormalCell.LINE_SOLID);

rd.setTBWidth(i, j, (float) 0.75);

}

}

// 输出到本地

OutputStream out1 = new FileOutputStream(“D:\\test.raq”);

ReportUtils.write(out1,rd);

out1.close();

步骤二,使用随机数为生成的报表命名

// 设置 request 中报表定义对象

String rptName = “RPT_”+Double.toString(Math.random());

request.setAttribute(rptName,rd);

直接调用 Math.random() 是产生一个 [0,1)之间的随机数,

如果用

java.util.Random random=new Random();random.nextInt() 这样产生一个长整型的随机数并且与上一次是一样的,如果过一会再产生就不会一样了,例如:

for (int i = 0; i < 10; i++) { 
Random random=new Random(); 
Thread.sleep(100); 
System.out.print((int)random.nextInt(100)+” “); 
}

就是产生不同的随机 Long 数了

如果想用 java.util.Random() 产生指定范围的随机数就需要进行求模运算,进行一些处理

步骤三,html 内 beanname 发布报表

<report:html name=”report1″

srcType=”defineBean”

beanName=”<%=rptName%>”

/>