C # Programming implementation to dynamically generate Word documents

Source: Internet
Author: User
Tags add object count datetime header insert range reference
How do I use C # programming to dynamically generate Word documents and populate the data? To use C # to manipulate word, first add a reference

1, add Reference->com->microsoft Word 11.0 Object Library

2, in the. cs file add

Using Word;

The following examples include C # 's creation of Word documents, inserting tables, setting styles, and so on:

(In the example code some of the data information is omitted, it is important to introduce some C # operation Word Document Method)

public string Createwordfile (string checkedinfo)

... {

String message = "";

Try

... {

Object nothing = System.Reflection.Missing.Value;

Directory.CreateDirectory ("C:/cnsi"); Create the directory where the files are located

String name = "Cnsi_" + DateTime.Now.ToShortString () + ". Doc";

Object filename = "c://cnsi//" + name; File Save path

Create a Word document

Word.Application WordApp = new Word.applicationclass ();

Word.Document WordDoc = WORDAPP.DOCUMENTS.ADD (ref nothing, ref no, ref nothing, ref nothing);

Add header

WordApp.ActiveWindow.View.Type = Wdviewtype.wdoutlineview;

WordApp.ActiveWindow.View.SeekView = Wdseekview.wdseekprimaryheader;

WordApp.ActiveWindow.ActivePane.Selection.InsertAfter ("[Header content]");

WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; Set Right alignment

WordApp.ActiveWindow.View.SeekView = wdseekview.wdseekmaindocument; Jump out of header settings

WordApp.Selection.ParagraphFormat.LineSpacing = 15f; Set line spacing for a document

Move focus and Line wrap

Object count = 14;

Object wdline = Word.WdUnits.wdLine; Change one line;

WordApp.Selection.MoveDown (ref wdline, ref count, ref nothing); Move focus

WordApp.Selection.TypeParagraph (); Insert Paragraph

Create a table in a document

Word.table newtable = WordDoc.Tables.Add (WordApp.Selection.Range, 3, ref nothing, ref nothing);

Set Table Styles

NewTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleThickThinLargeGap;

NewTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;

newtable.columns[1]. Width = 100f;

newtable.columns[2]. Width = 220f;

Newtable.columns[3]. Width = 105f;

Fill table Contents

Newtable.cell (1, 1). Range.Text = "Product detail table";

Newtable.cell (1, 1). Range.bold = 2; To set a bold font in a cell

Merging cells

Newtable.cell (1, 1). Merge (Newtable.cell (1, 3));

WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; Center vertically

WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; Center horizontally

Fill table Contents

Newtable.cell (2, 1). Range.Text = "Product basic information";

Newtable.cell (2, 1). Range.Font.Color = Word.WdColor.wdColorDarkBlue; Set the font color in a cell

Merging cells

Newtable.cell (2, 1). Merge (Newtable.cell (2, 3));

WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

Fill table Contents

Newtable.cell (3, 1). Range.Text = "brand Name:";

Newtable.cell (3, 2). Range.Text = brandname;

Merge cells vertically

Newtable.cell (3, 3). Select (); Select a row

Object moveunit = Word.WdUnits.wdLine;

Object movecount = 5;

Object moveextend = Word.WdMovementType.wdExtend;

WordApp.Selection.MoveDown (ref moveunit, ref movecount, ref moveextend);

WordApp.Selection.Cells.Merge ();

Insert Picture

string FileName = picture; The path where the picture is

Object linktofile = false;

Object savewithdocument = true;

Object Anchor = WordDoc.Application.Selection.Range;

WordDoc.Application.ActiveDocument.InlineShapes.AddPicture (FileName, ref linktofile, ref savewithdocument, ref Ancho R);

worddoc.application.activedocument.inlineshapes[1]. Width = 100f; Picture width

worddoc.application.activedocument.inlineshapes[1]. Height = 100f; Picture height

Set a picture to a four-week surround

Word.shape s = worddoc.application.activedocument.inlineshapes[1]. ConvertToShape ();

S.wrapformat.type = Word.WdWrapType.wdWrapSquare;

Newtable.cell (12, 1). Range.Text = "Product special attribute";

Newtable.cell (12, 1). Merge (Newtable.cell (12, 3));

Add rows to a table

worddoc.content.tables[1]. Rows.Add (ref nothing);

WordDoc.Paragraphs.Last.Range.Text = "Document creation time:" + DateTime.Now.ToString (); InScribe

WordDoc.Paragraphs.Last.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;

File Save

Worddoc.saveas (ref filename, ref nothing, ref no, ref nothing, ref no, ref nothing, ref nothing, Ref nothing, ref no, ref nothing, ref nothing, ref no, ref nothing, ref no, ref nothing, re f nothing);

Worddoc.close (ref nothing, ref no, ref nothing);

Wordapp.quit (ref nothing, ref no, ref nothing);

Message = name + "Document Generation succeeded to save to C:cnsi";

}

Catch

... {

Message = "File Export exception!" " ;

}

return message;

}



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.