关于程序的一些想法:
比较笨的方法是全部word文字自己打,这样程序上比较简单同时也不需要依赖模板,但这样做不够优雅。个人选择了打开模板另存为新文件的方法,代码上更复杂,出现了不少困难点。比如在文档中间插入文本我尝试了很久最后使用书签大法,这个书签要在模板word里手动加上的,而其他日期,cod数,各案件统计数则使用替换大法。还有在有内容的tb后面加上换行符而没有内容的tb不加,研究了很久最后使用遍历所有控件挑选出tb再根据textbox.text.length判断长度再加换行,有详情添加n,无详情删除序号。这个相应限制了自定义案件名的tb的长度,算是一种妥协。最后还加入付费功能----自定义案件名textbox,每个案件名可以有5宗案件。
Button流程:
定义变量
统计每种案件的宗数,cod数
打开模板
复制代码
1MsWord.Document wordDoc = wordApp.Documents.Add(@"E:治安要情模板.doc");
替换日期等变量
书签处插入案件详情
判断文档是否存在,若存在弹框提示,不存在生成word并保存退出
以下是代码
复制代码
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
82using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MsWord = Microsoft.Office.Interop.Word; using System.IO; using System.Reflection; namespace word { public partial class Form1 : Form { int year = DateTime.Now.Year;//定义全局变量 int month = DateTime.Now.Month; int date = DateTime.Now.Day; int _year = DateTime.Now.AddDays(-1).Year; int _month = DateTime.Now.AddDays(-1).Month;//昨天月份 int _date = DateTime.Now.AddDays(-1).Day;//昨天日期 public Form1() { InitializeComponent(); //MessageBox.Show(change2hanzi(year)+"年"+change2hanzi(month)+"月"+change2hanzi(date)+"日,第"+dayofyear+"期"); //MessageBox.Show("【盗窃案2宗】naaaaaaaaaaaa"); } public string change2hanzi(int a)//年月日中文写法 { switch (a) { case 0: return "O"; case 1: return "一"; case 2: return "二"; case 3: return "三"; case 4: return "四"; case 5: return "五"; case 6: return "六"; case 7: return "七"; case 8: return "八"; case 9: return "九"; case 10: return "十"; case 11: return "十一"; case 12: return "十二"; case 13: return "十三"; case 14: return "十四"; case 15: return "十五"; case 16: return "十六"; case 17: return "十七"; case 18: return "十八"; case 19: return "十九"; case 20: return "二十"; case 21: return "二十一"; case 22: return "二十二"; case 23: return "二十三"; case 24: return "二十四"; case 25: return "二十五"; case 26: return "二十六"; case 27: return "二十七"; case 28: return "二十八"; case 29: return "二十九"; case 30: return "三十"; case 31: return "三十一"; case 2018: return "二O一八"; case 2019: return "二O一九"; case 2020: return "二O二O"; case 2021: return "二O二一"; default: return "this is not a date."; } //return "error"; } private void button1_Click(object sender, EventArgs e)//治安情况良好 { string savename = ("xx派出所"+year+"年"+month+"月"+date+"日治安情况(第"+DateTime.Now.DayOfYear+"期)");
复制代码
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433MsWord.Application wordApp = new MsWord.ApplicationClass(); //wordApp.Visible = true; MsWord.Document wordDoc = wordApp.Documents.Add(@"E:治安要情模板(放在e盘根目录下).doc"); //wordApp.Visible = false; //不可见直接保存 object missing = Missing.Value; object findtext, replacetext, replaceall = MsWord.WdReplace.wdReplaceAll; List<string> liststr = new List<string>();//word中的替换 liststr.Add("{0}"); liststr.Add("{1}"); liststr.Add("{2}"); liststr.Add("{3}"); liststr.Add("{4}"); liststr.Add("[0]"); foreach (string str in liststr) { findtext = str; switch (str) { case "{0}": replacetext = DateTime.Now.DayOfYear; break; case "{1}": replacetext = change2hanzi(year) + "年" + change2hanzi(month) + "月" + change2hanzi(date) + "日";break; case "{2}": replacetext = _year + "年" + _month + "月" + _date + "日"; break; case "{3}": replacetext = year + "年" + month + "月" + date + "日"; break; case "{4}": replacetext = 0; break; case "[0]": replacetext = ""; break; default: replacetext = "error"; break; } //wordDoc.Content.Find.ClearFormatting(); wordDoc.Content.Find.Execute(ref findtext, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replacetext, ref replaceall, ref missing, ref missing, ref missing, ref missing); } if (System.IO.File.Exists(@"E:" + savename + ".doc"))//如果文档存在则提示 MessageBox.Show("该文件已经存在", "Error"); else wordDoc.SaveAs2(@"E:"+savename+".doc");//保存 //wordDoc.Close(true);//关闭 wordApp.Quit();//释放Word进程 } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//cb控件有选择再显示下一个cb和tb { if (comboBox1.SelectedItem != null && comboBox1.SelectedItem != "") { textBox1.Visible = true; comboBox2.Visible = true; } if (comboBox1.SelectedItem == "") { textBox1.Visible = false; comboBox2.Visible = false; } } private void button2_Click(object sender, EventArgs e)//自定义按键类型 { textBox25.Visible = true; button2.Visible = false; } private void notgood_Click(object sender, EventArgs e)//有案件的治安要请按钮 { string savename = ("xx派出所" + year + "年" + month + "月" + date + "日治安情况(第" + DateTime.Now.DayOfYear + "期)"); string casetype,casetmp="";//各案件统计数 int cod = 0, tmp1 = 0, tmp2 = 0, tmp3 = 0, tmp4 = 0, tmp5 = 0;//一共警情数,警情临时记数 if (comboBox1.SelectedItem != null&&comboBox1.SelectedItem!="") { casetmp = comboBox1.SelectedItem.ToString(); if (textBox1.Text.Length>5)//统计共计案件数和每种类型案件数 { cod++; tmp1++; if (textBox2.Text.Length > 5) { cod++; tmp1++; if (textBox3.Text.Length > 5) { cod++; tmp1++; if (textBox4.Text.Length > 5) { cod++; tmp1++; } } } } } else { MessageBox.Show("请录入案件"); return; } casetype = casetmp.ToString() + tmp1 + "宗。"; if (comboBox2.SelectedItem != null && comboBox2.SelectedItem != "") { casetmp = comboBox2.SelectedItem.ToString(); if (textBox8.Text.Length > 5) { cod++; tmp2++; if (textBox7.Text.Length > 5) { cod++; tmp2++; if (textBox6.Text.Length > 5) { cod++; tmp2++; if (textBox5.Text.Length > 5) { cod++; tmp2++; } } } } casetype += casetmp.ToString() + tmp2 + "宗。"; } if (comboBox3.SelectedItem != null && comboBox3.SelectedItem != "") { casetmp = comboBox3.SelectedItem.ToString(); if (textBox12.Text.Length > 5) { cod++; tmp3++; if (textBox11.Text.Length > 5) { cod++; tmp3++; if (textBox10.Text.Length > 5) { cod++; tmp3++; if (textBox9.Text.Length > 5) { cod++; tmp3++; } } } } casetype += casetmp.ToString() + tmp3 + "宗。"; } if (comboBox4.SelectedItem != null && comboBox4.SelectedItem != "") { casetmp = comboBox4.SelectedItem.ToString(); if (textBox16.Text.Length > 5) { cod++; tmp4++; if (textBox15.Text.Length > 5) { cod++; tmp4++; if (textBox14.Text.Length > 5) { cod++; tmp4++; if (textBox13.Text.Length > 5) { cod++; tmp4++; } } } } casetype += casetmp.ToString() + tmp4 + "宗。"; } if (textBox25.Text != "")//识别最后的textbox { casetmp = textBox25.Text.ToString(); if (textBox20.Text.Length > 5) { cod++; tmp5++; if (textBox19.Text.Length > 5) { cod++; tmp5++; if (textBox18.Text.Length > 5) { cod++; tmp5++; if (textBox17.Text.Length > 5) { cod++; tmp5++; } } } } casetype += casetmp.ToString() + tmp5 + "宗。"; } else ; MsWord.Application wordApp = new MsWord.ApplicationClass(); wordApp.Visible = true;//word可见 MsWord.Document wordDoc = wordApp.Documents.Add(@"E:治安要情模板(放在e盘根目录下).doc"); object missing = Missing.Value; object findtext, replacetext, replaceall = MsWord.WdReplace.wdReplaceAll; List<string> liststr = new List<string>(); liststr.Add("{0}"); liststr.Add("{1}"); liststr.Add("{2}"); liststr.Add("{3}"); liststr.Add("{4}"); liststr.Add("[0]"); //liststr.Add("[1]"); foreach (string str in liststr) { findtext = str; switch (str) { case "{0}": replacetext = DateTime.Now.DayOfYear; break; case "{1}": replacetext = change2hanzi(year) + "年" + change2hanzi(month) + "月" + change2hanzi(date) + "日"; break; case "{2}": replacetext = _year + "年" + _month + "月" + _date + "日"; break; case "{3}": replacetext = year + "年" + month + "月" + date + "日"; break; case "{4}": replacetext = cod; break; case "[0]": replacetext = casetype; break; //case "[1]": replacetext = cause; break; default: replacetext = "error"; break; } //wordDoc.Content.Find.ClearFormatting(); wordDoc.Content.Find.Execute(ref findtext, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replacetext, ref replaceall, ref missing, ref missing, ref missing, ref missing); } foreach (Control cur in Controls)//遍历所有控件找出有按键的textbox加换行 { if (cur is TextBox && cur.Text.Length <=2)//没有写东西的置空 { cur.Text = ""; } else if (cur is TextBox && cur.Text.Length > 5) cur.Text =" "+ cur.Text + "n"; } //还可以通过预先设置书签来添加文档 if (wordApp.ActiveDocument.Bookmarks.Exists("个v")) { wordApp.ActiveDocument.Bookmarks["个v"].Select(); wordApp.Selection.TypeText(" 【" + comboBox1.SelectedItem.ToString() + tmp1 + "宗】n" + textBox1.Text + textBox2.Text + textBox3.Text + textBox4.Text); if (comboBox2.SelectedItem != null) { wordApp.Selection.TypeText(" 【" + comboBox2.SelectedItem.ToString() + tmp2 + "宗】n" + textBox8.Text + textBox7.Text + textBox6.Text + textBox5.Text); } if (comboBox3.SelectedItem != null) { wordApp.Selection.TypeText(" 【" + comboBox3.SelectedItem.ToString() + tmp3 + "宗】n" + textBox12.Text + textBox11.Text + textBox10.Text + textBox9.Text); } if (comboBox4.SelectedItem != null) { wordApp.Selection.TypeText(" 【" + comboBox4.SelectedItem.ToString() + tmp4 + "宗】n" + textBox16.Text + textBox15.Text + textBox14.Text + textBox13.Text); } if (textBox25.Text!="") { wordApp.Selection.TypeText(" 【" +textBox25.Text.ToString() + tmp5 + "宗】n" + textBox20.Text + textBox19.Text + textBox18.Text + textBox17.Text); } } if (System.IO.File.Exists(@"E:" + savename + ".doc"))//如果文档存在则提示 { MessageBox.Show("该文件已经存在", "Error"); wordApp.Quit(); System.Environment.Exit(0); } else wordDoc.SaveAs2(@"E:" + savename + ".doc");//保存 wordDoc.Close(true);//关闭 wordApp.Quit();//释放Word进程 System.Environment.Exit(0); } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox2.SelectedItem != null && comboBox2.SelectedItem != "") { textBox8.Visible = true; comboBox3.Visible = true; } if(comboBox2.SelectedItem=="") { textBox8.Visible = false; comboBox3.Visible = false; } } private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox3.SelectedItem != null && comboBox3.SelectedItem != "") { textBox12.Visible = true; comboBox4.Visible = true; } if (comboBox3.SelectedItem == "") { textBox12.Visible = false; comboBox4.Visible = false; } } private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox4.SelectedItem != null && comboBox4.SelectedItem != "") { textBox16.Visible = true; } if (comboBox4.SelectedItem == "") { textBox16.Visible = false; } } private void textBox25_TextChanged(object sender, EventArgs e) { if (textBox25.Text != "") { textBox20.Visible = true; label1.Visible = true; } else { textBox20.Visible = false; label1.Visible = false; } } private void textBox1_TextChanged(object sender, EventArgs e) { if (textBox1.Text.Length > 2 && textBox1.Text.Substring(0, 2) == "1.") textBox2.Visible = true; else textBox2.Visible = false; } private void textBox2_TextChanged(object sender, EventArgs e) { if (textBox2.Text.Length > 2 && textBox2.Text.Substring(0, 2) == "2.") textBox3.Visible = true; else textBox3.Visible = false; } private void textBox3_TextChanged(object sender, EventArgs e) { if (textBox3.Text.Length > 2 && textBox3.Text.Substring(0, 2) == "3.") textBox4.Visible = true; else textBox4.Visible = false; } private void textBox8_TextChanged(object sender, EventArgs e) { if (textBox8.Text.Length > 2 && textBox8.Text.Substring(0, 2) == "1.") textBox7.Visible = true; else textBox7.Visible = false; } private void textBox7_TextChanged(object sender, EventArgs e) { if (textBox7.Text.Length > 2 && textBox7.Text.Substring(0, 2) == "2.") textBox6.Visible = true; else textBox6.Visible = false; } private void textBox6_TextChanged(object sender, EventArgs e) { if (textBox6.Text.Length > 2 && textBox6.Text.Substring(0, 2) == "3.") textBox5.Visible = true; else textBox5.Visible = false; } private void textBox12_TextChanged(object sender, EventArgs e) { if (textBox12.Text.Length > 2 && textBox12.Text.Substring(0, 2) == "1.") textBox11.Visible = true; else textBox11.Visible = false; } private void textBox11_TextChanged(object sender, EventArgs e) { if (textBox11.Text.Length > 2 && textBox11.Text.Substring(0, 2) == "2.") textBox10.Visible = true; else textBox10.Visible = false; } private void textBox10_TextChanged(object sender, EventArgs e) { if (textBox10.Text.Length > 2 && textBox10.Text.Substring(0, 2) == "3.") textBox9.Visible = true; else textBox9.Visible = false; } private void textBox16_TextChanged(object sender, EventArgs e) { if (textBox16.Text.Length > 2 && textBox16.Text.Substring(0, 2) == "1.") textBox15.Visible = true; else textBox15.Visible = false; } private void textBox15_TextChanged(object sender, EventArgs e) { if (textBox15.Text.Length > 2 && textBox15.Text.Substring(0, 2) == "2.") textBox14.Visible = true; else textBox14.Visible = false; } private void textBox14_TextChanged(object sender, EventArgs e) { if (textBox14.Text.Length > 2 && textBox14.Text.Substring(0, 2) == "3.") textBox13.Visible = true; else textBox13.Visible = false; } private void textBox20_TextChanged(object sender, EventArgs e) { if (textBox20.Text.Length > 2 && textBox20.Text.Substring(0, 2) == "1.") textBox19.Visible = true; else textBox19.Visible = false; } private void textBox19_TextChanged(object sender, EventArgs e) { if (textBox19.Text.Length > 2 && textBox19.Text.Substring(0, 2) == "2.") textBox18.Visible = true; else textBox18.Visible = false; } private void textBox18_TextChanged(object sender, EventArgs e) { if (textBox18.Text.Length > 2 && textBox18.Text.Substring(0, 2) == "3.") textBox17.Visible = true; else textBox17.Visible = false; } } }
最后
以上就是传统煎蛋最近收集整理的关于一键生成----自动生成每日治安要情的word程序的全部内容,更多相关一键生成----自动生成每日治安要情内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复