0

I am creating an Excel to JSON parser and everything works good, but I have confusion. Currently my application displays the result in the console, and I want to modify it and write the result to the disc on my local machine as a file named output.json.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;


namespace ExcelToJSON
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"C:\Users\demir.mahmutovic\Desktop\aaa.xlsx";
            string sheetName = "Sheet1";

            var connstr = string.Format(@"
            Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source={0};
            Extended Properties=""Excel 12.0 Xml;HDR=YES""", path);

            using (var conn = new OleDbConnection(connstr))
            {
                conn.Open();

                var cmd = conn.CreateCommand();
                cmd.CommandText = String.Format(@"SELECT * FROM [{0}$]",
                sheetName
                );

                using (var rdr = cmd.ExecuteReader())
                {
                    //LINQ query - when executed will create anonymous objects for each row
                    var query =
                        (from DbDataRecord row in rdr
                         select row).Select(x =>
                         {

                             //dynamic item = new ExpandoObject();
                             Dictionary<string, object> item = new Dictionary<string, object>();
                             item.Add(rdr.GetName(0), x[0]);
                             item.Add(rdr.GetName(1), x[1]);
                             item.Add(rdr.GetName(2), x[2]);
                             item.Add(rdr.GetName(3), x[3]);
                             item.Add(rdr.GetName(4), x[4]);
                             item.Add(rdr.GetName(5), x[5]);
                             item.Add(rdr.GetName(6), x[6]);
                             item.Add(rdr.GetName(7), x[7]);
                             item.Add(rdr.GetName(8), x[8]);
                             item.Add(rdr.GetName(9), x[9]);
                             item.Add(rdr.GetName(10), x[10]);
                             item.Add(rdr.GetName(11), x[11]);
                             item.Add(rdr.GetName(12), x[12]);
                             item.Add(rdr.GetName(13), x[13]);
                             item.Add(rdr.GetName(14), x[14]);
                             item.Add(rdr.GetName(15), x[15]);
                             item.Add(rdr.GetName(16), x[16]);
                             item.Add(rdr.GetName(17), x[17]);
                             item.Add(rdr.GetName(18), x[18]);
                             return item;

                         });

                    //Generates JSON from the LINQ query
                    var json = JsonConvert.SerializeObject(query);
                    var result = json;
                    Console.WriteLine(result);
                    Console.ReadLine();
                    //return json;
                }
            }
        }
    }
}

Can anyone guide me and tell me if this is possible or not and what is the best way to resolve this problem. I would be very thankful,

RedFox
  • 457
  • 3
  • 15
  • The simplest way would be something with [`File.WriteAllText`](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=netframework-4.8). I'm a bit surprised that you managed to query an excel file and serialize to json but don't know about `System.IO`... – germi Oct 29 '19 at 09:33
  • Hmm, I know about `System.IO` –  Oct 29 '19 at 09:39
  • Sorry, I did not want to attack you - I'm genuinely surprised because that seems much more basic than what you're already doing in your program. – germi Oct 29 '19 at 09:41
  • Sorry, I just get into programming and everything for me is new :/ Maybe for your is easy and basic but for me it's not –  Oct 29 '19 at 09:44
  • Possible duplicate of [How to write a JSON file in C#?](https://stackoverflow.com/questions/16921652/how-to-write-a-json-file-in-c) – Drag and Drop Oct 29 '19 at 10:04

2 Answers2

0

you can use StreamWriter , i'm just adding the date to file name,

using (StreamWriter file = new StreamWriter($"ToolBox_Log_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm") + ".Json", true))
        {

            file.WriteLine(result);
        }
Muhammed Saad
  • 131
  • 1
  • 5
  • Not sure did you understand what I need ? I have alredy have result store in console, but I want this JSON result store in output.json file –  Oct 29 '19 at 09:56
  • 1
    yes, you see that stream writer i used to write to disk Just Like File – Muhammed Saad Oct 29 '19 at 09:59
0

As already mentioned in the comments, use File.WriteAllText

string path = @"C:\Users\demir.mahmutovic\Desktop\output.json";
string json = JsonConvert.SerializeObject(query);
File.WriteAllText(path, json);
travelhawk
  • 134
  • 6