Excel是一款廣泛應用的電子表格軟件,它可以通過一些工具將電子表格中的數據導出為json格式,方便我們在各種應用程序中使用。
Sub ExportToJSON() Dim arr As Variant Dim i As Long, j As Long, k As Long Dim json As String Dim currRow As String 'Get data from range A1:C10 arr = Range("A1:C10").Value 'Create JSON structure json = "{""data"":[" 'Loop through rows For i = LBound(arr, 1) To UBound(arr, 1) 'Create row structure currRow = "{" For j = LBound(arr, 2) To UBound(arr, 2) 'Add column values to row structure currRow = currRow & """" & Cells(1, j) & """:" & """" & arr(i, j) & """" 'Add comma if not last column If j< UBound(arr, 2) Then currRow = currRow & "," End If Next j 'Close row structure and add to JSON string currRow = currRow & "}" json = json & currRow 'Add comma if not last row If i< UBound(arr, 1) Then json = json & "," End If Next i 'Close JSON structure json = json & "]}" 'Output JSON data to file Open "output.json" For Output As #1 Print #1, json Close #1 MsgBox "JSON export complete." End Sub
以上代碼是一個VBA宏,可以將Excel中指定的單元格范圍導出為一個包含在"data"數組中的JSON字符串,然后將其輸出到一個名為“output.json”的文件中。使用這個宏可以輕松處理大量數據,將其轉換為可與其他應用程序共享的格式。
上一篇c 對象類型轉換json
下一篇c 對象裝json