首页

文章

EXCEL里面怎么将汉字转换成拼音

发布网友 发布时间:2022-04-19 12:32

我来回答

2个回答

热心网友 时间:2023-04-25 17:48

具体比较复杂,VBA
Public Function HzToPy(Hz As String, _
Optional Sep As String = "", _
Optional NotationType As Integer = -1, _
Optional ShowInitialOnly As Boolean = False, _
Optional ShowOnlyOneChar As Boolean = False) As String
Dim hp As HZ2PY
Set hp = New HZ2PY '创建类
hp.Seperator = Sep
hp.InitialOnly = ShowInitialOnly
hp.OnlyOneChar = ShowOnlyOneChar
HzToPy = hp.GetPinYin(Hz)
HzToPy = hp.AdjustPhoneticNotation(HzToPy, NotationType)
Set hp = Nothing '释放类
End Function
'类模块HZ2PY'***************************************************************************
'*
'* Mole: HzToPy
'* Update: 2011-09-23
'* Author: tt.t
'*
'* Description: 将中文字符串转换为拼音,就这些。原先这里写了太多废话,删了。
'*
'* Theory: 原理依然是通过IFELanguage接口实现。
'* 唯一需要解释的是如何解决多音字正确注音的问题。
'* IFELanguage接口是能够正确返回很多多音字拼音的,但多音字的读音只有特定词汇中
'* 才能确认,因此在解析拼音时候不能把词拆成单字,否则多音字返回的拼音就很可能不对。
'* 之前版本中就是因为把词拆开获取拼音导致多音字拼音错误。
'* 这次的更新利用接口返回数据中标识每个拼音长度的数组实现了对返回拼音
'* 的按字拆分,无需再把词拆成字获取单个字的拼音,从而解决了多音字问题。
'* 需要说明的是,VB_MORRSLT结构就是MS文档中的MORRSLT结构,但是VBA自定义结构
'* 无法实现不按4字节对齐,使得不得不修改MORRSLT的定义方式,能这样修改只能说运气不错,
'* 因为被修改的部分刚好获取拼音用不到。
'*
'*
'* Histroy:
'* 2011-09-23
'* ● 重写主要代码,支持多音字,提高了运行效率。
'* ● 取拼音首字时,ao, ai, ei, ou, er作为首字而不是原来的第一个字母。
'* ● 为函数增加了注音方式选择,hàn可以显示为han或han4。
'* ● 函数的使用与之前版本兼容,将模块中函数代码和HZ2PY类代码覆盖之前版本即可实现升级,无需修改文档中的公式。
'* 2011-04-07
'* ● 更正CoTaskMemFree传递参数错误,消除了Win7等环境下崩溃。
'* 2007-04-03
'* ● 更正redim时vba数组默认起始值错误。
'* 2007-04-02
'* ● 最初版本,实现了由汉字获取拼音。
'*
'***************************************************************************
Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type VB_MORRSLT
dwSize As Long '4
pwchOutput As Long '4
cchOutput As Integer '2+(2),VBA内存对齐闹得,折腾了好一阵才确认问题所在,唉
Block1 As Long '4
pchInputPos As Long '4
pchOutputIdxWDD As Long '4
pchReadIdxWDD As Long '4
paMonoRubyPos As Long '4
pWDD As Long '4
cWDD As Integer '2
pPrivate As Long '4
BLKBuff As Long '4
End Type
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function CLSIDFromString Lib "ole32.dll" _
(ByVal lpszProgID As Long, pCLSID As GUID) As Long
Private Declare Function CoCreateInstance Lib "ole32" ( _
rclsid As GUID, ByVal pUnkOuter As Long, _
ByVal dwClsContext As Long, riid As GUID, _
ByRef ppv As Long) As Long
Private Declare Function DispCallFunc Lib "oleaut32" _
(ByVal pvInstance As Long, ByVal oVft As Long, _
ByVal cc As Long, ByVal vtReturn As Integer, _
ByVal cActuals As Long, prgvt As Integer, _
prgpvarg As Long, pvargResult As Variant) As Long
Private Declare Sub CoTaskMemFree Lib "ole32" (pv As Long)
Dim MSIME_GUID As GUID 'MSIME's GUID
Dim IFELanguage_GUID As GUID 'IFELanguage's GUID
Dim IFELanguage As Long 'Pointer to IFELanguage interface
Dim PinYinArray() As String
Dim HzLen As Integer
Dim pvSeperator As String
Dim pvUseSeperator As Boolean
Dim pvInitialOnly As Boolean
Dim pvOnlyOneChar As Boolean
Dim pvNonChnUseSep As Boolean
Public Function GetPinYin(HzStr As String) As String
Dim i As Integer
Dim Py As String
Dim IsPy As Boolean
GetPinYin = ""
If IFELanguage = 0 Then
GetPinYin = "未发现运行环境,请安装微软拼音2.0以上版本!"
Exit Function
End If
If HzStr = "" Then Exit Function
HzLen = Len(HzStr)
Call IFELanguage_GetMorphResult(HzStr)
For i = 1 To HzLen
Py = PinYinArray(i)
IsPy = Py <> ""
If Not IsPy Then Py = Mid(HzStr, i, 1)
If pvInitialOnly Then Py = GetInitial(Py)
If pvOnlyOneChar Then Py = VBA.Left(Py, 1)
GetPinYin = GetPinYin & Py & IIf(IsPy, pvSeperator, "")
Next i
If IsPy And pvSeperator <> "" Then GetPinYin = Left(GetPinYin, Len(GetPinYin) - 1)
End Function
Property Get Seperator() As String
Seperator = pvSeperator
End Property
Property Let Seperator(Value As String)
pvSeperator = Value
End Property
Property Get InitialOnly() As Boolean
UseSeperator = pvInitialOnly
End Property
Property Let InitialOnly(Value As Boolean)
pvInitialOnly = Value
End Property
Property Get OnlyOneChar() As Boolean
UseSeperator = pvOnlyOneChar
End Property
Property Let OnlyOneChar(Value As Boolean)
pvOnlyOneChar = Value
End Property
Public Function AdjustPhoneticNotation(Py As String, ByVal pn As Integer) As String
Dim i As Integer
Dim c As String
If pn = -1 Then
AdjustPhoneticNotation = Py
Exit Function
Else
For i = 1 To Len(Py)
c = VBA.Mid(Py, i, 1)
Select Case Asc(c)
Case VBA.Asc("ā") To VBA.Asc("à")
c = "a" & IIf(pn = 0, "", (VBA.Asc(c) - VBA.Asc("ā") + 1))
Case VBA.Asc("ē") To VBA.Asc("è")
c = "e" & IIf(pn = 0, "", (VBA.Asc(c) - VBA.Asc("ē") + 1))
Case VBA.Asc("ī") To VBA.Asc("ì")
c = "i" & IIf(pn = 0, "", (VBA.Asc(c) - VBA.Asc("ī") + 1))
Case VBA.Asc("ō") To VBA.Asc("ò")
c = "o" & IIf(pn = 0, "", (VBA.Asc(c) - VBA.Asc("ō") + 1))
Case VBA.Asc("ū") To VBA.Asc("ù")
c = "u" & IIf(pn = 0, "", (VBA.Asc(c) - VBA.Asc("ū") + 1))
Case VBA.Asc("ǖ") To VBA.Asc("ǜ")
c = "u" & IIf(pn = 0, "", (VBA.Asc(c) - VBA.Asc("ǖ") + 1))
Case VBA.Asc("ü")
c = "u"
Case VBA.Asc("ɡ")
c = "g"
End Select
AdjustPhoneticNotation = AdjustPhoneticNotation & c
Next i
End If
End Function
Private Function GetInitial(Py As String) As String
GetInitial = VBA.Mid(Py, 1, 2)
Select Case AdjustPhoneticNotation(GetInitial, 0)
Case "ch", "sh", "zh", "ao", "ai", "ei", "ou", "er"
Case Else
GetInitial = VBA.Left(GetInitial, 1)
End Select
End Function
Private Function IFELanguage_GetMorphResult(HzStr As String) As String
Dim ret As Variant
Dim pArgs(0 To 5) As Long
Dim vt(0 To 5) As Integer
Dim Args(0 To 5) As Long
Dim ResultPtr As Long
Dim TinyM As VB_MORRSLT
Dim Py() As Byte
Dim i As Integer
Dim j As Integer
Dim PinyinIndexArray() As Integer
IFELanguage_GetMorphResult = ""
If IFELanguage = 0 Then Exit Function
Args(0) = &H30000
Args(1) = &H40000100
Args(2) = Len(HzStr)
Args(3) = StrPtr(HzStr)
Args(4) = 0
Args(5) = VarPtr(ResultPtr)
For i = 0 To 5
vt(i) = vbLong
pArgs(i) = VarPtr(Args(i)) - 8
Next
Call DispCallFunc(IFELanguage, 20, 4, vbLong, 6, vt(0), pArgs(0), ret)
Call MoveMemory(TinyM, ByVal ResultPtr, Len(TinyM))
ReDim PinyinIndexArray(0 To HzLen - 1)
ReDim PinYinArray(1 To HzLen)
If TinyM.cchOutput > 0 Then
ReDim Py(0 To TinyM.cchOutput * 2 - 1)
Call MoveMemory(Py(0), ByVal TinyM.pwchOutput, TinyM.cchOutput * 2)
IFELanguage_GetMorphResult = Py
Call MoveMemory(PinyinIndexArray(0), ByVal TinyM.paMonoRubyPos + 2, HzLen * 2)
j = 0
For i = 0 To HzLen - 1
PinYinArray(i + 1) = VBA.Mid(IFELanguage_GetMorphResult, j + 1, PinyinIndexArray(i) - j)
j = PinyinIndexArray(i)
Next i
End If
Call CoTaskMemFree(ByVal ResultPtr)
End Function
Private Sub IFELanguage_Open()
Dim ret As Variant
Call DispCallFunc(IFELanguage, 4, 4, vbLong, 0, 0, 0, ret)
Call DispCallFunc(IFELanguage, 12, 4, vbLong, 0, 0, 0, ret)
End Sub
Private Sub IFELanguage_Close()
Dim ret As Variant
If IFELanguage = 0 Then Exit Sub
Call DispCallFunc(IFELanguage, 8, 4, vbLong, 0, 0, 0, ret)
Call DispCallFunc(IFELanguage, 16, 4, vbLong, 0, 0, 0, ret)
End Sub
Private Function GenerateGUID()
Dim Rlt As Long
'MSIME.China GUID = "{E4288337-873B-11D1-BAA0-00AA00BBB8C0}"
Rlt = CLSIDFromString(StrPtr("MSIME.China"), MSIME_GUID)
'IFELanguage GUID = "{019F7152-E6DB-11d0-83C3-00C04FDDB82E}"
With IFELanguage_GUID
.Data1 = &H19F7152
.Data2 = &HE6DB
.Data3 = &H11D0
.Data4(0) = &H83
.Data4(1) = &HC3
.Data4(2) = &H0
.Data4(3) = &HC0
.Data4(4) = &H4F
.Data4(5) = &HDD
.Data4(6) = &HB8
.Data4(7) = &H2E
End With
GenerateGUID = Rlt = 0
End Function
Private Sub Class_Initialize()
IFELanguage = 0
pvSeperator = ""
GenerateGUID
If CoCreateInstance(MSIME_GUID, 0, 1, IFELanguage_GUID, IFELanguage) = 0 Then Call IFELanguage_Open
End Sub
Private Sub Class_Terminate()
If IFELanguage <> 0 Then Call IFELanguage_Close
End Sub

