VB 自动读取中文首字母并自动写入数据库

VB 自动读取中文首字母并自动写入数据库,第1张

'读取单个汉字的首字母

Public Function getPYChar(char As String) As String

Dim lChar As Long

lChar = 65536 + Asc(char)

If (lChar >= 45217 And lChar <= 45252) Then getPYChar = "A"

If (lChar >= 45253 And lChar <= 45760) Then getPYChar = "B"

If (lChar >= 47761 And lChar <= 46317) Then getPYChar = "C"

If (lChar >= 46318 And lChar <= 46825) Then getPYChar = "D"

If (lChar >= 46826 And lChar <= 47009) Then getPYChar = "E"

If (lChar >= 47010 And lChar <= 47296) Then getPYChar = "F"

If (lChar >= 47297 And lChar <= 47613) Then getPYChar = "G"

If (lChar >= 47614 And lChar <= 48118) Then getPYChar = "H"

If (lChar >= 48119 And lChar <= 49061) Then getPYChar = "J"

If (lChar >= 49062 And lChar <= 49323) Then getPYChar = "K"

If (lChar >= 49324 And lChar <= 49895) Then getPYChar = "L"

If (lChar >= 49896 And lChar <= 50370) Then getPYChar = "M"

If (lChar >= 50371 And lChar <= 50613) Then getPYChar = "N"

If (lChar >= 50614 And lChar <= 50621) Then getPYChar = "O"

If (lChar >= 50622 And lChar <= 50905) Then getPYChar = "P"

If (lChar >= 50906 And lChar <= 51386) Then getPYChar = "Q"

If (lChar >= 51387 And lChar <= 51445) Then getPYChar = "R"

If (lChar >= 51446 And lChar <= 52217) Then getPYChar = "S"

If (lChar >= 52218 And lChar <= 52697) Then getPYChar = "T"

If (lChar >= 52698 And lChar <= 52979) Then getPYChar = "W"

If (lChar >= 52980 And lChar <= 53640) Then getPYChar = "X"

If (lChar >= 53689 And lChar <= 54480) Then getPYChar = "Y"

If (lChar >= 54481 And lChar <= 52289) Then getPYChar = "Z"

End Function

'读取汉字字符串的所有首字母

Public Function getPY(str As String) As String

For i = 0 To Len(str) - 1

getPY = getPY & getPYChar(Mid(str, i + 1, 1))

Next

End Function

开始写入数据:

连接数据库,我就不说了

rsopen "select from 文字表",conn,1,1

rsmovefirst

do while not rseof

rs("字段2")=getPY(rs("字段1"))

loop

rsupdate

rsclose

程序已发到你的邮箱,请查收

dim

s

as

object

=

scommexecutescalar

这句scomm已经执行过了

你下面就不能判断scommexecutescalar了

而是应该判断s是不是为null如果为null就说明执行没成功。

用FORMULA

1

,一个开源框架;里面有个JBook可以取到EXCEL中的数据;然后你在根据JBook将数据存到数据库

答案补充

导入的时候打开一个JFileChooser用来让用户选择要读的excel文件;然后JBook

WorkBook

=

new

JBook();WorkBookread(fileName,

new

ReadParams(JBookeFileExcel97));WorkBooksetSheet(0);WorkBooksetSelection(0,

0,

0,

0);这就将excel中的数据读入JBook中了,int

EndRow

=

WorkBookgetLastRow();//

总行数int

EndCol

=

WorkBookgetLastCol();//

总列数WorkBookgetNumber(row,col);

或者getText(row,col)活的excel中的数据;将这些数据从JBOOK提取出来,然后自己封装成自己需要的类型,再存到数据库就行了;说起来好像挺繁琐的,做做试试;去网上下个FORMULA

1

的jar包我现在是在java的CS模式下用的;

Private Sub Command1_Click()

Dim fileadd As String

CommonDialog1ShowOpen

CommonDialog1Filter = "xls文件(xls)|xls" '选择你要的文件

fileadd = CommonDialog1FileName

If fileadd = "" Then Exit Sub

Set xlApp = CreateObject("ExcelApplication") '创建EXCEL对象

Set xlBook = xlAppWorkbooksOpen(fileadd) '打开已经存在的EXCEL工件簿文件

xlAppVisible = False ' = True '设置EXCEL对象可见(或不可见)

Set xlSheet = xlBookWorksheets(1) '设置活动工作表

For R = 1 To 99999 '行循环

If LTrim(RTrim(xlBookWorksheets(1)Cells(R, 1))) <> "" Then

Call Dosql("INSERT INTO CB_JiXieFeiYong (danwei_name) VALUES (" & LTrim(RTrim(xlBookWorksheets(1)Cells(R, 1))) & ")")

Else

R = 99999 + 1

End If

Next R

xlAppDisplayAlerts = False '不进行安全提示 '

Set xlSheet = Nothing '

Set xlBook = Nothing '

xlAppQuit '

Set xlApp = Nothing

Unload Me

End Sub

Private Sub Dosql(ByVal tn As String) '执行SQL语句

Dim sql As String

Set conn = New ADODBConnection

connConnectionString = condstr

connOpen

connExecute tn

connClose

End Sub

这段代码估计对你有用。

必须安装mysql的驱动才行,不管是vb还是c++都需要这个驱动。

代码如下:

public

function

exemysql(byval

sql

as

string)

as

adodbrecordset

sql

=

trim$(sql)

set

conn

=

new

adodbconnection

set

rs

=

new

adodbrecordset

connopen

"driver=mysql

odbc

351

driver;"

&

_

"server=服务器地址;"

&

_

"port=3306;"

&

_

"database=数据库名;"

&

_

"uid=用户名;"

&

_

"pwd=密码"

conndefaultdatabase

=

"数据库名"

conncursorlocation

=

aduseclient

set

rsactiveconnection

=

conn

rslocktype

=

adlockbatchoptimistic

rscursortype

=

adopenkeyset

rsopen

sql

set

exesql

=

rs

set

rs

=

nothing

set

conn

=

nothing

end

function

以上就是关于VB 自动读取中文首字母并自动写入数据库全部的内容,包括:VB 自动读取中文首字母并自动写入数据库、VB.NET读取和写入SQL数据库的代码、vb.net 如何读出excel,并把数据插入oracle数据库中。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9838078.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存