C# 读取Excel转化为Json

json转XML可以在
https://1024tools.com/xmljson
找到

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Encodings.Web;
using Tool;
using System.IO;
using System.Xml;
//using Newtonsoft.Json;
using System.Text.Json;
using OfficeOpenXml;

namespace JsonPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            //Student stu = new Student() {Id=10001,Name="张三",Phonenumber="1505050505" };

            //var jsonstu = JsonSerializer.Serialize(stu, typeof(Student),
            //new JsonSerializerOptions
            //{
            //    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            //});

            //System.Text.Json.Utf8JsonReader
            //Console.WriteLine(JsonTool.ToJson(stu));
            //Console.ReadKey();
            //string FilePath = "D:\\jsontxt.json";
            string FilePath2 = @"D:\DB\ConfigFile\Variable_Modbus.json";
            FileStream fs = new FileStream(FilePath2, FileMode.Open);

            //Student aa = JsonSerializer.Deserialize(fs, new JsonSerializerOptions
            //{
            //    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            //});
            //Console.WriteLine("{0},{1},{2}", aa.Id, aa.Name, aa.Phonenumber);
            //Console.ReadKey();

            //JsonSerializer.Serialize(fs, jsonstu,
            //   new JsonSerializerOptions
            //   {
            //       Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            //   });

            JsonSerializerOptions jso = new JsonSerializerOptions
            {
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            };

            JsonDocument doc = JsonDocument.Parse(fs);
            List<Variable_Modbus> VarModbusList = new List<Variable_Modbus>();

            //var ip = doc.RootElement.GetProperty("Root").GetProperty("Variable")[0].GetProperty("VarName");

            //foreach (var item in doc.RootElement.GetProperty("Root").GetProperty("Variable").EnumerateArray())
            //{
            //    Variable_Modbus objVar = new Variable_Modbus();

            //    objVar.VarName = item.GetProperty("VarName").GetString();
            //    objVar.Address = item.GetProperty("Address").GetString();
            //    objVar.DataType = item.GetProperty("DataType").GetString();
            //    objVar.StoreArea = item.GetProperty("StoreArea").GetString();
            //    objVar.IsFiling = item.GetProperty("IsFiling").GetString();
            //    objVar.IsAlarm = item.GetProperty("IsAlarm").GetString();
            //    objVar.IsReport = item.GetProperty("IsReport").GetString();
            //    objVar.Note = item.GetProperty("Note").GetString();
            //    objVar.AbsoluteAddress = item.GetProperty("AbsoluteAddress").GetString();
            //    VarModbusList.Add(objVar);
            //}


            //var age = doc.RootElement.GetProperty("Variable");
            //for (int i = 0; i < VarModbusList.Count; i++)
            //{
            //    Console.WriteLine(VarModbusList[i].Note);
            //}
            //Console.ReadKey();
            string FilePath3 = @"D:\DB\ConfigFile\shenhua.xlsx";

            //ExcelTool exceltool = new ExcelTool();

            List<string> listVarName = new List<string>();
            List<string> listNote = new List<string>();
            //List listTest = new List();

            using (var packege = new ExcelPackage(FilePath3))
            {
                int columnIndexVarName = 3; //第N列
                int columnIndexNote = 4;
                int rowStartIndex = 2;
                int rowEndIndex = 60;
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

                ExcelWorksheet sheet = packege.Workbook.Worksheets[0];
                //var cell = sheet.Cells[50, columnIndex];
                for (int i = rowStartIndex; i < rowEndIndex; i++)
                {
                    var cell = sheet.Cells[i, columnIndexVarName];

                    listVarName.Add(cell.Value.ToString());
                }

                for (int i = rowStartIndex; i < rowEndIndex; i++)
                {
                    var cell = sheet.Cells[i, columnIndexNote];
                    listNote.Add(cell.Value.ToString());
                }
            }

            foreach (var item in doc.RootElement.GetProperty("Root").GetProperty("Variable").EnumerateArray())
            {
                Variable_Modbus objVar = new Variable_Modbus();

                objVar.VarName = item.GetProperty("VarName").GetString();
                objVar.Address = item.GetProperty("Address").GetString();
                objVar.DataType = item.GetProperty("DataType").GetString();
                objVar.StoreArea = item.GetProperty("StoreArea").GetString();
                objVar.IsFiling = item.GetProperty("IsFiling").GetString();
                objVar.IsAlarm = item.GetProperty("IsAlarm").GetString();
                objVar.IsReport = item.GetProperty("IsReport").GetString();
                objVar.Note = item.GetProperty("Note").GetString();
                objVar.AbsoluteAddress = item.GetProperty("AbsoluteAddress").GetString();
                VarModbusList.Add(objVar);
            }

            for (int i = 0; i < VarModbusList.Count; i++)
            {
                VarModbusList[i].VarName = listVarName[i];
                VarModbusList[i].Note = listNote[i];
            }
            string fp = @"D:\DB\ConfigFile\test1.json";
            File.WriteAllText(fp, Newtonsoft.Json.JsonConvert.SerializeObject(VarModbusList));
            Console.ReadKey();


            /// 转换XML为json
            //XmlDocument doc = new XmlDocument();

            //FileStream fs = new FileStream(FilePath2, FileMode.Open);
            //doc.Load(fs);
            //string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
            //Console.WriteLine("XML -> JSON: {0}", json);
            //Console.ReadKey();

        }
    }
}

你可能感兴趣的:(c#,json,linq)