Customizing Microsoft Word Report Code

You can rearrange columns, change column alignments and other aspects of your report by overriding and customizing the report code in the report’s button click handler method located in:

...\<App Name>\App_Code\<Table Name>\Show<Table Name>Table.Controls.cs or .vb

For example:

...\MyApp\App_Code\Customers\ShowCustomersTable.Controls.cs

Here is an example of report code for the ShowCustomersTable.aspx page.

C#:

public virtual void CustomersWordButton_Click(object sender, ImageClickEventArgs args)

{

     try {

          DbUtils.StartTransaction();

          WordReport report = new WordReport();

          report.SpecificReportFileName = Page.Server.MapPath("ShowCustomersTable.CustomersWordButton.word");

          report.Title = "Customers";

          report.AddColumn(CustomersTable.CustomerID.Name, ReportEnum.Align.Left, "${CustomersTable.CustomerID.Name}",

              ReportEnum.Align.Left, 15);

          report.AddColumn(CustomersTable.CompanyName.Name, ReportEnum.Align.Left,

              "${CustomersTable.CompanyName.Name}", ReportEnum.Align.Left, 28);

          report.AddColumn(CustomersTable.ContactTitle.Name, ReportEnum.Align.Left, "${CustomersTable.ContactTitle.Name}",

              ReportEnum.Align.Left, 24);

 

          WhereClause whereClause = CreateWhereClause();

          OrderBy orderBy = CreateOrderBy();

          int rowsPerQuery = 1000;

          int pageNum = 0;

          int recordCount = 0;

          int totalRecords = CustomersTable.GetRecordCount(whereClause);

 

          report.Page = Page.GetResourceValue("Txt:Page", "MyApp31");

          report.ApplicationPath = this.Page.MapPath(Page.Request.ApplicationPath);

 

          ColumnList columns = CustomersTable.GetColumnList();

          CustomersRecord[] records = null;

          do

          {

              records = CustomersTable.GetRecords(whereClause, orderBy, pageNum, rowsPerQuery);

              if (records != null && records.Length > 0)

              {

                   foreach ( CustomersRecord record in records)

                   {

                        report.AddData("${CustomersTable.CustomerID.Name}", record.Format(CustomersTable.CustomerID),

                             ReportEnum.Align.Left);

                        report.AddData("${CustomersTable.CompanyName.Name}", record.Format(CustomersTable.CompanyName),

                              ReportEnum.Align.Left, 100);

                        report.AddData("${CustomersTable.ContactTitle.Name}", record.Format(CustomersTable.ContactTitle),

                             ReportEnum.Align.Left, 100);

 

                        report.WriteRow();

                   }

                   pageNum++;

                   recordCount += records.Length;

              }

          }

          while (records != null && recordCount < totalRecords);

          report.save();

          BaseClasses.Utils.NetUtils.WriteResponseBinaryAttachment(this.Page.Response, report.Title + ".doc",

              report.wordInByteArray, 0, true);

          this.Page.CommitTransaction(sender);

     } catch (Exception ex) {

          this.Page.RollBackTransaction(sender);

          this.Page.ErrorOnPage = true;

   

          BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message);

     } finally {

          DbUtils.EndTransaction();

     }

}

Visual Basic .NET:

