Esempio n. 1
0
        public static JObject LoadJsonToJObject(string filename)
        {
            JObject jsonObj = null;

            if (!String.IsNullOrWhiteSpace(filename) && File.Exists(filename))
            {
                try
                {
                    using (StreamReader fileStream = File.OpenText(filename))
                        using (JsonTextReader reader = new JsonTextReader(fileStream))
                        {
                            jsonObj = (JObject)JToken.ReadFrom(reader);
                        }
                }
                catch (Exception ex)
                {
                    throw JsonExcelException.LoadFileException(ex);
                }
            }
            else
            {
                throw JsonExcelException.LoadFileNoFileFoundException();
            }

            if (jsonObj == null)
            {
                throw JsonExcelException.ParseFileException();
            }
            return(jsonObj);
        }
        public static Branch ParseSheetToBranch(Excel.Worksheet ws)
        {
            Branch root = new Branch("");

            for (int i = 1; i <= 5000; i++)
            {
                Microsoft.Office.Interop.Excel.Range line = ws.Cells.Range[ws.Cells[i, 1], ws.Cells[i, 50]];
                string[] strArrayFromExcelRow             = line.Cells.Cast <Excel.Range>().Select(x => (string)x.Text).Where(x => !(String.IsNullOrWhiteSpace(x))).ToArray <string>();
                if (!strArrayFromExcelRow.Any())
                {
                    break;
                }
                try
                {
                    Branch br = root.AddNewBranch(strArrayFromExcelRow);
                }
                catch (Exception ex)
                {
                    throw JsonExcelException.ParseBranchException($"Error: Line {i} could not parsed", ex);
                }
            }
            return(root);
        }