【问题标题】:Converting excel data to json data in a specific format using C#使用C#将excel数据转换为特定格式的json数据
【发布时间】:2021-10-17 11:05:33
【问题描述】:

我能够使用 Npoi 读取我的 excel 文件中包含的数据。但是我想要的是将它们转换为您在第一张图片中看到的格式的 json,我该怎么做?

代码: 字符串文件路径 = @"D:\Feedback4eDosyaları\DiscoveryDilPaketi.xlsx"; 尝试 { IWorkbook 工作簿 = null; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (filePath.IndexOf(".xlsx") > 0) 工作簿 = 新 XSSFWorkbook(fs); 否则 if (filePath.IndexOf(".xls") > 0) 工作簿 = 新 HSSFWorkbook(fs); Console.WriteLine(filePath.IndexOf(".xlsx")); Console.WriteLine(filePath.IndexOf(".xls")); ISheet sheet = workbook.GetSheetAt(0); int rowCount = sheet.LastRowNum;

            if (sheet != null)
            {
                int rowCount = sheet.LastRowNum;
                for (int i = 1; i <= rowCount; i++)
                {
                    IRow curROw = sheet.GetRow(i);
                    var cellValue0 = curROw.GetCell(0).StringCellValue.Trim();
                    var cellValue1 = curROw.GetCell(1).StringCellValue.Trim();
                    var cellValue2 = curROw.GetCell(2).StringCellValue.Trim();
                    var cellValue3 = curROw.GetCell(3).StringCellValue.Trim();
                    var cellValue4 = curROw.GetCell(4).StringCellValue.Trim();
                    var cellValue5 = curROw.GetCell(5).StringCellValue.Trim();
                    var cellValue6 = curROw.GetCell(6).StringCellValue.Trim();
                    var cellValue7 = curROw.GetCell(7).StringCellValue.Trim();

                    JsonClass jsonClas = new JsonClass()
                    {
                        label = cellValue0,
                        TrTR = cellValue1,
                        EnUS = cellValue2,
                        FrFR = cellValue3,

                    };

                    string strResulJson = JsonConvert.SerializeObject(jsonClas);
                    File.WriteAllText(@"jsonClass.json", strResulJson);
                    Console.WriteLine(strResulJson);
                }
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

    }

【问题讨论】:

  • 请将代码发布为文本而不是图像
  • 获取你想要的 json 并将其粘贴到QuickType.io - 它会生成你需要的类并告诉你如何序列化它。然后您所要做的就是创建这些类的树并将其序列化。您似乎忘记了创建任何将服务于 aboutToCreateCycleWarning 集的内容
  • 您可以使用 LinqToExcel 或仅使用 OleDb 来读取 excel 数据,然后使用 Newtonsoft 等库之一转换为 Json。请发布您的代码,而不是对任何人都没有帮助的图片。
  • 我添加了代码:)

标签: c# json excel npoi


【解决方案1】:

试试这个..

类 JsonClass{

string TrTR;
string EnUS;
string FrFR;
string BeBG;
string RoRO;
string RuRU;

}

int rowCount = sheet.LastRowNum;

List list_data = new List();

for (int i = 13 i <= rowCount; i++){

    TRow curROw = sheet.GetRow(i);

    var cellValue@ = curROw.GetCel1(0).StringCellValue. Trim();
    var cellValue1 = curROw.GetCell(1).StringCellValue.Trim();
    var cellvalue2 = curROw.GetCell1(2).StringCellValue.Trim();
    var cellValue4 = curROw.GetCell(4).StringCellValue.Trim() ;
    var cellValueS = curROw.GetCel1(5).StringCellValue.Trim();
    var cellValue7 = curROw.GetCell(7).StringCellValue.Trim();

        JsonClass jsonClas = new JsonClass(){

        TrTR = cellValuel,
        EnUS = cellValue2,
        FrFR = cellValue3,
        BeBG=cellValue5,
        RoRO=cellValue6,
        RuRU=cellValue7,
        }
    list_data.Add(jsonClas);
}

字符串 strResultJson = JsonConvert.SerializeObject(list_data);

File.WriteAllText(@"jsonClass.json",strResulJson) ;

控制台.WriteLine(strResulJson);

【讨论】:

  • 最好在另一个c#类文件中声明JsonClass。然后就可以在解码的时候使用了。
  • 我按照你说的做了,可惜没有得到我想要的结果。
猜你喜欢
  • 2019-12-04
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2019-05-03
  • 2020-12-09
  • 2016-08-05
  • 2022-08-16
相关资源
最近更新 更多