直到现在,我一直在使用github的vba-json解析器。由于大型JSON文件的出现,我意识到,vba-json解析器需要很长时间,因为它逐字符地读取json字符串并将它们转换为vba集合。
现在,我在互联网上研究了一下,发现了JSON.NET,它似乎是一个非常快速的json解析器。要在Excel-VBA中使用它,我安装了Visual Studio并创建了一个VSTO-Project。
我成功将json-string从vba-code传递给Visual Studio。然后将Json-String反序列化为C#-Object。现在,当我将C#-Object返回给Excel VBA时,我收到一条错误消息,指出
错误424需要对象
我希望在Excel-VBA中使用我的JSON文件:
[
{
"rootarray": [
{
"id": "11234",
"price": 2.99
},
{
"id": "5324",
"price": 32.99
},
{
"id": "23",
"price": 5.99
}
],
"name" : "Shop Nr. 22"
}
]
我的VBA代码:
Sub NetTest(strJson As String)
Dim addIn As COMAddIn
Dim objekt As Object
Set addIn = Application.COMAddIns("ExcelAddIn2")
Set objekt = addIn.Object
Dim solution As Object
Set solution = objekt.CSharpToVBA(strJson)
End Sub
我的C#-Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using Object = System.Collections.ObjectModel;
namespace ExcelAddIn2
{
[System.Runtime.InteropServices.ComVisible(true)]
public class CodeKlasse
{
public partial class Welcome
{
[JsonProperty("rootarray")]
public Rootarray[] Rootarray { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public partial class Rootarray
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("price")]
public double Price { get; set; }
}
public partial class Welcome
{
public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
public Welcome[] CSharpToVBA(string jsontext)
{
try
{
var jPerson = JsonConvert.DeserializeObject<Welcome[]>(jsontext, Converter.Settings);
return jPerson;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
var failure = JsonConvert.DeserializeObject<Welcome[]>(string.Empty);
return failure;
}
}
}
public partial class ThisAddIn
{
private CodeKlasse codeKlasse;
protected override object RequestComAddInAutomationService()
{
codeKlasse = new CodeKlasse();
return codeKlasse;
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region Von VSTO generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
显然,Excel无法使用C#对象。我该怎么做呢。我希望将C#Object转换为vba对象,这样当我在Excel-VBA中编写时:
?solution(0)("rootarray")(2)("price")
我得到了值:
5.99
我希望有人可以帮助我吗?我会很感激。 谢谢。
问候。