转拼音 首字母
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 xmzhpy : Form //姓名转换拼音 { Excel.Application xmzh_fm; Excel.Range rng_add; Excel.Range rng_tqsv; Stopwatch sw = new Stopwatch(); public xmzhpy() { InitializeComponent(); } private void xmzhpy_Load(object sender, EventArgs e) { xmzh_fm = Globals.ThisAddIn.Application; rng_add = xmzh_fm.Selection;//初始选择位置 zhtb.Text = rng_add.get_Address(); // string a = GetPinyin("强大"); // string b = GetSpell('倔强'); } private void zhtc_btn_Click(object sender, EventArgs e) { this.Close(); } private void xpy_btn_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; rng_add = xmzh_fm.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8); zhtb.Text = rng_add.get_Address(); this.WindowState = FormWindowState.Normal; } private void zhtb_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; rng_add = xmzh_fm.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8); zhtb.Text = rng_add.get_Address(); this.WindowState = FormWindowState.Normal; } private void zhbtn_en_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; rng_tqsv = xmzh_fm.InputBox("选择【同排】单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8); this.WindowState = FormWindowState.Normal; sw.Start(); object[,] result = rng_add.Value; if (szm_rb.Checked) { for (int i = 1; i <= result.GetLength(0); i++) { if (result[i, 1] == null) { continue; } string rnvv = result[i, 1].ToString(); result[i, 1] = GetFirstSpell(rnvv); } } else { for (int i = 1; i <= result.GetLength(0); i++) { if (result[i, 1] == null) { continue; } string rnvv = result[i, 1].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, 1] = fullSpell.ToString(); } } } rng_tqsv.Resize[result.GetLength(0), 1].Value = result; sw.Stop(); MessageBox.Show("程序运行结束:" + sw.Elapsed, "成功"); this.TopMost = true; this.TopMost = false; } //汉字转拼音 public static string GetPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); string tt = ""; tt += t.Substring(0, t.Length - 1); r += (String.Format("{0}{1}", tt.Substring(0, 1).ToUpper(),tt.Substring(1).ToLower())); } catch { r += obj.ToString(); } } return r; } //汉字转首拼 public static string GetFirstPinyin(string str) { string r = string.Empty; foreach (char obj in str) { try { ChineseChar chineseChar = new ChineseChar(obj); string t = chineseChar.Pinyins[0].ToString(); r += t.Substring(0,1).ToUpper(); } catch { r += obj.ToString(); } } return r; } //汉字转首拼 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; //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) //{ // ChineseChar chineseChar = new ChineseChar(chr); // string str = chineseChar.Pinyins[0]; // coverchr = str.Substring(0, str.Length - 1); //} //r += (String.Format("{0}{1}", coverchr.Substring(0, 1).ToUpper(), coverchr.Substring(1).ToLower())); //return r; } 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=4886"); } } }