excel通过宏来导出json数据

本文介绍了一种使用VBA宏将Excel数据转换并导出为JSON格式的方法。通过简单的宏按钮实现,适用于批量导出数据的需求。文章提供了完整的VBA代码示例,包括设置文件路径、定义数据范围及格式化输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sub 导出JSON()

    Dim s As String
    Dim FullName As String, rng As Range
    Dim row As Integer, column As Integer, maxrow As Integer
    Dim strContent As String
    Application.ScreenUpdating = False
    
    FullName = (ThisWorkbook.Path & "/" & Cells(1, 2) & ".txt") ' Cells(1, 2) 表示第1行,第二列的内容

    '配置区域
    proline = 5 '程序名定义行
    begindatalen = 6 '数据起始行
    row = ActiveSheet.UsedRange.Rows.Count '最大行
    column = ActiveSheet.UsedRange.Columns.Count '最大列

    strContent = "{" & """" & Cells(1, 2) & """" & ":["  'json组成部分
    For m = begindatalen To row
        If Cells(m, 1) <> "" Then
            
            strContent = strContent & "{"
            
            For n = 1 To column
                If n = column Then
                    strContent = strContent & """" & Cells(proline, n) & """" & ":" & """" & Cells(m, n) & """"
                Else
                    strContent = strContent & """" & Cells(proline, n) & """" & ":" & """" & Cells(m, n) & """" & ","
                End If
            Next n
            
            If m = row Then
                
                strContent = strContent & "}"
                
            Else
                
                strContent = strContent & "},"
                
            End If
            

        End If
    Next m
    
    strContent = strContent & "]}"


    WriteUTF8File strContent, FullName, False
    Application.ScreenUpdating = True
    MsgBox "数据已导出"

End Sub


PS:在excel中插入一个宏安钮,然后编辑该宏代码即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值