A project report at hand is relatively simple. Therefore, the report printing engine uses the VBA engine to customize the Word template, then fill in the data according to the template needs, and then OK and print.
Implementation Method: first, we need to reference VBA to build it. I use Office2003 Professional and the Dll version is Microsoft Word11.0,
You also need to reference Interop. Word. Dll.
The Code is as follows:
# Region open the Word document and return the wDoc and wDoc objects.
///
/// Open the Word document and return the wDoc and wDoc objects.
///
/// Complete Word file path + name
/// Returned Word. Document wDoc object
/// Returned Word. Application Object
Public static void CreateWordDocument (string FileName, ref Word. Document wDoc, ref Word. Application WApp)
{
If (FileName = "") return;
Word. Document thisDocument = null;
Word. FormFields formFields = null;
Word. Application thisApplication = new Word. ApplicationClass ();
ThisApplication. Visible = true;
ThisApplication. Caption = "";
ThisApplication. Options. CheckSpellingAsYouType = false;
ThisApplication. Options. CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = true;
Object AddToRecentFiles = false;
Object PasswordDocument = System. Type. Missing;
Object PasswordTemplate = System. Type. Missing;
Object Revert = System. Type. Missing;
Object WritePasswordDocument = System. Type. Missing;
Object WritePasswordTemplate = System. Type. Missing;
Object Format = System. Type. Missing;
Object Encoding = System. Type. Missing;
Object Visible = System. Type. Missing;
Object OpenAndRepair = System. Type. Missing;
Object DocumentDirection = System. Type. Missing;
Object NoEncodingDialog = System. Type. Missing;
Object XMLTransform = System. Type. Missing;
Try
{
Word. Document wordDoc =
ThisApplication. Documents. Open (ref filename, ref ConfirmConversions,
Ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
Ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
Ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
Ref NoEncodingDialog, ref XMLTransform );
ThisDocument = wordDoc;
WDoc = wordDoc;
WApp = thisApplication;
FormFields = wordDoc. FormFields;
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
}
# Endregion
Call the above static method to open the target file and fill in the data in the DataGrid to the corresponding Word tag.
# Region Word filling data (For Example)
///
/// Fill data with Word
///
Private void WordLoadData ()
{
Word. Document wDoc = null;
Word. Application wApp = null;
SysFun. CreateWordDocument ("E: \ Monitoring Report (new). dot", ref wDoc, ref wApp );
// Fill the label "C"
Object bkmC = "C ";
If (wApp. ActiveDocument. Bookmarks. Exists ("C") = true)
{
WApp. ActiveDocument. Bookmarks. get_Item
(Ref bkmC). Select ();
}
WApp.Selection.TypeText(this.txt 1. Text );
Object bkmG = "TWaterTable3 ";
Object unit;
Object count; // number of moves
Object extend;
Extend = Word. WdMovementType. wdExtend;
Unit = Word. WdUnits. wdCell;
// Fill in the data in the DataGrid to the tag TWaterTable3
If (wApp. ActiveDocument. Bookmarks. Exists ("TWaterTable3") = true)
{
WApp. ActiveDocument. Bookmarks. get_Item
(Ref bkmG). Select ();
For (int I = 0; I {
If (I = 0)
{
Count = 1;
}
Else
{
Count = 0;
}
// Fill in 5 columns of data
WApp. Selection. Move (ref unit, ref count );
WApp. Selection. TypeText (gridEX1.GetRow (I). Cells [0]. Text );
Count = 1;
WApp. Selection. Move (ref unit, ref count );
WApp. Selection. TypeText (gridEX1.GetRow (I). Cells [1]. Text );
WApp. Selection. Move (ref unit, ref count );
WApp. Selection. TypeText (gridEX1.GetRow (I). Cells [2]. Text );
WApp. Selection. Move (ref unit, ref count );
WApp. Selection. TypeText (gridEX1.GetRow (I). Cells [3]. Text );
WApp. Selection. Move (ref unit, ref count );
WApp. Selection. TypeText (gridEX1.GetRow (I). Cells [4]. Text );
// Line feed
WApp. Selection. MoveRight (ref unit, ref count, ref extend );
}
}
}
# Endregion
Then it's okay. In the label table control, you should pay attention to the column loop and line feed. I don't know if there are other good methods. You are welcome to discuss it!