ireport5.6使用table组件,怎么用table显示javaBean数据源
2015-12-29 · 知道合伙人数码行家
知道合伙人数码行家
向TA提问 私信TA
1.从组件面板添加一个table组件到报表中。
2.设计table的字段头.
合并操作
1.
2.
删除你不需要的列
新增你的合并列
3.在报表Parameters里新增一个参数dets(java.util.List)
4.配置table数据集
a.重命令数据集(方便)
右键-》属性,即可修改.
b.添加一个Parameters
新增一个table1(net.sf.jasperreports.engine.JRDataSource)
c.手动配置代码,把dets参数传递给table1接收
<datasetParameter name="table1">
<datasetParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dets})]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dets})]]></dataSourceExpression>
d.添加Fields,显示的字段
5.报表设计完成
6.编写java代码以传递javaBean数据源的方式调用报表并显示
/**
* 打印报表 ckz
*
* @param billcode
* @param req
* @param resp
* @throws Exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Transactional(readOnly = true)
public void doReport(String billcode,String exportType,HttpServletRequest request,HttpServletResponse response){
try {
String root_path = request.getSession ().getServletContext ().getRealPath ("");
List list = binadjustBillMasDao.queryBinadjustbillDS (billcode);
List datalist = binadjustBillDetDao.queryBinadjustbillDet (billcode);
// 设置report参数
Map params = new HashMap ();
String username = (String) request.getSession ().getAttribute ("employeename");
params.put ("username", username);
params.put ("title", "物资调整单");
params.put ("dets", datalist);
params.put ("SUBREPORT_DIR", root_path + "\\webresource\\reports\\binadjust\\");
params.put ("logo", root_path + IReportConstant.LOGOIMAGEURL);
// 获取打印报表所需的数据
String jaspername = "/binadjust/report_binadjustbill_mas_cn.jasper";
//注:list的size的大小,决定报表里的Detail显示几次,比如:size=2,那么加载后的报表就有2个table显示
JasperHelper.exportmain (exportType, jaspername, list, params, "binadjust", request, response);
} catch (Exception ex) {
PrintWriter out = null;
try {
response.setCharacterEncoding ("UTF-8");
out = response.getWriter ();
out.write ("<h1 style='position: absolute;left: 50%;top: 50%;margin-left: -180px;margin-top: -10px;'>打印报表出错,请重试!</h1>");
} catch (IOException e) {
e.printStackTrace ();
} finally {
out.close ();
}
ex.printStackTrace ();
}
response.setCharacterEncoding ("UTF-8");
response.setContentType ("text/html;charset=UTF-8");
}