姓名拆分转换

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 Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.International.Converters.PinYinConverter;
using NPinyin;
using System.Diagnostics;
namespace zizhuTools
{
    public partial class xmcf_fm : Form //姓名拆分
    {
        Excel.Application xmtq_ex;
        Excel.Range rng_add;
        Excel.Range rng_tqsv;
        Stopwatch sw = new Stopwatch();
        //现存复姓-库82个
        string fxk = "^(欧阳)|^(太史)|^(端木)|^(上官)|^(司马)|^(东方)|^(独孤)|^(南宫)|^(万俟)|^(闻人)|^(夏侯)|^(诸葛)|^(尉迟)|^(公羊)|^(赫连)|^(澹台)|^(皇甫)|^(宗政)|^(濮阳)|^(公冶)|^(太叔)|^(申屠)|^(公孙)|^(慕容)|^(仲孙)|^(钟离)|^(长孙)|^(宇文)|^(司徒)|^(鲜于)|^(司空)|^(闾丘)|^(子车)|^(亓官)|^(司寇)|^(巫马)|^(公西)|^(颛孙)|^(壤驷)|^(公良)|^(漆雕)|^(乐正)|^(宰父)|^(谷梁)|^(拓跋)|^(夹谷)|^(轩辕)|^(令狐)|^(段干)|^(百里)|^(呼延)|^(东郭)|^(南门)|^(羊舌)|^(微生)|^(公户)|^(公玉)|^(公仪)|^(梁丘)|^(公仲)|^(公上)|^(公门)|^(公山)|^(公坚)|^(左丘)|^(公伯)|^(西门)|^(公祖)|^(第五)|^(公乘)|^(贯丘)|^(公皙)|^(南荣)|^(东里)|^(东宫)|^(仲长)|^(子书)|^(子桑)|^(即墨)|^(达奚)|^(褚师)";
        public xmcf_fm()
        {
            InitializeComponent();

        }
        private void xmcf_fm_Load(object sender, EventArgs e)
        {
            xmtq_ex = Globals.ThisAddIn.Application;
            rng_add = xmtq_ex.Selection;//初始选择位置
            xmcf_tb.Text = rng_add.get_Address();
        }      

        private void xmcf_tb_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            rng_add = xmtq_ex.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
            xmcf_tb.Text = rng_add.get_Address();
            this.WindowState = FormWindowState.Normal;
        }

