如何在 iReports 中为 reportElement 标签添加新属性?

Sri*_*har 2 java jasper-reports

我试图在我的报告中包含 css 类。是否可以将我自己的属性添加到 reportElement 标签,以便我可以包含我的 css 文件引用链接?

谢谢

Jac*_*oen 5

您需要做的是向您想要引用的字段添加一个属性。要添加您需要添加的类名net.sf.jasperreports.export.html.class并包含您需要添加net.sf.jasperreports.export.html.id为属性的 id 。例如,下面是一个同时设置的 Text 字段:

<textField>
    <reportElement uuid="2399e4ef-633c-4d17-b964-3e093ece1936" x="0" y="22" width="100" height="20">
        <property name="net.sf.jasperreports.export.html.class" value="TEST"/>
        <property name="net.sf.jasperreports.export.html.id" value="ID"/>
    </reportElement>
    <textElement markup="html"/>
    <textFieldExpression><![CDATA[($F{field1}]]></textFieldExpression>
</textField>
Run Code Online (Sandbox Code Playgroud)

在 iReport 中,您可以通过选择字段来添加这些,然后在属性窗口中单击 旁边的省略号按钮Properties expressions在此处输入图片说明

要在导出的报告中包含指向 css 文件的链接,您需要JRHtmlExporterParameter.HTML_HEADER在导出前设置该参数的值。注意参数不是HTML意义上的header(head标签的内容),而是导出的HTML报表的header。这意味着它是在包含报告之前首先放置在导出报告中的内容。Jasper Reports 使用的默认值是:

<html>
<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <style type="text/css">
    a {text-decoration: none}
  </style>
</head>
<body text="#000000" link="#000000" alink="#000000" vlink="#000000">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td width="50%">&nbsp;</td><td align="center">
Run Code Online (Sandbox Code Playgroud)

因此,您需要通过添加以下内容来修改它以包含指向样式表的链接:

<link rel="stylesheet" type="text/css" href="<cssfile you want to point to>" />
Run Code Online (Sandbox Code Playgroud)

到适当的地方,我认为它在头部标签内,但如果没有移动到适当的区域。所以java代码最终看起来像:

JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, 
    "<html>"+
    "<head>"+
    "  <title></title>"+
    "  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>"+
    "  <link rel=\"stylesheet\" type=\"text/css\" href=\"css/jasper.css\" />"+
    "  <style type="text/css">"+
    "    a {text-decoration: none}"+
    "  </style>"+
    "</head>"+
    "<body text="#000000" link="#000000" alink="#000000" vlink="#000000">"+
    "<table width="100%" cellpadding="0" cellspacing="0" border="0">"+
    "<tr><td width="50%">&nbsp;</td><td align="center">");
exporter.exportReport();
Run Code Online (Sandbox Code Playgroud)