EXCEL VBA 读AS400数据中文乱码问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EXCEL VBA 读AS400数据中文乱码问题相关的知识,希望对你有一定的参考价值。

我在EXCEL中写宏将AS400中的数据写到EXCEL中,但有汉字的字段导出的内容是乱码
下面是我写的代码,有哪位知道应该怎么改才不会出现乱码?
谢谢!
Sheet1.Cells.clear
Sheet1.Cells(1, 1).Value = "AAAA"
Sheet1.Cells(1, 2).Value = "BBBB"
Sheet1.Cells(1, 3).Value = "CCCC"
Sheet1.Cells(1, 4).Value = "DDDD"
Sheet1.Cells(1, 5).Value = "EEEE"
'link as400
'CCSID = '65535'
Dim Conn As New ADODB.Connection
Conn.Open ("rovider=IBMDA400;Data Source=000.00.000.000;User id=XXXassword=XXX;QUOTE TYPE C 65535"
Dim rs As New ADODB.Recordset
Dim i As Integer
i = 2
Sql = "select * from LIB1.FILE1 where EEEE=12345"
rs.Open Sql, Conn, 1, 3
If Not rs.EOF Then
rs.MoveFirst
Do While Not rs.EOF
Sheet1.Cells(i, 1).Value = Trim(rs("AAAA".Value)
Sheet1.Cells(i, 2).Value = Trim(rs("BBBB".Value)
Sheet1.Cells(i, 3).Value = Trim(rs("CCCC".Value)
Sheet1.Cells(i, 4).Value = Trim(rs("DDDD".Value)
Sheet1.Cells(i, 5).Value = Trim(rs("EEEE".Value)
rs.MoveNext
i = i + 1
Loop
Else
MsgBox "NO DATA!"
Sheet1.Cells.clear
EXIT SUB
End If
MsgBox "OK!"
顺便说一下,不改变AS400中文件的设置,我如果建ODBC连接的话,建ODBC的时候有个选项可以设置转出来的字符是
65535,这样导出的数据就是正常的,但像上面那样写,直接连接AS400,不知道应该在什么地方设置CCSID,代码不知道怎么写,写在什么地方

看一下你使用的User Profile的CCSID,看和你字段的CCSID能不能对上。 参考技术A 是不是AS400中的字符类型要调协为UTF-8?

VBA读excel写xml

VBA

◆写文件
Dim sFile As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

sFile.WriteLine (String(8, " ") & "<Request>")

sFile.Close
Set sFile = Nothing
Set fso = Nothing

◆选择文件夹
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folder = .SelectedItems(1)

End If
End With
MsgBox folder & "/TaskInfo.xml"


◆循环/IF
Do While Cells(rowIndex, 2).Value <> ""
For compCol = 8 To 17 Step 1

If idStr < 10 Then
sFile.WriteLine (String(12, " ") & "<Complete id=""0" & idStr & """ />")
Else
sFile.WriteLine (String(12, " ") & "<Complete id=""" & idStr & """ />")
End If

Next compCol

Loop


◆完整


Sub CreateFile()

Dim sFile As Object, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

folder = "D:"

With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
folder = .SelectedItems(1)

End If
End With
MsgBox folder & "/TaskInfo.xml"

Set sFile = fso.CreateTextFile(folder & "/TaskInfo.xml", True)

sFile.WriteLine ("<?xml version=""1.0"" encoding=""UTF-8""?>")
sFile.WriteLine ("<Tasks>")

rowIndex = 4

Do While Cells(rowIndex, 2).Value <> ""
‘ タスクID
taskId = Cells(rowIndex, 2).Value

‘ 家電対象
obj = Cells(rowIndex, 4).Value

sFile.WriteLine (String(4, " ") & "<Task id=""" & taskId & """ obj=""" & obj & """>")

sFile.WriteLine (String(8, " ") & "<Request>")


For compCol = 8 To 17 Step 1
‘ 補完ID
compId = Cells(rowIndex, compCol).Value

If compId <> "" Then

idStr = compCol - 7
If idStr < 10 Then
sFile.WriteLine (String(12, " ") & "<Complete id=""0" & idStr & """ />")
Else
sFile.WriteLine (String(12, " ") & "<Complete id=""" & idStr & """ />")
End If

End If

Next compCol


sFile.WriteLine (String(8, " ") & "</Request>")

‘ URL
URL = Cells(rowIndex, 5).Value

‘ Path
Path = Cells(rowIndex, 6).Value

‘ Method
method = Cells(rowIndex, 7).Value

sFile.WriteLine (String(8, " ") & "<Api url=""" & URL & """ path=""" & Path & """ method=""" & method & """ >")

sFile.WriteLine (String(12, " ") & "<Input>")

‘ 入力定義
inputText = Cells(rowIndex, 18).Value

sFile.WriteLine (inputText)

sFile.WriteLine (String(12, " ") & "</Input>")

sFile.WriteLine (String(8, " ") & "</Api>")

sFile.WriteLine (String(8, " ") & "<Response>")

‘ 出力応答文種別
outputStatus = Cells(rowIndex, 19).Value

sFile.WriteLine (outputStatus)

sFile.WriteLine (String(8, " ") & "</Response>")

sFile.WriteLine (String(4, " ") & "</Task>")

sFile.WriteLine

rowIndex = rowIndex + 1

Loop


sFile.WriteLine ("</Tasks>")
sFile.Close
Set sFile = Nothing
Set fso = Nothing
End Sub


Sub DeleteFile()

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile ("D:/TestFile.txt")
End Sub

 

以上是关于EXCEL VBA 读AS400数据中文乱码问题的主要内容,如果未能解决你的问题,请参考以下文章

直接从 Excel 中选择 AS400 查询记录

AS400 数据库 - 错误 - PWS98​​01 - 功能被用户出口程序拒绝

通过VBA将EXCEL的数据传给WORD

Excel 2010 索引匹配 VBA

VBA 将 excel 单元格数据导出到 .txt 文件中

Excel VBA 连接到 MS Access(读/写)到多用户表/查询