Word programming supplement-Practical Application of word Printing

Source: Internet
Author: User

The actual report printing may need to write data of the table type and the report topic, time, and unit at the same time. Therefore, you need to process a single piece of Scattered Data and table data with rules at the same time. You can rewrite the InsertBlemDatatoBookmark method. (For more information, see the previous documents)

The full text of the method is as follows: (including instructions)

Parameter: FileName -- Report Template File Name

Savefilename -- name of the file saved after the report is generated

Dt-report table data

Has-scattered report data

Introw-template row where the report table data table header is located

Public void InsertBlemDatatoBookmark (string FileName, System. Data. DataTable dt,
String savefilename, System. Collections. Hashtable has, int introw)
{
// Open the document
Microsoft. Office. Interop. Word. Document wDoc = null;
Microsoft. Office. Interop. Word. Application wApp = null;
This. OpenWordDoc (FileName, ref wDoc, ref wApp );
Object oEndOfDoc = "http://www.cnblogs.com/Andmm/admin/file://endofdoc ";
Object missing = System. Reflection. Missing. Value;

// Search for table tags-list all non-table tags (excluding table tags)
System. Collections. IEnumerator enu = wApp. ActiveDocument. Bookmarks. GetEnumerator ();

Microsoft. Office. Interop. Word. Bookmark bk = null;
Object tableobj = null;
While (enu. MoveNext ())
{
Bk = (Microsoft. Office. Interop. Word. Bookmark) enu. Current;

If (bk. Name. ToString (). Trim (). IndexOf ("table")>-1)
{
Tableobj = bk. Name. ToString (); break;
}
}

// Write table data-table-containing labels
// Unit object definition
Object unith = Microsoft. Office. Interop. Word. WdUnits. wdRow; // table row Mode
Object extend = Microsoft. Office. Interop. Word. WdMovementType. wdExtend;/*** // extended selection of the cursor moving area
Object unitu = Microsoft. Office. Interop. Word. WdUnits. wdLine; // you can view a table row as a document row. It is different from wdRow.
Object unitp = Microsoft. office. interop. word. wdUnits. wdParagraph; // The section method. You can select a carriage break after the table row is selected. The simplest way to find the rows that are merged across rows is as follows:

If (tableobj! = Null)
{
// Move the cursor to the current insert position
WApp. ActiveDocument. Bookmarks. get_Item (ref tableobj). Select ();
Microsoft. Office. Interop. Word. Table table = wApp. Selection. Tables [1];

// The next row in the table Header

Int r = introw + 1;
For (int I = 0; I <dt. Rows. Count; I ++)
{
For (int j = 0; j <dt. Columns. Count; j ++)
{
Table. Cell (r, j + 1). Range. Text = dt. Rows [I] [j]. ToString (). Trim ();
} //

// Prepare to insert a new empty table row

// Obtain the page number of the current cursor

Object strpage1 = wApp. Selection. get_Information (WdInformation. wdActiveEndAdjustedPageNumber );
// New Line
If (I + 1 <dt. Rows. Count)
{
Table. Rows. Add (ref missing );
}
Else
{
Break;
}

Object count = table. Rows. Count; // volume of cursor Movement
WApp. Selection. Move (ref unith, ref count );

// Obtain the current page number after the new row
Object strpage2 = wApp. Selection. get_Information (WdInformation. wdActiveEndAdjustedPageNumber );

// If the page number is inconsistent, You need to insert a new header (this is a single row header. For details about complex headers, refer to the following section)
If (strpage2.ToString ()! = Strpage1.ToString ())
{// Insert the header
WApp. Selection. Tables [1]. Cell (introw, 1). Select ();
WApp. Selection. HomeKey (ref unith, ref missing );
WApp. Selection. EndKey (ref unith, ref extend );
WApp. Selection. Copy ();
WApp. Selection. Move (ref unith, ref count );
WApp. Selection. Paste ();
R ++;
} // If

// Insert row + 1
R ++;
}
}

// Write other data
If (has! = Null & has. Count> 0)
{
Object tempobject = null;
// Int length = 0;
Foreach (System. Collections. DictionaryEntry d in has)
{
Tempobject = d. Key. ToString ();

If (wApp. ActiveDocument. Bookmarks. Exists (d. Key. ToString ()))
{
WApp. ActiveDocument. Bookmarks. get_Item (ref tempobject). Select ();

WApp. Selection. Text = d. Value. ToString ();

// Temporarily blocked. Tag data is used
// Length = dt. Rows [0] [strbook [I]. ToString (). Length;
// WApp. ActiveDocument. Bookmarks. get_Item (ref tempobject). End = wApp. ActiveDocument. Bookmarks. get_Item (ref tempobject). End + length;
}

}
}

// Close the work
Object o = null;

Object sFileName = savefilename;

If (wDoc. SaveFormat = (int) Microsoft. Office. Interop. Word. WdSaveFormat. wdFormatDocument)
{
WDoc. Application. ActiveDocument. SaveAs (ref sFileName, ref missing, ref missing,
Ref missing,
Ref missing, ref missing,
Ref missing, ref missing,
Ref missing,
Ref missing, ref missing, ref missing );
}

WDoc. Close (ref missing, ref missing, ref missing );
WApp. Quit (ref missing, ref missing, ref missing );

If (wDoc! = Null)
{
System. Runtime. InteropServices. Marshal. ReleaseComObject (wDoc );
WDoc = null;
}

If (wApp! = Null)
{
System. Runtime. InteropServices. Marshal. ReleaseComObject (wApp );
WApp = null;
}

GC. Collect ();
}

 

 

 

  

# Region multi-row complex table header acquisition method (simple row-moving replication does not allow copying complex table headers. This should be a word defect and must be extended to the end of the line)

//// Locate the 1st cell in the table
// WApp. Selection. Tables [1]. Cell (1, 1). Select ();
//// Locate the 1st cells before 1st characters
// WApp. Selection. HomeKey (ref unith, ref missing );
//// Expand to the end of the row and select the table's 1st rows
// WApp. Selection. EndKey (ref unith, ref extend );
/// Define the number of rows in the table title. titlerow is a parameter.
// Object strtitlerow = titlerow-1;
/// Move the cursor and select the end section marker of the row 1st
// WApp. Selection. MoveDown (ref unitp, ref count, ref extend );
/// Select the next row. For example, if the last column of the table title is merged, only two rows are selected.
// WApp. Selection. MoveDown (ref unitu, ref strtitlerow, ref extend );
//// Extend to the end of the row to ensure that all merged rows can be selected
// WApp. Selection. EndKey (ref unith, ref extend );
//// Copy the selected content to the clipboard
// WApp. Selection. Copy ();
//// The following shows how to move the cursor to any position and paste the content. In my program, the header of the next page is automatically inserted when the table is changed.
// WApp. Selection. Tables [1]. Cell (System. Convert. ToInt32 (strRownum), 1). Select ();
// WApp. Selection. HomeKey (ref unith, ref missing );
// WApp. Selection. Paste ();

# Endregion

--------------------------------------------------------------

Do not worry about complex things, do not waste things, have words to believe, have no desire, just.

And if the spring breeze, Su if the autumn cream, like the money, outcircle inside.

--- Gu Yanpei?

----------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.