2013-04-18 80 views
0

我被要求複製由以下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 

結束子

+0

如果數據正在被正確讀取,那就不用擔心了,但我會在內存中查看數字的表示形式:http://stackoverflow.com/questions/13663026/how-to-represent-floating -point-in-binary-ieee – SeanC

回答

1

創建文件的一些狩獵圍繞我發現以下後的文件:

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方法執行的轉換類型相同,不需要寫入文件。

1

Put #在一種二進制格式的寫入數據 - 長度爲字節前述字段,數字轉換爲二進制及其他有用的東西。只要您使用Get #再次讀取數據,這就沒有問題了。儘管你的文件不是人類可讀的。

如果要編寫純文本ASCII數據,請改爲使用Print #Write #,並觀察這兩個(分隔符,終止CR + LF等)之間的差別差異。

您可能還需要分別在Print #Write #聲明中指定TypeM的每個元素 - 我不確定這兩個接受用戶定義的對象。

但要注意:你的目標系統,這是正常讀取由Put #創建可以拒絕通過Print #Write #

+0

我的問題是如何使用T-SQL複製這種二進制格式,因爲這是遺留系統所能理解的。我試圖找到一些關於轉換性質的文檔。 –

+0

ahhh ...這是一個不同的問題......以及我的第一個反應是要求Excel使用'Input#1'從數據庫中獲得的純文本文件中讀取並使用'Put#2'將其輸出到另一個文件中 – MikeD

+0

理想情況下,能夠以Put#(或更新的'BinaryWriter')將單個文件寫入文件的方式在T-SQL中進行復制將會很好。 –