Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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):
C#
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"));
                            }
                        }
                    }
                }

            }

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	


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.
Posted
Updated 9-Jun-17 20:46pm
v2
Comments
F-ES Sitecore 16-May-17 12:29pm    
So you're writing functionality for a client and you're effectively wanting someone to do the work for you, presumambly so you can submit it and get paid for it? Why do you think someone is going to do your job for free? If you can't do the job you're employed for then sub-contract or try a freelancer (both of which will probably do what you've just done), but no-one here is going to do your job for you.

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:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900