1
private
string
CreateWordFile()
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
string message = "";
4
try
5![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
Object Nothing = System.Reflection.Missing.Value;
7
Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录
8
string name = "CNSI_1"+".doc";
9
object filename = "C://CNSI//" + name; //文件保存路径
10
//创建Word文档
11
Microsoft.Office.Interop.Word.Application WordApp =
12
new Microsoft.Office.Interop.Word.ApplicationClass();
13
Microsoft.Office.Interop.Word.Document WordDoc =
14
WordApp.Documents.Add(ref Nothing, ref Nothing,
15
ref Nothing, ref Nothing);
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
//添加页眉
18
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
19
WordApp.ActiveWindow.View.SeekView =
20
WdSeekView.wdSeekPrimaryHeader;
21
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
22
//设置右对齐
23
WordApp.Selection.ParagraphFormat.Alignment =
24
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
25
//跳出页眉设置
26
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
//移动焦点并换行
31
object count = 14;
32
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
33
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
34
WordApp.Selection.TypeParagraph();//插入段落
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
//文档中创建表格
37
Microsoft.Office.Interop.Word.Table newTable =
38
WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3,
39
ref Nothing, ref Nothing);
40
//设置表格样式
41
newTable.Borders.OutsideLineStyle =
42
Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
43
newTable.Borders.InsideLineStyle =
44
Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
45
newTable.Columns[1].Width = 100f;
46
newTable.Columns[2].Width = 220f;
47
newTable.Columns[3].Width = 105f;
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
//填充表格内容
50
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
51
newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
52
//合并单元格
53
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
54
WordApp.Selection.Cells.VerticalAlignment =
55
Microsoft.Office.Interop.Word.WdCellVerticalAlignment.
56
wdCellAlignVerticalCenter;//垂直居中
57
WordApp.Selection.ParagraphFormat.Alignment =
58
Microsoft.Office.Interop.Word.WdParagraphAlignment.
59
wdAlignParagraphCenter;//水平居中
60
61
//填充表格内容
62
newTable.Cell(2, 1).Range.Text = "产品基本信息";
63
//设置单元格内字体颜色
64
newTable.Cell(2, 1).Range.Font.Color =
65
Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
66
//合并单元格
67
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
68
WordApp.Selection.Cells.VerticalAlignment =
69
Microsoft.Office.Interop.Word.WdCellVerticalAlignment. wdCellAlignVerticalCenter;
70![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
//填充表格内容
72
newTable.Cell(3, 1).Range.Text = "品牌名称:";
73
newTable.Cell(3, 2).Range.Text = "BrandName";
74
//纵向合并单元格
75
newTable.Cell(3, 3).Select();//选中一行
76
object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
77
object moveCount = 5;
78
object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
79
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
80
WordApp.Selection.Cells.Merge();
81
//插入图片
82
string FileName =
83
@"C:\Documents and Settings\longwei\My Documents\My Pictures"+
84
"\新建文件夹\U1982P28T3D1798647F326DT20071119152225.jpg";//图片所在路径
85
object LinkToFile = false;
86
object SaveWithDocument = true;
87
object Anchor = WordDoc.Application.Selection.Range;
88
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName,
89
ref LinkToFile, ref SaveWithDocument, ref Anchor);
90
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 200f;//图片宽度
91
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 200f;//图片高度
92
//将图片设置为四周环绕型
93
Microsoft.Office.Interop.Word.Shape s =
94
WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
95
s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
96
97
newTable.Cell(12, 1).Range.Text = "产品特殊属性";
98
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
99
//在表格中增加行
100
WordDoc.Content.Tables[1].Rows.Add(ref Nothing);
101
102
WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" +
103
DateTime.Now.ToString();//“落款”
104
WordDoc.Paragraphs.Last.Alignment =
105
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
106![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
//文件保存
112
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing,
113
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
114
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
115
ref Nothing, ref Nothing);
116
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
117
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
118
message=name+@"文档生成成功,以保存到C:\CNSI\下";
119
this.richTextBox1.Text = message;
120
}
121
catch
122![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
message = "文件导出异常!";
124
MessageBox.Show(message);
125
}
126
return message;
127
}
128
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
93
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
96
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
98
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
99
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
100
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
101
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
102
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
103
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
104
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
105
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
107
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
115
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
117
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
119
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
120
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
121
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
122
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
123
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
124
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
125
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
126
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
127
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
128
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)