/// <summary>
        /// Load Json-File and show there Data in current Excel-Sheet.
        /// </summary>
        /// <param name="control"></param>
        public void OnButtonPressed_LoadJsonFile(IRibbonControl control)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = this._lastPath,
                Filter           = "Json files (*.json)|*.json|txt files (*.txt)|*.txt|All files (*.*)|*.*",
                FilterIndex      = 1,
                RestoreDirectory = true,
                Multiselect      = false,
                Title            = "Select a file to import"
            };

            this._app             = (Excel.Application)ExcelDnaUtil.Application;
            this._activeWorksheet = (Excel.Worksheet) this._app.ActiveWorkbook.ActiveSheet;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                JObject jsonObj = null;
                try
                {
                    jsonObj        = JsonExcelLib.LoadJsonToJObject(openFileDialog1.FileName);
                    this._lastPath = Path.GetDirectoryName(openFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this._gapToShowValuesInSameColumn = jsonObj.FindMaxDepth();

                int rowNumber = 1;
                foreach (KeyValuePair <string, JToken> jsonToken in jsonObj)
                {
                    rowNumber = ExcelJsonShow.ShowTokenInSheet(jsonToken.Value, rowNumber, 2, new List <string> {
                        jsonToken.Key
                    }, this._activeWorksheet, this._gapToShowValuesInSameColumn);
                }
            }
        }
Exemple #2
0
        public void FindDepthInJsonTest()
        {
            string jsonTest = @"
{
	'l1': {
		'l2' : {
			'l3': {
				'l4' : 'Hallo'
			}
		},
		'l11' : 'Nix',
		'l12' : 'Nix2'
	},
	'l21' : {
		'l211' : 'huhu'
	}
}
";

            JObject o3    = JObject.Parse(jsonTest);
            int     depth = JsonExcelLib.FindMaxDepth(o3);

            Assert.AreEqual(depth, 5, "Biggest depth of this Json should be 5");
        }