Public Overridable Sub CustomersWordButton_Click(ByVal sender As Object, ByVal args As ImageClickEventArgs)

 

     Try

          Dim report As WordReport = New WordReport

          report.SpecificReportFileName = Page.Server.MapPath("ShowCustomersTable.CustomersWordButton.word")

          report.Title = "Customers"

 

          report.AddColumn(CustomersTable.CustomerID.Name, ReportEnum.Align.Center, "${Customers.CustomerID}",

              ReportEnum.Align.Left, 15)

          report.AddColumn(CustomersTable.CompanyName.Name, ReportEnum.Align.Center, "${Customers.CompanyName}",

              ReportEnum.Align.Left, 28)

          report.AddColumn(CustomersTable.ContactName.Name, ReportEnum.Align.Center, "${Customers.ContactName}",

              ReportEnum.Align.Left, 24)

          report.AddColumn(CustomersTable.ContactTitle.Name, ReportEnum.Align.Center, "${Customers.ContactTitle}",

              ReportEnum.Align.Left, 24)

          Dim whereClause As WhereClause = CreateWhereClause

          Dim orderBy As OrderBy = CreateOrderBy

          Dim rowsPerQuery As Integer = 1000

          Dim pageNum As Integer = 0

          Dim recordCount As Integer = 0

          Dim totalRecords As Integer = CustomersTable.GetRecordCount(whereClause)

 

          report.Page = Page.GetResourceValue("Txt:Page", "MyApp144")

          report.ApplicationPath = Me.Page.MapPath(Page.Request.ApplicationPath)

 

          Dim columns As ColumnList = CustomersTable.GetColumnList()

          Dim records As CustomersRecord() = Nothing

          Do

               records = CustomersTable.GetRecords(whereClause, orderBy, pageNum, rowsPerQuery)

               If Not (records Is Nothing) AndAlso records.Length > 0 Then

                    For Each record As CustomersRecord In records

 

                    report.AddData("${Customers.CustomerID}",  record.Format(CustomersTable.CustomerID),

                        ReportEnum.Align.Left, 100)

                    report.AddData("${Customers.CompanyName}",  record.Format(CustomersTable.CompanyName),

                        ReportEnum.Align.Left, 100)

                    report.AddData("${Customers.ContactName}",  record.Format(CustomersTable.ContactName),

                        ReportEnum.Align.Left, 100)

                    report.AddData("${Customers.ContactTitle}",  record.Format(CustomersTable. ContactTitle),

                        ReportEnum.Align.Left, 100)

                    report.WriteRow

               Next

               System.Math.Min(System.Threading.Interlocked.Increment(pageNum), pageNum-1)

               recordCount += records.Length

               End If

          Loop While Not (records Is Nothing) AndAlso recordCount < totalRecords

          report.save

          BaseClasses.Utils.NetUtils.WriteResponseBinaryAttachment(Me.Page.Response,

              report.Title + ".doc", report.wordInByteArray, 0, true)

          Me.Page.CommitTransaction(sender)

     Catch ex As Exception

          Me.Page.RollBackTransaction(sender)

          Me.Page.ErrorOnPage = True

          Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", ex.Message)

     Finally

          DbUtils.EndTransaction

     End Try

End Sub

The following sections discuss several aspects of the report generation code that are frequently customized.

The AddData function call

The AddData() function supplies data to the report.  AddData calls are generally followed by WriteRow function calls to write the data to the Microsoft Word report.  AddData()’s parameters are:

The AddColumn function call

The AddColumn function specifies the column layout.  AddColumn() is called once for each column in the report.:  AddColumn()’s parameters are:

In the example above, the first AddColumn() call uses "${Customers.CustomerID}" for the column detail.  This means that the first column displays whatever text is supplied by the first AddData() call because they both have the same substitution parameter, "${Customers.CustomerID}".

You can also use a combination of substitution parameters.  For example, to create a column with the following format:

[ContactName]

[Address]

[City], [State] [PostalCode]

Your AddColumn() calls should take the following as the third parameter:

"${Customers.ContactName}\r\n${Customers.Address}\r\n${Customers.City} ${Customers.State}, ${Customers. PostalCode }"

Notice that “\r\n” represents a new line.

Button-specific configuration file paths

In the example code, you can see:

report.SpecificReportFileName = Page.Server.MapPath("ShowCustomersTable.CustomersWordButton.word")

This code specifies the location of a button-specific configuration file.  If this file specifies a table with at least one column, your application will not use the column layout provided by the AddColumn function and instead will use the column layout provided by the button-specific configuration file.

ReportDirection

Keep in mind if the overridden property of ReportDirection is RightToLeft, the horizontal alignments and the column order will be swapped.  Therefore, the column specifies by the first AddColumn call displays on the right side, and the column specifies by the last AddColumn function call displays on the left side.

See Customizing Microsoft Word Report Configuration Files for information regarding to overridden properties.

See Also

Customizing Microsoft Word Report Configuration Files

Text Substitution Parameters for Titles, Headers, Footers and Columns

Microsoft Word Report Alignment Configuration

Microsoft Word Report Language and Culture-Based Configuration

Customizing Microsoft Word Report Code