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"));
}
}
}
}
}
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.