如何在Jaspersoft Studio中呈现HTML表格 [英] How to render a HTML Table in Jaspersoft Studio

查看:92
本文介绍了如何在Jaspersoft Studio中呈现HTML表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中存储了一个html标记,例如:

I have a html markup stored in database like:

<table style='border: 1px solid black'>
    <tr>
        <td  style='border: 1px solid black'><strong>1</strong></td>
        <td  style='border: 1px solid black'><strong>2</strong></td>
    </tr>
    <tr>
        <td  style='border: 1px solid black'>Data</td>
        <td  style='border: 1px solid black'>Data</td>
    </tr>
</table>

HTML是动态生成的,并以文本形式存储在DB中. 我必须像在Japersoft Studio中一样渲染此表.

The HTML is generated dynamically and stored in the DB as text. I have to render this table as is in Japersoft Studio.

1..创建一个文本字段并将其标记为HTML. 问题:这仅适用于文本格式.不适用于表格标签.

1. Making a Text Field and making its markup as HTML. Problem: This works only for text formatting. Does not work with table tags.

2..制作通用元素并在其中呈现HTML(

2. Making a Generic Element and rendering the HTML there(Reference). Problem: The rendered HTML is takes the height and width of the Text Field.

对于如何实现此目标,我们将不胜感激.我开始怀疑是否甚至可以通过Jaspersoft Studio实现.

Any help would be very much appreciated on how to achieve this. I am starting to doubt if this is even possible through Jaspersoft Studio.

我正在使用TIBCO Jaspersoft Studio 6.3.1 final.

I am using TIBCO Jaspersoft Studio 6.3.1 final.

推荐答案

受贾维斯(Jarvis)的启发,主要是出于娱乐目的

如您已正确指出您无法在textField中呈现html表,则需要使用HtmlComponent 但这会创建图像,您不仅会遇到缩放问题,而且还会出现文本的选择和可搜索性问题.此外,正如Dave Jarvis在其答案中所述,通常,这不是将数据传递给jasper报告的合适方法.

但是谁在乎让您呈现html的所有内容

As you have correctly noted you can't render html table in textField you would need to use the HtmlComponent <hc:html/> but this will create an image and you will not only have scaling problems but also issue as selection and searchability of text. Furthermore as Dave Jarvis has stated in their answer this in general is not a suitable way to pass data to jasper reports.

  1. 我们需要将html转换为 JRDataSource ,我们将使用 Jsoup

public class HtmlTableDataSource implements JRDataSource {

    private List<Elements> rows;
    private Iterator<Elements> iterator;
    private Elements currentRow;

    public HtmlTableDataSource(String html) {
        super();
        init(html);
    }

    private void init(String html) {
        this.rows = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements tables = doc.select("table"); 
        if (tables == null || tables.isEmpty()) {
           return;
        }
        // Get first table ignore others
        Element table = tables.get(0);
        //Get all rows
        Elements trs = table.select("tr");
        for (Element element : trs) {
            //add all of our columns to our list
            this.rows.add(element.select("td"));
        }

        this.iterator = this.rows.iterator();
    }

    @Override
    public Object getFieldValue(JRField field) throws JRException {
        if (field==null||currentRow==null){
            return null;
        }
        try {
            int col = Integer.parseInt(field.getName());
            if (col<currentRow.size()){
                return currentRow.get(col).html();
            }
        } catch (NumberFormatException e) {
            throw new JRException("Using the HTMLTableDataSource, the field name need to be numbers starting from 0");
        }
        return null;
    }

    @Override
    public boolean next() throws JRException {
        if (this.iterator == null || !this.iterator.hasNext()) {
            return false;
        }
        this.currentRow = this.iterator.next();
        return true;
    }
}

  • 现在让我们在报告中使用它(您需要在类路径中使用上述类和Jsoup)

  • Now lets use it in a report (you need the above class and Jsoup in classpath)

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="htmlTable" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="597c0716-df6b-42ec-a7c8-863eb1b7174a">
        <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <subDataset name="TableDataset" uuid="998ba41a-db15-454b-a081-bc8613899c31">
            <field name="0" class="java.lang.String"/>
            <field name="1" class="java.lang.String"/>
        </subDataset>
        <summary>
            <band height="30">
                <componentElement>
                    <reportElement x="0" y="0" width="550" height="30" uuid="5d0d5bcb-a094-4446-a5e9-09de629cefc7"/>
                    <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                        <datasetRun subDataset="TableDataset" uuid="eac86c28-fad2-433a-a02e-0dd419d9e135">
                            <dataSourceExpression><![CDATA[new my.package.HtmlTableDataSource("<table style='border: 1px solid black'><tr><td  style='border: 1px solid black'><b>1</b></td><td  style='border: 1px solid black'><b>2</b></td></tr><tr><td  style='border: 1px solid black'>Data</td><td  style='border: 1px solid black'>Data</td></tr></table>")]]></dataSourceExpression>
                        </datasetRun>
                        <jr:column width="180" uuid="86ce5c9c-d285-4a51-bf84-30fa9c55579f">
                            <jr:detailCell style="Table_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="180" height="30" uuid="3acb437b-8b59-4e78-ac1b-4f096f960e89"/>
                                    <textElement textAlignment="Center" verticalAlignment="Middle" markup="html"/>
                                    <textFieldExpression><![CDATA[$F{0}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="150" uuid="741517bf-5107-4b13-9d61-44209f266c6c">
                            <jr:detailCell style="Table_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="150" height="30" uuid="1174a60c-8b9e-441b-a9ef-4340b0cd7b68"/>
                                    <textElement textAlignment="Center" verticalAlignment="Middle" markup="html"/>
                                    <textFieldExpression><![CDATA[$F{1}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
            </band>
        </summary>
    </jasperReport>
    

  • <dataSourceExpression><![CDATA[new my.package.HtmlTableDataSource("<table..")]]></dataSourceExpression>这将实例化我们的数据源,而jasper报告将在其上循环调用next()方法和getFieldValue(JRField field)(当要评估字段时)

    The <dataSourceExpression><![CDATA[new my.package.HtmlTableDataSource("<table..")]]></dataSourceExpression> this will instance our datasource and jasper reports will loop over it calling the next() method and the getFieldValue(JRField field) when field is to be evaluate

    我对JRDataSource的实现既快速又有趣,自然可以大大改进.

    My implementation of the JRDataSource is just quick and fun, naturally it can be vastly improved.

    动态列,如果只有最大数目且它们是固定大小,则可以使用类似于 https://stackoverflow的解决方案. com/a/36911788/5292302 (如果未切换为使用交叉表根据需要修改JRDataSource.

    Dynamic columns, if only a maximum number and they are fixed size you could use a solution similar to this https://stackoverflow.com/a/36911788/5292302 if not switch to use crosstabs modifying the JRDataSource as needed.

    是的,我将<strong/>更改为<b/>,这是另一个问题为什么html标记(s,强)在jasper中不起作用报告?,在这种情况下,您还可以在数据源中处理它,因此可以使用Jsoup进行替换.

    Yes I changed <strong/> to <b/> this is another problem Why does html tags (s, strong) not work in jasper reports?, in this case you could also handle it within your datasource, hence use Jsoup to substitute.

    这篇关于如何在Jaspersoft Studio中呈现HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