如何动态地将信息从数据网格行打印到word文档 [英] How do I print info from datagrid rows to word document dynamically

查看:113
本文介绍了如何动态地将信息从数据网格行打印到word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为客户构建工资单应用程序,并且在Microsoft Word中生成报告时遇到了问题。薪水和员工的所有信息都保存在Sql Server数据库中。



所以,问题是我不知道如何动态创建报告满足他们报告的要求。

报告应该基本上显示当前,上一个或下一个支付期(我有它想要的布局,所以这不是一个问题)。



当他们转到应用程序的报告选项卡时,会显示一个数据网格,其中包含已保存的所有工资信息。

所以当他们过滤网格以显示特定的支付期并选择过滤后显示的行,打印按钮变为启用状态,并应打印所选行并将必要的信息合并到报告文档中。



我尝试了什么:



我尝试使用OpenXml进行简单的mailmerge以下操作代码(令人惊讶的是不起作用):

I'm building a payroll application for a client and have run into a problem with generating a report in Microsoft Word. All information for salary and employees are saved in a Sql Server database.

So, the problem is that I'm not sure how to go about creating the report dynamically to meet the requirements of their report.
The report should basically show the current, previous or next pay period (I have the layout of what it should like so that's not much of a problem).

When they go to the reports tab in the application, a datagrid is displayed with all the salary info that has already been saved.
So when they filter the grid to show a specific pay period and select the rows which are displayed after the filter, the print button becomes enabled and should print the selected rows and "merge" the necessary info into the report document.

What I have tried:

I've tried doing a simple mailmerge using OpenXml with the following code (which surprisingly doesn't work):

using (WordprocessingDocument doc =
        WordprocessingDocument.Open(@"E:\test.docx", true))
            {
                var body = doc.MainDocumentPart.Document.Body;
                var paras = body.Elements();

                foreach (var para in paras)
                {
                    foreach (var run in para.Elements())
                    {
                        foreach (var text in run.Elements())
                        {
                            if (text.InnerText.Contains("Name"))
                            {
                                text.InnerText.Equals(text.InnerText.Replace("Name",
                                "Bruh"));
                            }
                        }
                    }
                }

            }



现在,我不是确定mailmerge是否可行,因为如果我创建报告模板,我怎样才能为每一行重用该模板?



例如:如果John,Mary和Jack在数据网格中各有五行(我已经知道如何对货币列进行求和,所以不要有15行在报告中,货币栏总结后将变为3)如何为John,Mary和Jack使用相同的模板?




Now, I'm not sure if mailmerge is the way to go because if I create a template of the report, how could I reuse that template for each row?

For example: If John, Mary and Jack each have five rows in the datagrid (I already know how to sum the monetary columns, so instead of having 15 rows going to the report, it would become 3 after the monetary columns are summed) how can I use that same template for John, Mary and Jack?

	                           Payroll Register
                              - - - - - - - - - - -	
Period No.  period_num   period_date		            Run Date    run_date
	- - - - - - - - -Earnings- - - - - -   - - - - - -Deductions - - - - - - 
		        This Per.      Y-T-D    This Per.    This Per.   Y-T-D
	                                        Employee     Employer    Employee

Emp No		
Name		
NIS Ref		
T.R.N.		
Tax Basis		
Department		
Cost Centre		
	           TOTALS	
Rent Cheque	*  Net Pay	





这只是ONE(1)员工/行的一个例子,对于其他员工/行应该重用相同的布局/格式。



This is just an example of what it should like for ONE (1) employee/row, for other employees/rows it should reuse that same layout/format.

推荐答案

如果要将数据(不是数据网格)导出到OpenXml Word文档,我建议将其放入Word表:

如何:将表格添加到文字处理文档(Open XML SDK) [ ^ ]

如何:将表格插入文字处理文档(Open XML SDK) [ ^ ]

使用OpenXML和C#创建Word表而不使用Automation / Interop [ ^ ]



如果你想使用邮件合并,请检查:

在没有Microsoft Word的.docx文档中填写Mergefields [ ^ ]

C# OpenXML邮件合并完整示例 - Jarred Capellman [ ^ ]
If you want to export data (not datagrid) to OpenXml Word document, i'd suggest to put it into Word-table:
How to: Add tables to word processing documents (Open XML SDK)[^]
How to: Insert a table into a word processing document (Open XML SDK)[^]
Create Word table using OpenXML and C# Without Automation/Interop[^]

In case you want to use mail merge, check this:
Fill Mergefields in .docx Documents without Microsoft Word[^]
C# OpenXML Mail Merge Complete Example - Jarred Capellman[^]


解决方案可能并不困难。您需要的是一个Word模板(您已经拥有),但它必须能够使用列表组件,您可以在其中重复数据占位符的行,因此您可以在最终中获得具有相同格式的多行文献。您可以通过查看此处来了解如何创建模板以及如何将其与数据合并。



您已经在应用程序中读取数据了,所以只需要以.NET数据对象的形式提供这些数据(具有属性的类类型和这个类的实例的集合)。然后你可以用两行代码合并模板和数据:



The solution may not be that difficult at all. What you need is a Word template (which you've got already), but it has to be able to work with a list component where you can repeat rows of data placeholders, so you can get several lines with the same formatting in your final document. You can take a look here to get the idea how to create a template and how to merge it with data.

You are already reading data within your application, so all it takes is to have this data in the form of .NET data object (of class type with properties and a collection of instances of this class). Then you can merge the template and the data with just 2 lines of code:

// salaries contain a collection of data we want to merge
DocumentGenerator dg = new DocumentGenerator(salaries);
DocumentGenerationResult result = dg.GenerateDocument("MyTemplate.docx", "MyReport.docx");


这篇关于如何动态地将信息从数据网格行打印到word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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