热心网友 时间:2023-04-25 17:48

这个有在线转换的。
函数是做不到的
宏可以做到,但是这个代码非常非常复杂。
土地入股的定义 ups快递客服电话24小时 贷款记录在征信保留几年? 安徽徽商城有限公司公司简介 安徽省徽商集团新能源股份有限公司基本情况 安徽省徽商集团有限公司经营理念 2019哈尔滨煤气费怎么有税? 快手删除的作品如何恢复 体育理念体育理念 有关体育的格言和理念 什么是体育理念 万里挑一算彩礼还是见面礼 绿萝扦插多少天后发芽 绿萝扦插多久发芽 扦插绿萝多久发芽 炖牛排骨的做法和配料 网络诈骗定罪标准揭秘 “流水不争先”是什么意思? mc中钻石装备怎么做 为什么我的MC里的钻石块是这样的?我想要那种。是不是版本的问题?如果是... 带“偷儿”的诗句 “君不见巴丘古城如培塿”的出处是哪里 带“奈何”的诗句大全(229句) 里翁行()拼音版、注音及读音 带“不虑”的诗句 “鲁肃当年万人守”的出处是哪里 无尘防尘棚 进出口报关流程,越详细越好。谢谢大家指教。 双线桥不是看化合价升多少就标多少的吗?为什么CL2+2KI=2KCL+I2中I失... 出师表高锰酸钾有画面了吗 2021年幼儿园新学期致家长一封信 电脑屏幕一条黑线怎么办? 销售代理商销售代理商的特点 商业代理商业代理的特征 如何看微信有没有开通微众银行 为什么微众没有开户 微众银行怎么开户 微众银行APP开户流程是什么? 唐古拉山海拔唐古拉山海拔是多少 怎么看待取消跳广场舞的人的退休金 如何选购新鲜的蓝田水柿? 恭城水柿柿树作用 创维洗衣机使用教程 创维全自动洗衣机怎么使用 自动开门器 狗羊属相婚姻相配吗 3岁的小孩不会说话怎么办 3岁孩子不会说话,应该挂什么科? 3岁小孩不会说话正常吗 鹿茸炖乌鸡怎么做? 新型冠状肺炎吃什么药可以预防 冰箱上电后一直响 如何使用excel将汉字转换成拼音格式,求解 excel怎么文字将自动转换为拼音 excel姓名转换拼音公式 如何将EXCEL表格中的汉字转为拼音 excel表格怎么把汉字转换成拼音简码 怎样能使EXCEL里的汉字变成拼音啊? EXCEL中能否直接把中文转换成拼音? 如何在excel中把汉字转换成拼音 怎样把excel中的汉字转换成拼音 修复手机屏幕白边,除了用白边正液还可以用什么方法? 怎么操作使wps表格里出现最多数字的排在最前面? WPS表格排序出现这种情况 wps表格怎么把乱的数字排序 wps表格怎么排序成绩 怎么使用WPS的表格把表格中的数据从大到小排列? 新版WPS表格怎么设置数值的排序 WPS怎么在很多数字之间排序? wps中的表格数字排序怎么弄? WPS电子表格怎么排序阿? wps表格数据怎么排序 Excel 如何设置 把汉字变成 拼音的格式 EXCEL汉字转拼音怎么转 EXCEL中把汉字转为拼音如何操作 EXCEL中怎么把汉字转成拼音? Excel里的汉字和拼音可以互换啦,掌握小技能轻松提高工作 excel表汉字生成拼音的方法 找个靠谱的excel汉字转换拼音方法? WPS、Excel中如何将中文姓名转换成汉语拼音 Excel中姓名转换大写拼音怎么转换 没有手机除边液手机该怎么去白边 qq如何设置离线留言 如何设置QQ离线留言 离线请留言 qq离线请留言怎么设置 QQ里显示有离线请留言和离线的状态,有什么不同的? 怎么修改QQ离线留言 qq离线咋个设置自己想设置的离线留言啊!~~ QQ2007正式版怎么改离线留言 如何在QQ上语音离线留言 怎么设置QQ离线留言 怎样让自己qq留言不让别人看见
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com