如何在没有数据库的情况下创建报告(RDLC)?

发布于 2024-10-21 15:21:33 字数 390 浏览 6 评论 0原文

问题

当您创建报告(RDLC)时,数据源似乎只是这个或那个数据库。有什么办法说服VS建立内存数据源的链接吗?类似于 WPF 数据绑定。

问题是,我想创建一个仅包含少量数据(由用户输入)的报告,重点是布局,我没有大量数据。因此,安装数据库,将数据写入数据库,然后获取它们只是为了显示报告,这是巨大的矫枉过正。

因此,我正在寻找从内存数据创建报告的能力。

背景

我想设计一个布局,添加图像,设置样式,字体颜色等,并添加不超过几个参数,例如“名字”,“姓氏”(用户)和“文本”。用户将输入这 3 个值,获取传单并打印 X 次。 布局必须精确——从纸张大小、图像位置、字体大小等开始。

也许有比RDLC更好的解决方案,但它是内置引擎,无论我如何搜索它总是会弹出搜索结果。

Problem

When you create a report (RDLC) the datasource seems to be only this or that database. Is there any way to convince VS to establish a link to memory data source? Something similar to WPF databinding.

The issue is, I would like to create a report with just a few data (entered by user), the whole point is layout, I don't have massive amount of data. So installing DB, writing data to DB, and then fetching them just to show the report is huge overkill.

So, I am looking for ability to create a report from memory data.

Background

I would like to design a layout, add images, set styles, font colors, etc. and add no more than few parameters like "first name", "last name" (of user) and "text". User would enter those 3 values, get a flyer and print it X times.
The layout has to be exact -- starting from paper size, the placement of images, size of fonts, etc.

Maybe there are better solutions than RDLC but it is built-in engine, and no matter how I search it always pops out in search results.

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

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

发布评论

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

