Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Excel数据转换为特定的JSON格式_C#_Json_Excel - Fatal编程技术网

C# 将Excel数据转换为特定的JSON格式

C# 将Excel数据转换为特定的JSON格式,c#,json,excel,C#,Json,Excel,我得到一个Excel文件(.xlsx),如下所示: { "value":[ { "Prename":"Nik", "Age":"17", "Country":"Switzerland" }, { "Prename":"

我得到一个Excel文件(.xlsx),如下所示:

{
   "value":[
      {
         "Prename":"Nik",
         "Age":"17",
         "Country":"Switzerland"
      },
      {
         "Prename":"Tom",
         "Age":"19",
         "Country":"Russia"
      },
      {
         "Prename":"Isak",
         "Age":"18",
         "Country":"Switzerland"
      }
   ]
}
{
   "value":[
      {
         "Prename":"Nik",
         "Age":"17",
         "Country":"Switzerland",
         "Car":"Audi"
      },
      {
         "Prename":"Tom",
         "Age":"19",
         "Country":"Russia",
         "Car":"Mercedes"
      },
      {
         "Prename":"Isak",
         "Age":"18",
         "Country":"Switzerland",
         "Car":"Tesla"
      },
      {
         "Prename":"Jeff",
         "Age":"27",
         "Country":"Belarus",
         "Car":"BMW"
      }
   ]
}

行和列的数量可能会有所不同。它也可以如下所示,例如:

对于第一张图像中的excel工作表,JSON应该如下所示:

{
   "value":[
      {
         "Prename":"Nik",
         "Age":"17",
         "Country":"Switzerland"
      },
      {
         "Prename":"Tom",
         "Age":"19",
         "Country":"Russia"
      },
      {
         "Prename":"Isak",
         "Age":"18",
         "Country":"Switzerland"
      }
   ]
}
{
   "value":[
      {
         "Prename":"Nik",
         "Age":"17",
         "Country":"Switzerland",
         "Car":"Audi"
      },
      {
         "Prename":"Tom",
         "Age":"19",
         "Country":"Russia",
         "Car":"Mercedes"
      },
      {
         "Prename":"Isak",
         "Age":"18",
         "Country":"Switzerland",
         "Car":"Tesla"
      },
      {
         "Prename":"Jeff",
         "Age":"27",
         "Country":"Belarus",
         "Car":"BMW"
      }
   ]
}
对于第二张excel工作表,JSON应该如下所示:

{
   "value":[
      {
         "Prename":"Nik",
         "Age":"17",
         "Country":"Switzerland"
      },
      {
         "Prename":"Tom",
         "Age":"19",
         "Country":"Russia"
      },
      {
         "Prename":"Isak",
         "Age":"18",
         "Country":"Switzerland"
      }
   ]
}
{
   "value":[
      {
         "Prename":"Nik",
         "Age":"17",
         "Country":"Switzerland",
         "Car":"Audi"
      },
      {
         "Prename":"Tom",
         "Age":"19",
         "Country":"Russia",
         "Car":"Mercedes"
      },
      {
         "Prename":"Isak",
         "Age":"18",
         "Country":"Switzerland",
         "Car":"Tesla"
      },
      {
         "Prename":"Jeff",
         "Age":"27",
         "Country":"Belarus",
         "Car":"BMW"
      }
   ]
}
我正在使用这个框架

我使用框架中的这种方法从表格中获取数据:

读一行

IEnumerable<object> row1 = sheetReader.Row(1);
IEnumerable行1=sheetReader.Row(1);
通过此操作,我获得excel工作表的第一行:

我当前的整个代码看起来像这样,正如您所看到的,它非常基本

var excelReader = new ExcelReader("path\to\excel");
var sheetReader = excelReader[0];
IEnumerable<object> row1 = sheetReader.Row(1);
var excelReader=new excelReader(“path\to\excel”);
var sheetReader=excelReader[0];
IEnumerable row1=sheetReader.Row(1);
我的主要问题是将数据组合成JSON数组,然后将它们组合成一个JSON对象


我如何使用读取excel所需的给定行函数构建JSON?

@jdweng我按照建议重写了这个问题,因为前面的问题不够具体。在这个问题上,我尽量具体。但我没有删除任何注释。我将使用oledb数据适配器填充数据表(请参阅)。datatable将提供所有列,而无需手动读取每一行。使用如下库方法将表转换为json:请参阅我的答案。