有个金融领域的需求,有一段excel的vba代码可以自动生成金融产品数据,要把这些数据在网页上(其实是微信公众号)展示,于是就需要把生成的数据发送到服务器并保存给网页用。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
vba里面可以发送Http请求,笔者先把需要的数据拼成了1个Json字符串。真的是拼的,最好的方式肯定是序列化类实例,不过笔者以为vba不支持类,可能记糊涂了。不过后来发现vba居然支持类,但没有现成的json序列化方法,那么跟自己拼也没啥区别,就这么用了。ps,写vba真是浪费时间,比C#效率低多了,也没用vs这种强大的IDE。
Sub MakeJson()
Dim result, duedates, lists As String
Dim currentdate As String
Dim target As Range
Set target = Sheets("publish").Range("A1")
result = "["
Do
'开始处理标的
duedates = ""
lists = ""
If Len(result) <> 1 Then
result = result + ","
End If
result = result + "{"
result = result + """exchange_code"":""" + target.Offset(1, 7).Value + ""","
result = result + """product_code"":""" + target.Offset(1, 8).Value + ""","
result = result + """link_contract"":""" + target.Offset(1, 0).Value + ""","
Do
Set target = target.Offset(1, 0)
If currentdate <> target.Offset(0, 1).Value Then
If duedates <> "" Then
duedates = duedates + ","
End If
currentdate = target.Offset(0, 1).Value