评论(5

我恋#小黄人 2024-10-28 15:21:33

RDLC 报告的数据源可以是任何实现 IEnumerable 的数据源。如果它是对象的枚举,则对象的属性将成为报表中的字段。

报告的特点是它们对数据集有自己的内部概念。在设计时,您需要向报表设计者提供要使用的数据集。报告在内部提取该数据集并用于设计报告。现实情况是报告本身并不关心实际的数据集。它只关心它的模式。但是,在运行时,您为满足该数据集而提供的对象可以来自任何地方,只要它们满足相同的架构即可。

我在 MS 时代写过一篇小博客文章,展示了如何获得良好的设计时支持,然后在运行时向报告提供您想要的任何数据的技巧:

http://blogs.msdn.com /b/magreer/archive/2008/10/16/setting-the-datasource-for-a-report-at-runtime.aspx

update Microsoft 此后删除了我的博客,但是我在回程机中找到了它

https://web.archive.org/web/20160204041848/http://blogs.msdn.com/b/magreer /archive/2008/10/16/setting-the-datasource-for-a-report-at-runtime.aspx

The datasource for an RDLC report can be anything that implements IEnumerable. If it is an enumeration of objects, then the properties on the object become fields in the report.

The thing about reports is they have their own internal notion of what the dataset is. At design time you need to provide the report designer with a dataset to work with. The report ingests that dataset internally and it is used to design the report. The reality is the report itself doesn't care about the actual dataset. It only cares about its schema. However, at runtime the objects you provide to satisfy that dataset can come from anywhere, as long as they satisfy that same schema.

I have a little blog post from back in my MS days that shows a trick on how to get good design time support, and then at runtime provide the report with any data you want:

http://blogs.msdn.com/b/magreer/archive/2008/10/16/setting-the-datasource-for-a-report-at-runtime.aspx

update Microsoft has since deleted my blog, but I found it in the wayback machine

https://web.archive.org/web/20160204041848/http://blogs.msdn.com/b/magreer/archive/2008/10/16/setting-the-datasource-for-a-report-at-runtime.aspx

年华零落成诗 2024-10-28 15:21:33

我最近写了一篇关于创建报告程序集并在项目中使用它的博客文章。我的报告接受我的类列表作为数据源,并且不从数据库本身读取。

如果您查看此处:

http:// wraithnath.blogspot.com/2011/02/visual-studio-2010-report-viewer-object.html

它应该有帮助。基本上,您创建一个包含数据源的类库,因为 VS 2010 在检测对象数据源方面存在真正的问题。它在 20% 的时间内有效,这就是我决定这样做的原因。

I recently wrote a blog post on creating a reporting assembly and using it in a project. My reports accept a list of my classes as a datasource and dont read from the DB themselves.

If you have a look here:

http://wraithnath.blogspot.com/2011/02/visual-studio-2010-report-viewer-object.html

it should help. Basically you create a class library containing the datasources as VS 2010 has a real problem detecting object datasources. It works like 20% of the time which is why i decided to do it this way.

N

想念有你 2024-10-28 15:21:33

您绝对可以绑定到数据表。由于您可以手动创建数据表,因此这是无需数据库即可完成此操作的一种方法。

下面是一个示例,我们使用 DataTables 以编程方式加载 RDLC 控件以呈现 PDF:

Dim Viewer As New ReportViewer
Viewer.LocalReport.ReportPath = "Physicians\Patients\OrderPlacement\DownloadRx\RxPdf.rdlc"

Me.LoadReport(orderID, Viewer)

Dim Renderer As New Code.Reporting.RenderToPDF
Renderer.Save(Viewer, FileFullPath)

这里是 LoadReport 的内容:

Private Sub LoadReport(ByVal orderID As Integer, ByVal viewer As ReportViewer)
    'This is adapted from here: http://www.codeproject.com/KB/reporting-services/RDLC_and_DataSet.aspx
    '--Setup
    viewer.LocalReport.DataSources.Clear()
    viewer.LocalReport.EnableHyperlinks = True

    '--Configure DataSources
    Dim DocumentData As New RxDocumentData(orderID)
    Me.SetupRxPdfDataSourceHeader(DocumentData, viewer)
    Me.SetupRxPdfDataSourceMetrics(DocumentData, viewer)
    Me.SetupRxPdfDataSourceOrderHeader(DocumentData, viewer)
    Me.SetupRxPdfDataSourceOrderItems(DocumentData, viewer)
    Me.SetupRxPdfDataSourceChainOfCustody(DocumentData, viewer)
    Me.SetupRxPdfDataSourcePreTreatmentWorkupOrderTags(DocumentData, viewer)
    Me.SetupRxPdfDataSourceTakeHomeMedicationsOrderTags(DocumentData, viewer)

    viewer.LocalReport.Refresh()
End Sub

这里是这些小配置方法之一:

Private Sub SetupRxPdfDataSourceHeader(ByVal data As RxDocumentData, ByVal viewer As ReportViewer)
    Dim Dset_Header As New ReportDataSource("Dset_Header", data.HeaderDataTable)
    viewer.LocalReport.DataSources.Add(Dset_Header)
End Sub

data.HeaderDataTable 只是一个我们以编程方式创建并手动将数据放入的强类型 DataTable。

DataTable 没有什么特别的,但是要让这段代码发挥作用可能需要整整一周的时间。希望这有帮助。

You can definitely bind to DataTables. Since you can create DataTables by hand, that's one way to do this without a database.

Here's an example where we programmatically load an RDLC control in order to render a PDF, using DataTables:

Dim Viewer As New ReportViewer
Viewer.LocalReport.ReportPath = "Physicians\Patients\OrderPlacement\DownloadRx\RxPdf.rdlc"

Me.LoadReport(orderID, Viewer)

Dim Renderer As New Code.Reporting.RenderToPDF
Renderer.Save(Viewer, FileFullPath)

And here are the contents of LoadReport:

Private Sub LoadReport(ByVal orderID As Integer, ByVal viewer As ReportViewer)
    'This is adapted from here: http://www.codeproject.com/KB/reporting-services/RDLC_and_DataSet.aspx
    '--Setup
    viewer.LocalReport.DataSources.Clear()
    viewer.LocalReport.EnableHyperlinks = True

    '--Configure DataSources
    Dim DocumentData As New RxDocumentData(orderID)
    Me.SetupRxPdfDataSourceHeader(DocumentData, viewer)
    Me.SetupRxPdfDataSourceMetrics(DocumentData, viewer)
    Me.SetupRxPdfDataSourceOrderHeader(DocumentData, viewer)
    Me.SetupRxPdfDataSourceOrderItems(DocumentData, viewer)
    Me.SetupRxPdfDataSourceChainOfCustody(DocumentData, viewer)
    Me.SetupRxPdfDataSourcePreTreatmentWorkupOrderTags(DocumentData, viewer)
    Me.SetupRxPdfDataSourceTakeHomeMedicationsOrderTags(DocumentData, viewer)

    viewer.LocalReport.Refresh()
End Sub

And here's one of those little configuration methods:

Private Sub SetupRxPdfDataSourceHeader(ByVal data As RxDocumentData, ByVal viewer As ReportViewer)
    Dim Dset_Header As New ReportDataSource("Dset_Header", data.HeaderDataTable)
    viewer.LocalReport.DataSources.Add(Dset_Header)
End Sub

data.HeaderDataTable is just a strongly typed DataTable that we create programmatically and put data into by hand.

There's nothing special about the DataTable, but getting to the point where this code was functional probably took a solid week. Hope this helps.

夜还是长夜 2024-10-28 15:21:33

您可以手动创建一个 DataTable 对象,填充其中的 Columns 集合,然后调用 NewRow()。获取结果并填充字段,然后将其传递给 Rows.Add()。这就是我一直在做的事情(真的不喜欢rdlc,与html相比,它是如此缓慢和笨重)。

You can manually create a DataTable object, populate the Columns collection in there, then call NewRow(). Take the result of that and fill the fields, then pass it to Rows.Add(). That's what I've been doing (really don't like rdlc, it's so slow and clunky compared to html).

浮世清欢 2024-10-28 15:21:33

返回业务对象的列表并将其添加为数据源:

ReportViewer.LocalReport.DataSources.Add(new ReportDataSource("Report", new List<ReportDto> { new ReportDto(businessObj) }));

ReportDto 是业务对象的包装器,所有格式设置、串联和其他与报表相关的修改均在其中完成。它仅发出报告所需的属性。

然后去添加数据集并选择ReportDto的命名空间作为数据源并选择ReportDto作为数据集。现在,您包含在 ReportDto 中的所有属性都将在设计器中可用。

Return a list of your business objects and add it as the data source:

ReportViewer.LocalReport.DataSources.Add(new ReportDataSource("Report", new List<ReportDto> { new ReportDto(businessObj) }));

ReportDto is a wrapper for your business object where all formatting, concatenations and other report related modifications are done. It emits only the properties you need for the report.

Then go to add data set and pick the ReportDto's namespace as the data source and pick ReportDto as the dataset. Now all the properties you have included in ReportDto will be available in the designer.

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