我编写了一组模块,将数据解析为.json文件,以便上传到ArcGIS。一切都很好,除了当我尝试上传.json文件时,它崩溃并说有意外的字符。当我在NotePad中打开.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
答案 0 :(得分: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