【问题标题】:File Export - Strange Characters文件导出 - 奇怪的字符
【发布时间】:2013-04-23 17:37:07
【问题描述】:

有人要求我复制以下 Excel VBA 代码发送的文件。问题在于“for”循环中包含的数据的导出。数量“.111”在导出文件中写为“øSã=”,“.222”写为“øSc>”,“.333”写为“ú~ª>”。

我不完全确定这里发生了什么。但无论如何,我将数据发送到的目标遗留系统都能够正确读取这些数据(即,它被转换回原始值)。

是否有人对正在发生的事情以及如何复制此行为以便可以读取我的文件有任何想法?

Type Rigo_File
Status As Integer
Invio As Integer
Codice As String * 13
Quantita As Single
Udm As Integer End Type

Type type_file
Partita As String * 10
Macchina As String * 25
articolo As String * 25
colore As String * 25
note As String * 25
urgenza As Integer
Invio As String * 3
Righi(20) As Rigo_File
End Type

Dim NrOpen As Integer
Dim NomeFile As String, NomeFileTmp As String
Dim Rigo_File
Dim type_file
Dim typeM As type_file
Dim c As Integer

Sub CREATEFILE()
FILEDIR = "C:\"
FILENAMEE = "TESTFILE1.txt"

Partita = Right(Cells(1, 3), 10)
Macchina = Left(Cells(2, 3), 25)
articolo = Left(Cells(3, 3), 25)
colore = Left(Cells(4, 3), 25)
note = Left(Cells(5, 3), 25)
urgenza = CDbl(Cells(6, 3))

With typeM
.Partita = Partita
.Macchina = Macchina
.articolo = articolo
.colore = colore
.note = note
.urgenza = urgenza
.Invio = "001"
For ci = 1 To 20
.Righi(ci).Status = True
.Righi(ci).Invio = 1
.Righi(ci).Codice = Cells(8 + ci, 2)
.Righi(ci).Quantita = Cells(8 + ci, 3)
.Righi(ci).Udm = 1
Next ci
End With

NrOpen = FreeFile
On Error GoTo 0

Open FILEDIR & FILENAMEE For Random Access Write Shared As #NrOpen Len = Len(typeM)

Put #NrOpen, 1, typeM
Close #NrOpen

结束子

【问题讨论】:

标签: vba file-io excel binary codepages


【解决方案1】:

Put # 以一种二进制格式写入数据 - 前面有长度字节的字段,将数字转换为二进制和其他有用的东西。这没问题,只要你用Get #重新读入数据。虽然您的文件不是严格人类可读的。

如果要写纯文本 ASCII 数据,请改用Print #Write #,并观察这两者之间的细微差别(分隔符、终止 CR+LF 等)。

您可能还需要在 Print #Write # 语句中单独指定 TypeM 的每个元素 - 我不确定这两个是否会接受用户定义的对象。

但要注意:可以读取Put # 创建的文件的目标系统可能会拒绝Print #Write # 创建的文件

【讨论】:

  • 我的问题是如何使用 T-SQL 复制这种二进制格式,因为这是遗留系统所能理解的。我正在尝试查找有关转换性质的一些文档。
  • ahhh ...这是一个不同的问题...好吧,我的第一个反应是让Excel读取使用Input #1从数据库获取的纯文本文件,然后使用@将其输出到另一个文件987654332@
  • 理想情况下,最好能够在 T-SQL 中复制 Put#(或更新的“BinaryWriter”)将单个写入文件的方式。
  • 现在我要离开舒适区了;也许CAST()CONVERT() 函数可以使用
【解决方案2】:

经过一番搜寻,我发现了以下内容:

    Dim bArray As Byte()
    Dim val As Single = 0.111
    Dim sChars as String
    Dim arrChars as String()
    Dim sFinal as string

    bArray = BitConverter.GetBytes(val)
    sChars = BitConverter.ToString(bArray)
    arrChars = Split(sChars, "-")
    For Each sChar as string in arrChars
        sFinal & = ChrW(Convert.ToInt32(sChar, 16))
    Next

这与 Put# 方法和 BinaryWriter.Write 方法执行相同类型的转换,无需写入文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多