润乾报表中利用API动态设置左表头

本文介绍如何使用润乾报表的API动态调整报表在Web页面上的展示样式,特别是如何根据参数动态设置左表头数量,实现固定表头滚动等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在润乾报表中,我们常常需要根据一些参数来动态改变报表的在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来丰富您的报表样式和功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值