        private void xcf_btn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            rng_add = xmtq_ex.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
            xmcf_tb.Text = rng_add.get_Address();
            this.WindowState = FormWindowState.Normal;
        }

        private void tqxm_exit_btn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cfxm_en_btn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            rng_tqsv = xmtq_ex.InputBox("选择【N-1排】单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
            this.WindowState = FormWindowState.Normal;

            this.Text = "姓名拆分(程序运行中)";
            sw.Start();
            int rg_row, rg_col;
            rg_row = rng_tqsv.Cells[1].row;
            rg_col = rng_tqsv.Cells[1].Column;
            //遍历数据           
            int rg_i = rg_row;
            int rg_c = rg_col+1;
            object[,] arr = rng_add.Value;

           
            if (cfx_chk.Checked && cfm_chk.Checked)//拆分姓名
            {
                object[,] result = new object[arr.GetLength(0) + 1, 2];
                result[0, 0] = "";
                result[0, 1] = "";
                for (int i = 1; i <= arr.GetLength(0); i++)
                {
                    if (arr[i, 1] == null)
                    {
                        //arr[i, 1] = "";
                        continue;
                    }
                    System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(fxk);
                    string rnvv = arr[i, 1].ToString().Trim();
                    string aa = System.Text.RegularExpressions.Regex.Replace(rnvv, "[•·▪•.·,、。­­­]", "·");

                    int wzz = aa.IndexOf("·");
                    System.Text.RegularExpressions.Match mc = rg.Match(rnvv);
                    bool fxppp = mc.Success;
                    result[i, 0] = xscf(rnvv, fxppp, wzz);
                    result[i, 1] = xmcf(rnvv, fxppp, wzz);

                }
               
                if (xpy_btn.Checked)//全拼拆分姓名
                {
                    for (int i = 1; i < result.GetLength(0); i++)
                    {
                        for (int j = 0; j <= 1; j++)
                        {
                            if (result[i, j] == null)
                            {
                                continue;
                            }
                            string rnvv = result[i, j].ToString().Trim();
                            string clstr = "";

                            //xmzh_fm.Cells[rg_i, rg_col].Value = GetPinyin(rnvv);
                            if (rnvv.Length != 0)
                            {
                                StringBuilder fullSpell = new StringBuilder();
                                for (int z = 0; z < rnvv.Length; z++)
                                {
                                    var chr = rnvv[z];
                                    fullSpell.Append(GetSpell(chr));
                                }
                                result[i,j] = fullSpell.ToString();
                            }
                        }
                    }
                }
                rng_tqsv.Resize[result.GetLength(0), 2].Value = result;
            }
            else if (cfx_chk.Checked)//拆分姓
            {
                object[,] result = new object[arr.GetLength(0) + 1, 1];
                result[0, 0] = "";
                for (int i = 1; i <= arr.GetLength(0); i++)
                {
                    if (arr[i, 1] == null)
                    {
                        //arr[i, 1] = "";
                        continue;
                    }
                    System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(fxk);
                    string rnvv = arr[i, 1].ToString().Trim();
                    string aa = System.Text.RegularExpressions.Regex.Replace(rnvv, "[•·▪•.·,、。­­­]", "·");

                    int wzz = aa.IndexOf("·");
                    System.Text.RegularExpressions.Match mc = rg.Match(rnvv);
                    bool fxppp = mc.Success;
                    result[i, 0] = xscf(rnvv, fxppp, wzz);
                   
                }
                if (xpy_btn.Checked)//全拼拆分姓名
                {
                    for (int i = 1; i < result.GetLength(0); i++)
                    {
                        if (result[i, 0] == null)
                        {
                            continue;
                        }
                            string rnvv = result[i, 0].ToString();
                            string clstr = "";

                            //xmzh_fm.Cells[rg_i, rg_col].Value = GetPinyin(rnvv);
                            if (rnvv.Length != 0)
                            {
                                StringBuilder fullSpell = new StringBuilder();
                                for (int z = 0; z < rnvv.Length; z++)
                                {
                                    var chr = rnvv[z];
                                    fullSpell.Append(GetSpell(chr));
                                }
                                result[i, 0] = fullSpell.ToString();
                            }
                        }
                    
                }
                rng_tqsv.Resize[result.GetLength(0), 1].Value = result;
            }
            else if (cfm_chk.Checked)//拆分名
            {

                object[,] result = new object[arr.GetLength(0) + 1, 1];
                result[0, 0] = "";
                for (int i = 1; i <= arr.GetLength(0); i++)
                {
                    if (arr[i, 1] == null)
                    {
                       // arr[i, 1] = "";
                        continue;
                    }
                    System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(fxk);
                    string rnvv = arr[i, 1].ToString().Trim();
                    string aa = System.Text.RegularExpressions.Regex.Replace(rnvv, "[•·▪•.·,、。­­­]", "·");

                    int wzz = aa.IndexOf("·");
                    System.Text.RegularExpressions.Match mc = rg.Match(rnvv);
                    bool fxppp = mc.Success;
                    result[i, 0] = xmcf(rnvv, fxppp, wzz);
                }
                if (xpy_btn.Checked)//全拼拆分姓名
                {
                    for (int i = 1; i < result.GetLength(0); i++)
                    {
                        if (result[i, 0] == null)
                        {
                            continue;
                        }
                        string rnvv = result[i, 0].ToString();
                        string clstr = "";

                        //xmzh_fm.Cells[rg_i, rg_col].Value = GetPinyin(rnvv);
                        if (rnvv.Length != 0)
                        {
                            StringBuilder fullSpell = new StringBuilder();
                            for (int z = 0; z < rnvv.Length; z++)
                            {
                                var chr = rnvv[z];
                                fullSpell.Append(GetSpell(chr));
                            }
                            result[i, 0] = fullSpell.ToString();
                        }
                    }

                }
                rng_tqsv.Resize[result.GetLength(0), 1].Value = result;
            }
            
            this.Text = "姓名拆分";
            sw.Stop();
            MessageBox.Show("程序运行结束:" + sw.Elapsed, "成功");
            this.TopMost = true;
            this.TopMost = false;
        }

        public static string GetFirstSpell(string strChinese)
        {
            try
            {
                if (strChinese.Length != 0)
                {
                    StringBuilder fullSpell = new StringBuilder();
                    for (int i = 0; i < strChinese.Length; i++)
                    {
                        var chr = strChinese[i];
                        fullSpell.Append(GetSpell(chr)[0]);
                    }

                    return fullSpell.ToString().ToUpper();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("首字母转化出错!" + e.Message);
            }

            return string.Empty;
        }
        //汉字转拼音
        private static string GetSpell(char chr)
        {
            string r = "";
            var coverchr = NPinyin.Pinyin.GetPinyin(chr);

            bool isChineses = ChineseChar.IsValidChar(coverchr[0]);
            if (isChineses)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(chr);
                    string str = chineseChar.Pinyins[0];
                    str = str.Substring(0, str.Length - 1);
                    r += (String.Format("{0}{1}", str.Substring(0, 1).ToUpper(), str.Substring(1).ToLower()));
                    return r;
                }
                catch
                {
                    r += chr;
                    return r;
                }
            }
            else if (coverchr.Length <= 1)
            {
                try
                {
                    ChineseChar chineseChar = new ChineseChar(chr);
                    string str = chineseChar.Pinyins[0];
                    coverchr = str.Substring(0, str.Length - 1);
                }
                catch
                {
                    r += chr;
                    return r;
                }
            }

            r += (String.Format("{0}{1}", coverchr.Substring(0, 1).ToUpper(), coverchr.Substring(1).ToLower()));
            return r;
        }
        public string xscf(string rnv,bool fxpp,int wz)//提取姓氏
        {
            int xmlen = rnv.Length;
            string jg_xs="";
            if (fxpp)
            {
                jg_xs = rnv.Substring(0, 2);
            }
            //else if (xmlen == 3)
            //{
            //    jg_xs = rnv.Substring(0, 1);            
            //}
            else if (wz > 0)
            {               
                jg_xs = rnv.Substring(0, wz);
            }
            else
            {
                jg_xs = rnv.Substring(0, 1);
            }
            return jg_xs;
        }
        public string xmcf(string rnv, bool fxpp, int wz)//提取姓名
        {
            //复姓判断
            int xmlen = rnv.Length;
            string jg_xm="";
            if (fxpp)
            {
                jg_xm = rnv.Substring(2);
                //switch (xmlen)
                //{
                //    case 3: jg_xm = rnv.Substring(2, 1);
                //        break;
                //    case 4: jg_xm = rnv.Substring(2, 2);
                //        break;
                //}
            }          
            //else if (xmlen == 2)
            //{                
            //    jg_xm = rnv.Substring(1, 1);
            //}
            else if (wz>0)
            {
                jg_xm = rnv.Substring(wz + 1, xmlen - wz - 1);
            }
            else
            {
                jg_xm = rnv.Substring(1);
            }
            return jg_xm;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Image\\原图\\姓名拆分转换.png";
            System.Diagnostics.Process.Start(fileName);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.xxzizhu.com/index.php?ac=article&at=read&did=4885");

        }      
    }
}

 

posted on 2021-05-20 01:50  Glor  阅读(30)  评论(0)    收藏  举报

导航