vba将msexcel表转换为json。结束线不一致

时间:2015-07-05 20:27:15

标签: json excel vba

下面的子项从Excel表格创建一个json文件,该表格以单元格(1,1)开头。问题是当我在Visual Studio中拖放生成的文件时,我收到以下消息:

  

以下文件中的行结尾不一致。你想要   标准化行结尾?

如何让这个子行产生一致的行结束?

Sub CreateWksJON(mywksht As Worksheet, NbofFields As Integer, NbofElements As Integer)
Dim FilePath As String
Dim fieldData As String
Dim i As Integer
Dim j As Integer
Dim objStream
Set objStream = CreateObject("ADODB.Stream")

objStream.Charset = "utf-8"
objStream.Open


FilePath = "E:\Git\ACWB-MSEcelWorksheetTOJson\" & mywksht.Name & ".json"

'Open FilePath For Output As #1


fieldData = "{" & Chr(10) & Chr(13) & """" & mywksht.Name & """" & ": {" & Chr(10) & Chr(13)



objStream.writetext fieldData

    fieldData = ""
For j = 1 To NbofFields
     i = 1

     fieldData = """" & mywksht.Cells(i, j).Value & """" & ": ["

    For i = 2 To NbofElements - 2
        If IsNumeric(mywksht.Cells(i, j).Value) Then

            fieldData = fieldData & mywksht.Cells(i, j).Value & ","
        Else
            fieldData = fieldData & """" & mywksht.Cells(i, j).Value & """" & ","

        End If
    Next i
        'add the last element of the field

        If IsNumeric(mywksht.Cells(i + 1, j).Value) Then

            fieldData = fieldData & mywksht.Cells(i + 1, j).Value
        Else
            fieldData = fieldData & """" & mywksht.Cells(i + 1, j).Value & """"

        End If


        If j = NbofFields Then

            fieldData = fieldData & "]" & Chr(10) & Chr(13)
        Else

            fieldData = fieldData & "]," & Chr(10) & Chr(13)
        End If

        objStream.writetext fieldData


Next j

objStream.writetext "}" & Chr(10) & Chr(13) & "}" & Chr(10) & Chr(13)

objStream.SaveToFile FilePath, 2


End Sub

这是表格

Envelpope_ID    CG_MAC  CG_Imp  CG_SI   Weight_Imp  Weight_SI
1         20    875     22.225  71700   32522.58942
1         20    875     22.225  98000   44452.0748
1         28    900     22.86   129720  58840.03207
1         38    1000    25.4    129720  58840.03207
1         38    1000    25.4    119840  54358.53718
1         43    1200    30.48   102200  46357.16372
1         43    1200    30.48   94000   42637.7044
1         34    950     24.13   71700   32522.58942
1         20    875     22.225  71700   32522.58942
2         20    875     22.225  71700   32522.58942
2         20    875     22.225  98000   44452.0748
2         28    900     22.86   125000  56699.075
2         38    1000    25.4    125000  56699.075
2         38    1200    30.48   102200  46357.16372
2         43    1200    30.48   94000   42637.7044
2         43    950     24.13   71700   32522.58942
2         34    875     22.225  71700   32522.58942

1 个答案:

答案 0 :(得分:0)

您可以更改为VbCrLf,但如果按正确顺序执行,则字符命名应该有效。

Cr是ASCII(13),Lf是ASCII(10),所以你的行结尾应该是

chr(13)&chr(10)