如何使 Jasper Reports 以编程方式确定报告中的列名称?

发布于 2024-12-07 11:30:53 字数 684 浏览 0 评论 0原文

我正在生成一份报告,其中有 7 列,其中最后 6 列应列出最近 6 个月的情况。因此,截至撰写本文时,它应该是:

NAME -> September -> August -> July -> June -> May -> April

ss 列标题。我试图避免将它们作为参数传递,并试图让 Jasper Reports 在运行时弄清楚它。我可以使用文本字段表达式轻松获得第一个月。看起来:

new java.text.SimpleDateFormat("MMMMM").format(new Date())

这个问题是在其他月份出现的。我最初尝试

new java.text.SimpleDateFormat("MMMMM").format(java.util.Calendar.getInstance().add(Calendar.MONTH, new Integer("-1)).getTime())

这不起作用,因为 Calendar.add 不返回 Calendar 实例。然后我尝试使用一个变量,然后使用变量组合,但也不起作用。

如何让 Jasper Reports 以编程方式确定报告本身的列名称?

I am generating a report with that will have a 7 columns where the last 6 should have the the last 6 months listed. So as of the time of this writing it should be:

NAME -> September -> August -> July -> June -> May -> April

ss the column headers. I am trying to avoid having to pass them in as parameters, and am trying to get Jasper Reports to figure it out at runtime. I can get the first month pretty easily using a Text Field Expression. It looks like:

new java.text.SimpleDateFormat("MMMMM").format(new Date())

The issue comes in with the other months. I initially tried

new java.text.SimpleDateFormat("MMMMM").format(java.util.Calendar.getInstance().add(Calendar.MONTH, new Integer("-1)).getTime())

This does not work since Calendar.add does not return a Calendar instance. I then tried using a variable and then a combination of variables which also did not work.

How to make Jasper Reports programmatically determine the Name of Columns within the report it self?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任谁 2024-12-14 11:30:53

我认为解决这个问题的最佳方法是使用 Commons Lang。该软件包提供了实用程序,使此类计算变得非常容易。通过添加一个额外的 jar,您可以使用如下表达式:

DateUtils.addMonths(new Date(),-1)

我发现这比先创建一个辅助日历类然后使用三元运算符但忽略其结果更容易维护。

$P{cal}.add(Calendar.MONTH, -1)
? null : $P{cal}.getTime()

如果您需要概括解决方案,那么从 SQL 查询中获取日期比获取日历要容易得多。所以你可以快速更改为“DateUtils.addMonths($F{MyDate},-1)”。初始化日历以匹配查询返回的日期并不那么简单。当然,不必再添加一个 .jar 文件有一定的好处。因此,有时三元运算符技术是完成任务的最快方法。

几年前,我在这里写过有关使用 Commons Lang 方法的文章: http://mdahlman.wordpress.com/2009/09/09/jasperreports-first-dates/

I think the best approach to solving this problem is to use Commons Lang. That package provides utilities to make calculations like this very easy. By adding one extra jar you can then use expressions like this:

DateUtils.addMonths(new Date(),-1)

I find that easier to maintain than first creating a helper Calendar class and then using the ternary operator but ignoring its results.

$P{cal}.add(Calendar.MONTH, -1)
? null : $P{cal}.getTime()

If you ever need to generalize the solution then it's a lot easier to get a Date back from a SQL query than to get a Calendar. So you can quickly change to "DateUtils.addMonths($F{MyDate},-1)". Initializing a Calendar to match a date returned by a query isn't nearly as simple. But of course there's a certain benefit to not having to add one more .jar file. So sometimes that ternary operator technique is the quickest way to get things done.

I wrote about using the Commons Lang approach a couple of years ago here: http://mdahlman.wordpress.com/2009/09/09/jasperreports-first-dates/

天荒地未老 2024-12-14 11:30:53

我还需要上个月的特定格式。我最终结合了其他两个答案:

new java.text.SimpleDateFormat("yyyyMM").format(
    org.apache.commons.lang3.time.DateUtils.addMonths(
        new java.util.Date(),
        -6
    )
)

这样我就不需要添加另一个参数。添加 Commons Lang jar 对我来说不是问题,因为 JasperServer 5.5 附带了开箱即用的 3.0 版本。

我希望这对像我一样偶然发现此页面的人有所帮助。

I also needed a certain format for the previous month. I ended up combining the other two answers:

new java.text.SimpleDateFormat("yyyyMM").format(
    org.apache.commons.lang3.time.DateUtils.addMonths(
        new java.util.Date(),
        -6
    )
)

This way I don't need to add another parameter. Adding the Commons Lang jar is a non issue for me since JasperServer 5.5 comes with version 3.0 out of the box.

I hope this helps someone who stumples upon this page, just like I did.

习ぎ惯性依靠 2024-12-14 11:30:53

我找到了一个非常巧妙的可行解决方案(不,我没有想出它)。我在这里找到了。其要点是创建一个名为 call 的参数,默认值为:

Calendar.getInstance()

并取消选中“用作提示”选项。然后在文本字段表达式中,您将执行

new java.text.SimpleDateFormat("MMMMM").format(
    (
     $P{cal}.add(Calendar.MONTH, -1)
    ? null : $P{cal}.getTime()
    )
)

以下操作: 它将设置日历实例的默认值,然后执行 add 方法,该方法将解析为 false,因此它将返回 getTime() 方法的结果按照我想要的方式格式化。

I found a working solution that is pretty ingenious (no I did not come up with it). I found it here. The gist of it is create a parameter called call, with a default value of:

Calendar.getInstance()

and un-check the option 'Use as a prompt'. Then in your text field expression you would do:

new java.text.SimpleDateFormat("MMMMM").format(
    (
     $P{cal}.add(Calendar.MONTH, -1)
    ? null : $P{cal}.getTime()
    )
)

What happens is it will set the default value for the calendar instance, then execute the add method, which will resolve to false, so then it will then return the result from getTime() method which gets formatted how I want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文