C # sample code for generating Word documents

Source: Internet
Author: User

C # sample code for generating Word documents

This article mainly introduces the sample code for generating a Word document in C #. This article provides examples of Code directly. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

Public bool CreateWordFile (string _ filename, "data List or your C # data to be written ")

{

# Region starts generating Word

Try

{

 

String strtitle = "task export ";

Object oEndOfDoc = "// endofdoc ";

Object Nothing = System. Reflection. Missing. Value;

Object filename = _ filename;

// Create a Word document

 

Microsoft. Office. Interop. Word. Application WordApp = new Microsoft. Office. Interop. Word. ApplicationClass ();

Microsoft. Office. Interop. Word. Document WordDoc = WordApp. Documents. Add (ref Nothing, ref Nothing );

 

 

// Set the header

WordApp. ActiveWindow. View. Type = Microsoft. Office. Interop. Word. WdViewType. wdOutlineView;

WordApp. ActiveWindow. View. SeekView = Microsoft. Office. Interop. Word. WdSeekView. wdSeekPrimaryHeader;

WordApp. ActiveWindow. ActivePane. Selection. InsertAfter ("WPFOA task export ");

WordApp. Selection. ParagraphFormat. Alignment = Microsoft. Office. Interop. Word. WdParagraphAlignment. wdAlignParagraphRight; // set the right Alignment

WordApp. ActiveWindow. View. SeekView = Microsoft. Office. Interop. Word. WdSeekView. wdSeekMainDocument; // jump out of the header settings

// Task export ------ name

Word. Paragraph oPara1;

OPara1 = WordDoc. Content. Paragraphs. Add (ref Nothing );

OPara1.Range. Text = strtitle;

OPara1.Range. Font. Bold = 1;

OPara1.Range. Font. Name = "";

OPara1.Range. Font. Size = 20;

OPara1.Range. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphCenter;

OPara1.Format. SpaceAfter = 5; // 24 pt spacing after paragraph.

OPara1.Range. InsertParagraphAfter ();

# Region loop each table

 

Foreach (var v in lst_task)

{

# Region loops through each column to generate a row of data

// Description

Word. Paragraph oPara3;

Object oRng = WordDoc. Bookmarks. get_Item (ref oEndOfDoc). Range;

OPara3 = WordDoc. Content. Paragraphs. Add (ref oRng );

OPara3.Range. Text = "";

OPara3.Range. Font. Bold = 0;

OPara3.Range. Font. Name = "";

OPara3.Range. Font. Size = 9;

OPara3.Range. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphCenter;

OPara3.Format. SpaceBefore = 1;

OPara3.Format. SpaceAfter = 1;

OPara3.Range. InsertParagraphAfter ();

 

// Insert table

Word. Table newTable;

Word. Range wrdRng = WordDoc. Bookmarks. get_Item (ref oEndOfDoc). Range;

NewTable = WordDoc. Tables. Add (wrdRng, 5, 4, ref Nothing, ref Nothing );

NewTable. Columns [1]. Width = 60;

NewTable. Columns [2]. Width = 145;

NewTable. Columns [3]. Width = 80;

NewTable. Columns [4]. Width = 145;

NewTable. Borders. OutsideLineStyle = Microsoft. Office. Interop. Word. WdLineStyle. wdLineStyleSingle;

NewTable. Borders. InsideLineStyle = Microsoft. Office. Interop. Word. WdLineStyle. wdLineStyleSingle;

 

// Fill in the table content

NewTable. Cell (1, 1). Range. Text = "project name ";

NewTable. Cell (1, 2). Range. Text = "1 ";

NewTable. Cell (1, 3). Range. Text = "module name ";

NewTable. Cell (1, 4). Range. Text = "2 ";

NewTable. Cell (2, 1). Range. Text = "owner name ";

NewTable. Cell (2, 2). Range. Text = "3 ";

NewTable. Cell (2, 3). Range. Text = "status ";

NewTable. Cell (2, 4). Range. Text = "4 ";

NewTable. Cell (3, 1). Range. Text = "published on ";

NewTable. Cell (3, 2). Range. Text = "5 ";

NewTable. Cell (3, 3). Range. Text = "required completion time ";

NewTable. Cell (3, 4). Range. Text = "6 ";

NewTable. Cell (4, 1). Range. Text = "completion time ";

NewTable. Cell (4, 2). Range. Text = "7 ";

NewTable. Cell (4, 3). Range. Text = "";

NewTable. Cell (4, 4). Range. Text = "8 ";

NewTable. Cell (5, 1). Range. Text = "task description ";

NewTable. Cell (5, 2). Merge (newTable. Cell (5, 4 ));

NewTable. Cell (5, 2). Range. ParagraphFormat. Alignment = Word. WdParagraphAlignment. wdAlignParagraphLeft;

NewTable. Cell (5, 2). Range. Text = "12313213123213 ";

# Endregion

}

 

 

WordDoc. SaveAs (ref filename, ref Nothing,

Ref Nothing,

Ref Nothing,

Ref Nothing, ref Nothing );

 

WordDoc. Close (ref Nothing, ref Nothing, ref Nothing );

WordApp. Quit (ref Nothing, ref Nothing, ref Nothing );

# Endregion

// Close the WinWord Process

System. Diagnostics. Process [] MyProcess = System. Diagnostics. Process. GetProcessesByName ("WINWORD ");

MyProcess [0]. Kill ();

Return true;

 

}

Catch

{

 

System. Diagnostics. Process [] MyProcess = System. Diagnostics. Process. GetProcessesByName ("WINWORD ");

MyProcess [0]. Kill ();

Return false;

}

# Endregion

}

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.