c#操作word表格彪悍实例Word格式文档下载.docx
《c#操作word表格彪悍实例Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《c#操作word表格彪悍实例Word格式文档下载.docx(26页珍藏版)》请在冰点文库上搜索。
strFilePath)
19.
{
20.
21.
string
strEnd
=
this.txtEnd.Text.Trim().ToString();
22.
strStart
this.txtStart.Text.Trim().ToString();
23.//生成文档分页中的起始和终止页
24.
strSign
"
("
+strStart
+
-"
)"
25.
26.
//杀掉所有word进程以保证速度
27.
//KillWordProcess();
28.
29.
object
Nothing
System.Reflection.Missing.Value;
30.
missing
31.
filename=
strFilePath;
32.
33.
Word.Application
wordApp=new
Word.ApplicationClass();
34.
Word.Document
wordDoc=wordApp.Documents.Add(
35.ref
Nothing,ref
Nothing);
36.
37.
try
38.
39.
//生成过程中屏蔽返回按扭,不允许中途停止
40.
Button2.Enabled
false;
41.
#region
生成文档
42.
//设置文档宽度
43.
wordApp.Selection.PageSetup.LeftMargin
=
44.
wordApp.CentimetersToPoints(float.Parse("
2"
));
45.
wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled
11
46.
wordApp.Selection.PageSetup.RightMargin
47.
48.
49.
Object
start
Type.Missing;
50.
end
51.
unit
52.
count
53.
wordDoc.Range(ref
start,
ref
end).
54.Delete(ref
unit,
count);
55.
56.
57.
rng
58.
strInfo
this.txtNameCh.
59.Text.ToString()+"
明细表"
+strSign+"
\r\n"
60.
0;
61.
62.
63.InsertBefore(strInfo);
64.
65.Font.Name
Verdana"
66.
67.Font.Size
20;
68.
69.ParagraphFormat.Alignment
Word.WdParagraphAlignment.
70.wdAlignParagraphCenter;
71.
72.
8;
73.
strInfo.Length;
74.
75.InsertParagraphAfter();
//插入回车
76.
77.
if(DT.Rows.Count>
0)
78.
79.//C#操作Word表格之存在数据项
80.//添加一个表格
81.object
missingValue
82.object
location
83.//注:
若location超过已有字符的长度将会出错。
一定要比"
串多一个字符
84.Word.Range
rng2
location,
location);
85.
86.wordDoc.Tables.Add(rng2,
13,
6,
missingValue,
missingValue);
87.wordDoc.Tables.Item
(1).Rows.HeightRule
88.Word.WdRowHeightRule.wdRowHeightAtLeast;
89.wordDoc.Tables.Item
(1).Rows.Height
wordApp.
90.CentimetersToPoints(float.Parse("
0.8"
91.wordDoc.Tables.Item
(1).Range.Font.Size
10;
92.wordDoc.Tables.Item
(1).Range.Font.Name
宋体"
93.wordDoc.Tables.Item
(1).Range.ParagraphFormat.Alignment
94.
Word.WdParagraphAlignment.wdAlignParagraphCenter;
95.wordDoc.Tables.Item
(1).Range.Cells.VerticalAlignment
96.
Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
97.//C#操作Word表格之设置表格样式
98.wordDoc.Tables.Item
(1).Borders.
99.Item(Word.WdBorderType.wdBorderLeft).LineStyle
100.
Word.WdLineStyle.wdLineStyleSingle;
101.wordDoc.Tables.Item
(1).Borders.
102.Item(Word.WdBorderType.wdBorderLeft).LineWidth
103.
Word.WdLineWidth.wdLineWidth050pt;
104.wordDoc.Tables.Item
(1).Borders.
105.Item(Word.WdBorderType.wdBorderLeft).Color
106.Word.WdColor.wdColorAutomatic;
107.
108.wordDoc.Tables.Item
(1).Borders.
109.Item(Word.WdBorderType.wdBorderTop).LineStyle
110.Word.WdLineStyle.wdLineStyleSingle;
111.wordDoc.Tables.Item
(1).Borders.
112.Item(Word.WdBorderType.wdBorderTop).LineWidth
113.Word.WdLineWidth.wdLineWidth050pt;
114.wordDoc.Tables.Item
(1).Borders.
115.Item(Word.WdBorderType.wdBorderTop).Color
116.
Word.WdColor.wdColorAutomatic;
117.
118.wordDoc.Tables.Item
(1).Borders.
119.Item(Word.WdBorderType.wdBorderBottom).LineStyle
120.
121.wordDoc.Tables.Item
(1).Borders.
122.Item(Word.WdBorderType.wdBorderBottom).LineWidth
123.
124.wordDoc.Tables.Item
(1).Borders.
125.Item(Word.WdBorderType.wdBorderBottom).Color
126.
127.
128.wordDoc.Tables.Item
(1).Borders.
129.Item(Word.WdBorderType.wdBorderHorizontal).LineStyle
130.Word.WdLineStyle.wdLineStyleSingle;
131.wordDoc.Tables.Item
(1).Borders.
132.Item(Word.WdBorderType.wdBorderHorizontal).LineWidth
133.Word.WdLineWidth.wdLineWidth050pt;
134.wordDoc.Tables.Item
(1).Borders.
135.Item(Word.WdBorderType.wdBorderHorizontal).Color
136.Word.WdColor.wdColorAutomatic;
137.
138.wordDoc.Tables.Item
(1).Borders.
139.Item(Word.WdBorderType.wdBorderVertical).LineStyle
140.Word.WdLineStyle.wdLineStyleSingle;
141.wordDoc.Tables.Item
(1).Borders.
142.Item(Word.WdBorderType.wdBorderVertical).LineWidth
143.Word.WdLineWidth.wdLineWidth050pt;
144.wordDoc.Tables.Item
(1).Borders.
145.Item(Word.WdBorderType.wdBorderVertical).Color
146.Word.WdColor.wdColorAutomatic;
147.
148.wordDoc.Tables.Item
(1).Borders.
149.Item(Word.WdBorderType.wdBorderRight).LineStyle
150.Word.WdLineStyle.wdLineStyleSingle;
151.wordDoc.Tables.Item
(1).Borders.
152.Item(Word.WdBorderType.wdBorderRight).LineWidth
153.Word.WdLineWidth.wdLineWidth050pt;
154.wordDoc.Tables.Item
(1).Borders.
155.Item(Word.WdBorderType.wdBorderRight).Color
156.Word.WdColor.wdColorAutomatic;
157.
158.//wordDoc.Tables.Item(k).Borders.
159.Item(Word.WdBorderType.wdBorderDiagonalDown).
160.LineStyle
161.//wordDoc.Tables.Item(k).Borders.
162.Item(Word.WdBorderType.wdBorderDiagonalDown).
163.LineWidth
164.//wordDoc.Tables.Item(k).Borders.
165.Item(Word.WdBorderType.wdBorderDiagonalDown).
166.Color
167.//第一行显示
168.wordDoc.Tables.Item
(1).Cell(1,2).
169.Merge(wordDoc.Tables.Item
(1).Cell(1,3));
170.wordDoc.Tables.Item
(1).Cell(1,4).
171.Merge(wordDoc.Tables.Item
(1).Cell(1,5));
172.
173.//第二行显示
174.wordDoc.Tables.Item
(1).Cell(2,5).
175.Merge(wordDoc.Tables.Item
(1).Cell(2,6));
176.wordDoc.Tables.Item
(1).Cell(1,4).
177.Merge(wordDoc.Tables.Item
(1).Cell(2,5));
178.
179.#region
插入数据行
180.wordDoc.Tables.Item
(1).Cell(1,
1).Range.Text
cell11"
181.//wordDoc.Tables.Item(k).Cell(1,
2).Range.Text
182.
DT.Rows[i]["
].ToString();
183.
184.////******************
185.wordDoc.Tables.Item
(1).Cell(1,
3).Range.Text
cell13"
186.//wordDoc.Tables.Item(k).Cell(1,
4).Range.Text
187.
188.
189.////******************
190.wordDoc.Tables.Item
(1).Cell(2,
cell21"
191.//wordDoc.Tables.Item(k).Cell(2,
192.DT.Rows[i]["
193.
194.////******************
195.wordDoc.Tables.Item
(1).Cell(2,
cell23"
196.//wordDoc.Tables.Item(k).Cell(2,
197.
198.
199.#endregion
200.
201.#region
第三行显示
202.wordDoc.Tables.Item
(1).Cell(3,2).
203.Merge(wordDoc.Tables.Item
(1).Cell(3,3));
204.wordDoc.Tables.Item
(1).Cell(3,2).
205.Merge(wordDoc.Tables.Item
(1).Cell(3,3));
206.
207.////******************
208.wordDoc.Tables.Item
(1).Cell(3,
cell31"
209.
210.////******************
211.wordDoc.Tables.Item
(1).Cell(3,
cell33"
212.
213.#endregion
214.
215.#region
第五行显示
216.wordDoc.Tables.Item
(1).Cell(5,2).
217.Merge(wordDoc.Tables.Item
(1).Cell(5,3));
218.wordDoc.Tables.Item
(1).Cell(5,2).
219.Merge(wordDoc.Tables.Item
(1).Cell(5,3));
220.wordDoc.Tables.Item
(1).Cell(5,2).
221.Merge(wordDoc.Tables.Item
(1).Cell(5,3));
222.wordDoc.Tables.Item
(1).Cell(5,2).
223.Merge(wordDoc.Tables.Item
(1).Cell(5,3));
224.#endregion
225.
226.#region
第四行显示
227.
228.////******************
229.wordDoc.Tables.Item
(1).Cell(4,
cell41"
230.
231.////******************
232.wordDoc.Tables.Item
(1).Cell(4,
cell43"
233.
234.////******************
235.wordDoc.Tables.Item
(1).Cell(4,
5).Range.Text
cell45"
236.
237.#endregion
238.
239.#region
第六行显示
240.wordDoc.Tables.Item
(1).Cell(6,2).
241.Merge(wordDoc.Tables.Item
(1).Cell(6,3));
242.wordDoc.Tables.Item
(1).Cell(6,2).
243.Merge(wordDoc.Tables.Item
(1).Cell(6,3));
244.wordDoc.Tables.Item
(1).Cell(6,2).
245.Merge(wordDoc.Tables.Item
(1).Cell(6,3));
246.wordDoc.Tables.Item
(1).Cell(6,2).
247.Merge(wordDoc.Tables.Item
(1).Cell(6,3));
248.
249.
250.////******************
251.wordDoc.Tables.Item
(1).Cell(5,
1).
252.Range.Text
cell51"
253.
254.wordDoc.Tables.Item
(1).Cell(5,
2).
255.Range.ParagraphFormat.Alignment
256.Word.WdParagraphAlignment.wdAlignParagraphLeft;
257.
258.
259.
260.////******************
261.wordDoc.Tables.Item
(1).Cell(6,
cdll61"
262.
263.wordDoc.Tables.Item
(1).Cell(6,
264.Range.ParagraphFormat.Align