Excel将单元格中的json本文格式化

打开Excel文件并按下ALT + F11打开Visual Basic for Applications(VBA)编辑器。

输入下面的代码

vbnet 复制代码
Sub FormatJSONCells()
    Dim cell As Range
    Dim jsonString As String
    Dim json As Object
    Dim formattedJSON As String

    ' 循环遍历选定的单元格范围
    For Each cell In Selection
        jsonString = cell.Value
        ' 检查单元格中的内容是否为JSON数据
        If IsValidJSON(jsonString) Then
            ' 使用VBA内置JSON解析功能将JSON数据解析为对象
            Set json = JsonConverter.ParseJson(jsonString)
            ' 将JSON数据重新格式化为易于阅读的形式
            formattedJSON = JsonConverter.ConvertToJson(json, Whitespace:=2)
            ' 将格式化后的JSON数据写回到单元格中
            cell.Value = formattedJSON
        End If
    Next cell
End Sub

Function IsValidJSON(ByVal strJSON As String) As Boolean
    On Error Resume Next
    Dim json As Object
    Set json = JsonConverter.ParseJson(strJSON)
    IsValidJSON = (Err.Number = 0)
    On Error GoTo 0
End Function

进入https://github.com/VBA-tools/VBA-JSON,下载release的最新代码

VBA编辑器,"文件"->"导入", 导入文件 JsonConverter.bas

点击VBA编辑器的菜单栏上的"工具"。

选择"引用",然后在弹出的对话框中找到并勾选"Microsoft Scripting Runtime",以及"Microsoft XML 6.0"。

点击"确定"保存更改。

保存并关闭VBA编辑器。

相关推荐
城数派11 小时前
2015-2025年我国区县逐年二手房房价数据(Excel/Shp格式)
excel
还是大剑师兰特13 小时前
Vue3 前端专属配置(VSCode settings.json + .prettierrc)
前端·vscode·json
用户83562907805113 小时前
Python 设置 Excel 条件格式教程
后端·python·excel
qq_2837200515 小时前
Cesium实战(三):加载天地图(影像图,注记图)避坑指南
json·gis·cesium
雷帝木木16 小时前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
长安110816 小时前
JsonCpp的编译与使用
json
凌晨一点的秃头猪18 小时前
JSON 文件基础介绍
json
lzksword19 小时前
关于EXCEL中vlookup身份证匹配失败的处理
excel
城数派19 小时前
1990-2025年我国省市县三级的逐年土地覆盖数据(9类用地/Excel/Shp格式)
excel
catoop20 小时前
构建高稳健性、可交互的复杂 Excel 报表方法论:切片、流式与动态公式
excel