在润乾报表中,我们常常需要根据一些参数来动态改变报表的在web页面展现样式,由于润乾报表中对于单元格样式可以通过表达式来动态设置,大大降低了实现一些复杂功能的困难度,但对于一些无法用表达式来控制的属性,我们要怎么动态设置呢?这里就要用到功能强大的API了。
像在有一些报表中,表头是动态的,也就是说,表头列的个数不是一定的,这样在web展现时,怎么才能根据不同的情况固定不同的表头呢?
这里我们举一个动态设置左表头的例子(使用设计器自带的demo数据源):
第一步:利用API生成一张新报表:
package demo;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.runqian.report4.model.ReportDefine;
import com.runqian.report4.model.ReportDefine2;
import com.runqian.report4.usermodel.ByteMap;
import com.runqian.report4.usermodel.Context;
import com.runqian.report4.usermodel.DataSetMetaData;
import com.runqian.report4.usermodel.Engine;
import com.runqian.report4.usermodel.IByteMap;
import com.runqian.report4.usermodel.IColCell;
import com.runqian.report4.usermodel.INormalCell;
import com.runqian.report4.usermodel.IReport;
import com.runqian.report4.usermodel.SQLDataSetConfig;
import com.runqian.report4.util.ReportUtils;
public class apiReadReport {
public static void main(String[] args) {
//新建一张3行3列报表
ReportDefine rd = new ReportDefine2(3, 3);
rd.addCol((short) 2);
rd.addRow(2);
//设置展现字段
INormalCell inc1 = rd.getCell(1, (short) 1);
inc1.setValue("类别ID");
INormalCell inc2 = rd.getCell(1, (short) 2);
inc2.setValue("类别名称");
//设置表达式
INormalCell inc3 = rd.getCell(2, (short) 1);
IByteMap map1 = new ByteMap();
map1.put(INormalCell.VALUE, "ds1.select(类别ID,false)");
inc3.setExpMap(map1);
INormalCell inc4 = rd.getCell(2, (short) 2);
IByteMap map2 = new ByteMap();
map2.put(INormalCell.VALUE, "ds1.类别名称");
inc4.setExpMap(map2);
第二步:根据参数动态设置左表头:
//设置几列为左表头(这个就是控制几列为左表头的参数,传进来几列,前几列就为左表头;参数为2,就是前两列为左表头)
int colnum = 2;//这个位置可以通过前台传递的参数来动态改变,例如:String colnum = request.getParameter( "colnum" );
//循环设置列的左表头属性,有把几列设为左表头就循环几次
for(int i = 0 ;i<colnum;i++){
IColCell colCell = rd.getColCell((short)(i+1));
//IByteMap map3 = new ByteMap();
//设置左表头
colCell.setColType(IColCell.TYPE_LEFT_HEADER);
//colCell.setExpMap(arg0)
}
// IColCell colCell2 = rd.getColCell((short)2);
// colCell2.setColType(IColCell.TYPE_LEFT_HEADER);
//设置数据集
DataSetMetaData dsmd = new DataSetMetaData();
SQLDataSetConfig sdc = new SQLDataSetConfig();
sdc.setName("ds1");
sdc.setSQL("SELECT类别.类别ID,类别.类别名称 from 类别");
dsmd.addDataSetConfig(sdc);
rd.setDataSetMetaData(dsmd);
try {
//保存报表
ReportUtils.write("D:\\aa.raq", rd);
System.out.println("报表保存成功!");//这个时候用报表设计打开报表,就可以看到前两列被设置成左表头了.
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第三步:在展现报表的jsp标签中增加滚动条属性:
<table align=center>
<tr><td>
<report:html name="report1" reportFileName="<%=report%>"
funcBarLocation=""
needPageMark="yes"
generateParamForm="no"
needLinkStyle="yes"
params="<%=param.toString()%>"
needScroll="yes"//是否固定住左表头
scrollWidth="250"//固定表头报表的显示宽度
scrollHeight="400"//固定表头报表的显示高度
exceptionPage="/reportJsp/myError2.jsp"
/>
</td></tr>
</table>
效果:
设置后的报表模版:
参数模版:
参数模版中B2单元格的web变量设置:
这样就可以通过参数模版来设置表头的个数了。
在页面上的展现效果为:
当报表中的一些功能无法通过单纯设置报表模版来实现时,利用API也可以达到相同的效果,而且,利用API还可以完成一些较复杂的功能,如一些动态变更展现效果,动态更换数据源等等。如此一来,就可以通过润乾报表的API来丰富您的报表样式和功能了。