来自XML数据源的非常简单的XLS - Jasper [英] Really simple XLS from XML datasource - Jasper

查看:153
本文介绍了来自XML数据源的非常简单的XLS - Jasper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的XML数据源结构如下:

I have a very simple XML datasource structured like this:

<datasource>
    <row>
        <column>Some text 1</column>
        <column>Some text 2</column>
        <column>Some text 3</column>
    </row>
    <row>
        <column>Some text 4</column>
        <column>Some text 5</column>
        <column>Some text 6</column>
    </row>
    <row>
        <column>Some text 7</column>
        <column>Some text 8</column>
        <column>Some text 9</column>
    </row>
</datasource>

我想创建一个非常简单的XLS报告格式如下:

And I want to create a very simple XLS report formatted like this:

-------------------------------------------
| Some text 1 | Some text 2 | Some text 3 |
-------------------------------------------
| Some text 4 | Some text 5 | Some text 6 |
-------------------------------------------
| Some text 7 | Some text 8 | Some text 9 |
-------------------------------------------

我创建了一个包含XPath查询的报告:

I have created a report with the XPath query:

/datasource

还有两个字段:

$F{row}
$F{column}

带表达式:

row
row/column

我添加了一个交叉表元素详细信息区域并尝试使用上面为行和列分组选项定义的字段的不同变体进行配置。但我能得到的最好结果是第一列值(即上例中的一些文本1值)。

I have added a crosstab element to the Detail band and have made a few attempts to configure it with different variations of the fields defined above for the row and column grouping options. But the best result I have been able to get is the 1st column value (i.e. "Some text 1" value in the above example).

我的问题:


  1. 如何配置交叉表元素以迭代我的数据源中的每一行
    元素并将列值推送到正确的
    上面的例子?

  2. 交叉表元素是此类任务中最合适的
    元素吗?


推荐答案

好的,我已经弄明白了,我真的不能指出任何一个来源作为这个东西的一个很好的参考点。 Jasper的文档和社区贡献在很大程度上是稀疏和过时的。我离题了。以下是我为iReport 4.5.0和Jasper 4.5.0工作所做的工作:

Ok so I have figured it out and I really cannot pin point any one source as a good point of reference for this stuff. Jasper's documentation and community contribution is for the most part sparse and dated. I digress. Here is what I did to get it working for iReport 4.5.0 and Jasper 4.5.0:


  1. 在iReport中创建空白A4报告。

  2. 编辑文档查询以使用XPath语法选择所有行元素(即/ datasource / row)

  3. 现在通过右边创建一个子数据集单击Report Inspector视图中的document属性(即与.jrmxl文件同名的文件属性)并选择Add dataset。

  4. 在向导中选择从连接创建新数据集或者数据源

  5. 选择与主报告相同的XML数据源,如果您有要传递的字段或组,请在随后的相应向导窗口中选择它们。但主要是选择您的数据源。

  6. 现在您需要编辑查询以在列元素上进行选择。在此之前,为了简化生活,我编辑了我的XML数据源,在每个行元素中包含一个count属性。如果您愿意,可以创建自己的递增计数,但对我来说,编辑数据源文档是最简单的。
  1. Create a blank A4 report in iReport.
  2. Edit the document query to select all 'row' elements using XPath syntax (i.e. /datasource/row)
  3. Now create a sub dataset by right clicking the document property from your Report Inspector view (i.e. the one with the same name as your .jrmxl file) and selecting Add dataset.
  4. In the wizard select "create new dataset from a connection or datasource"
  5. Select the same XML datasource as your master report and if you have fields or groups you want to pass across select them in the corresponding wizard windows that follow. But the main thing is to select your datasource.
  6. Now you will need to edit the query to select on your column elements. Before doing this, to make life easier, I edited my XML datasource to include a "count" attribute in each row element. You can create your own incrementing count if you like but for me editing the datasource document was the easiest.

  1. 所以我接下来要做的是在我的主报告中创建一个名为rowCount的字段并给它描述表达式@count,以便它存储count属性值来自每个行元素。

  2. 之后我在子数据集中设置了一个具有相同名称的参数。

  3. 然后我配置了我的子数据集查询阅读/ datasource / row [@count =$ P {rowCount}] / column以便为每一行选择所有列元素。

  4. 接下来我创建了一个字段名为columnValue的子数据集,带有描述表达式:text(),用于存储每个列元素的文本。

  1. So next thing I did was create a field in my master report called "rowCount" and gave it the description expression "@count" so that it would store the count attribute value from each row element.
  2. After that I set up a parameter in my sub dataset with the same name.
  3. Then I configured my sub dataset query to read "/datasource/row[@count="$P{rowCount}"]/column" so that all column elements were selected for each row.
  4. Next I created a field in the sub dataset called columnValue with the description expression: "text()" to store the text from each column element.


  • 配置后子数据集我将交叉表对象拖到我的详细信息区域中,这促使交叉表向导显示。

  • After configuring the sub dataset I dragged a crosstab object into my detail band which prompted the crosstab wizard to show.


    1. 在向导中,我选择了子数据集作为其数据源,并选择了与主报告相同的连接。

    2. 对于行和列组值我刚刚分别选择了$ P {rowCount}和$ V {COLUMN_COUNT}。

    3. 最重要的是,我将数据部分配置为指向我的columnValue字段并选择度量为没有,而不是计数或任何其他计算。最后单击完成以创建对象。


  • 下一步是右键单击Report Inspector中的交叉表对象并选择交叉表数据这会打开一个数据配置窗口。在这里我:

  • Next step was to right click the crosstab object in the Report Inspector and select "crosstab data" which bring up a data config window. In here I:


    1. 选定的数据已预先排序

    2. 在底部的数据集运行部分的连接中/ Datasource exp选项卡我选择了使用连接表达式,其值为$ P {REPORT_CONNECTION}

    3. 在参数选项卡中,我添加了:

      • XML_DATA_DOCMENT

      • REPORT_CONTEXT

      • XML_DATE_PATTERN

      • XML_NUMBER_PATTERN

      • XML_LOCALE

      • XML_TIME_ZONE

      • rowCount

    1. Selected "data is pre-sorted"
    2. In the Dataset Run section at the bottom, in the "Connection/Datasource exp" tab I selected "Use Connection Expression" with the value $P{REPORT_CONNECTION}
    3. In the "Parameters" tab I added:
      • XML_DATA_DOCMENT
      • REPORT_CONTEXT
      • XML_DATE_PATTERN
      • XML_NUMBER_PATTERN
      • XML_LOCALE
      • XML_TIME_ZONE
      • rowCount


  • 从这一点开始,要做的最后几件事就是破解要删除的XML行和列组标记需要从crostabJRXML元素中删除以下元素(包括下面的所有子元素):

    • crosstabRowHeader

    • crosstabTotalRowHeader

    • crosstabColumnHeader

    • crosstabTotalColumnHeader

    • The last couple of things to do from this point is to hack the XML to remove the row and column group markup which entails removing the following elements from inside the "crostab" JRXML element (including all sub elements of the below):
      • crosstabRowHeader
      • crosstabTotalRowHeader
      • crosstabColumnHeader
      • crosstabTotalColumnHeader
      • 就是这样。对我来说,这就是解决交叉表/ XML数据源/子数据集难题所需要的!

        So that's it. For me this is what I need to solve the crosstab / XML datasource / sub dataset conundrum!

        这篇关于来自XML数据源的非常简单的XLS - Jasper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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