0

我编写了一组将数据解析为 .json 文件以上传到 ArcGIS 的模块。一切都很好,除了当我尝试上传 .json 文件时,它崩溃并说有意外的字符。在记事本中打开 .json 时看不到任何字符。我试过简单地重新保存文件。我已将数据写入 .txt 文件并更改了扩展名。我已经把数据删掉了,保存了空文件,然后把它粘贴回去了。

唯一可行的方法是:打开目标 .json 文件,将数据复制到 new.txt 文件中,关闭目标 .json 文件,然后将 new.txt 文件保存为目标 .json 文件。这解决了这个问题,但它非常耗时,因为我必须处理 24 个 .json 文件。

这是我的代码:

Sub WriteData()
Dim filepath As String
Dim celldata As String
Dim lastrow As String
Dim lastcol As String
Dim k As Long
Dim temp As Worksheet
Dim ouput As Worksheet
Dim fileout As Object
Dim fso As Object
'
Set temp = ActiveWorkbook.Sheets("Temp")
Set output = ActiveWorkbook.Sheets("Output Sheet")
Set fso = CreateObject("Scripting.FileSystemObject")
For i = 15 To 22
    For j = 3 To 5
        k = 3 * (i - 15) + j - 2
        filepath = Application.ActiveWorkbook.Path & "\JSON Files\" & temp.Cells(i, 1) & " " & temp.Cells(15, j) & ".json"
        Set fileout = fso.CreateTextFile(filepath, True, True)
        For l = 1 To Cells(Rows.Count, k).End(xlUp).Row + 1
            celldata = output.Cells(l, k)
            fileout.write celldata
        Next l
        fileout.Close
    Next j
Next i
Sheets("Output Sheet").Select
ActiveWorkbook.Save
End Sub
4

1 回答 1

1

我修好了它!当您使用 fileout.write 时,一定有什么事情发生了。我把它改成了一个简单的打印命令,它就可以工作了!以下是更改(我留下了旧行以供参考):

k = 3 * (i - 15) + j - 2
filepath = Application.ActiveWorkbook.Path & "\JSON Files\" & temp.Cells(i, 1) & " " & temp.Cells(15, j) & ".json"
'Set fileout = fso.CreateTextFile(filepath, True, True)
Open filepath For Output As #1
For l = 1 To Cells(Rows.Count, k).End(xlUp).Row + 1
    celldata = output.Cells(l, k)
    'fileout.write celldata
    Print #1, celldata
Next l
'fileout.Close
Close #1
于 2018-01-08T19:39:50.813 回答