【发布时间】: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。请发布您的代码,而不是对任何人都没有帮助的图片。
-
我添加了代码:)