How to use the word documents generated by C #

Source: Internet
Author: User

The implementation code is as follows:

Public class BiultReportForm
{
/// <SUMMARY> </SUMMARY>
/// Word Application Object
///
Private Microsoft. Office. Interop. Word. Application _ wordApplication;

/// <SUMMARY> </SUMMARY>
/// Word file object
///
Private Microsoft. Office. Interop. Word. Document _ wordDocument;
/// <SUMMARY> </SUMMARY>
/// Create a document
///
Public void CreateAWord ()
{
// Instantiate the word Application Object
This. _ wordApplication = new Microsoft. Office. Interop. Word. ApplicationClass ();
Object myNothing = System. Reflection. Missing. Value;

This. _ wordDocument = this. _ wordApplication. Documents. Add (ref myNothing, ref myNothing );
}
/// <SUMMARY> </SUMMARY>
/// Add a header
///
/// <PARAM name = "pPageHeader"/>
Public void SetPageHeader (string pPageHeader)
{
// Add a header
This. _ wordApplication. ActiveWindow. View. Type = Microsoft. Office. Interop. Word. WdViewType. wdOutlineView;
This. _ wordApplication. ActiveWindow. View. SeekView = Microsoft. Office. Interop. Word. WdSeekView. wdSeekPrimaryHeader;
This. _ wordApplication. ActiveWindow. ActivePane. Selection. InsertAfter (pPageHeader );
// Set the center alignment
This. _ wordApplication. Selection. ParagraphFormat. Alignment = Microsoft. Office. Interop. Word. WdParagraphAlignment. wdAlignParagraphCenter;
// Jump out of the header settings
This. _ wordApplication. ActiveWindow. View. SeekView = Microsoft. Office. Interop. Word. WdSeekView. wdSeekMainDocument;
}
/// <SUMMARY> </SUMMARY>
/// Insert text
///
/// <PARAM name = "pText"/> text information
/// <PARAM name = "pFontSize"/> small font size
/// <PARAM name = "pFontColor"/> font color
/// <PARAM name = "pFontBold"/> bold
/// <PARAM name = "ptextAlignment"/> direction
Public void InsertText (string pText, int pFontSize, Microsoft. Office. Interop. Word. WdColor pFontColor, int pFontBold, Microsoft. Office. Interop. Word. WdParagraphAlignment ptextAlignment)
{
// Set the font style and direction
This. _ wordApplication. Application. Selection. Font. Size = pFontSize;
This. _ wordApplication. Application. Selection. Font. Bold = pFontBold;
This. _ wordApplication. Application. Selection. Font. Color = pFontColor;
This. _ wordApplication. Application. Selection. ParagraphFormat. Alignment = ptextAlignment;
This. _ wordApplication. Application. Selection. TypeText (pText );
}


/// <SUMMARY> </SUMMARY>
/// Line feed
///
Public void NewLine ()
{
// Line feed
This. _ wordApplication. Application. Selection. TypeParagraph ();
}
/// <SUMMARY> </SUMMARY>
/// Insert an image
///
/// <PARAM name = "pPictureFileName"/>
Public void InsertPicture (string pPictureFileName)
{
Object myNothing = System. Reflection. Missing. Value;
// Center the image
This. _ wordApplication. Selection. ParagraphFormat. Alignment = Microsoft. Office. Interop. Word. WdParagraphAlignment. wdAlignParagraphCenter;
This. _ wordApplication. Application. Selection. InlineShapes. AddPicture (pPictureFileName, ref myNothing );
& N

Related Article

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.