一键生成----自动生成每日治安要情的word程序

关于程序的一些想法:

比较笨的方法是全部word文字自己打,这样程序上比较简单同时也不需要依赖模板,但这样做不够优雅。个人选择了打开模板另存为新文件的方法,代码上更复杂,出现了不少困难点。比如在文档中间插入文本我尝试了很久最后使用书签大法,这个书签要在模板word里手动加上的,而其他日期,cod数,各案件统计数则使用替换大法。还有在有内容的tb后面加上换行符而没有内容的tb不加,研究了很久最后使用遍历所有控件挑选出tb再根据textbox.text.length判断长度再加换行,有详情添加\n,无详情删除序号。这个相应限制了自定义案件名的tb的长度,算是一种妥协。最后还加入付费功能----自定义案件名textbox,每个案件名可以有5宗案件。

Button流程:

定义变量

统计每种案件的宗数,cod

打开模板

MsWord.Document wordDoc = wordApp.Documents.Add(@"E:\治安要情模板.doc");

替换日期等变量

书签处插入案件详情

判断文档是否存在,若存在弹框提示,不存在生成word并保存退出


以下是代码

using 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+"期)");
            
            MsWord.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;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值