[英]Converting excel data to json data in a specific format using C#
我能夠使用 Npoi 讀取我的 excel 文件中包含的數據。 但是我想要的是將它們以您在第一張圖片中看到的格式轉換為 json,我該怎么做?
代碼:String filePath = @"D:\\Feedback4eDosyaları\\DiscoveryDilPaketi.xlsx"; 嘗試 { IWorkbook 工作簿 = null; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (filePath.IndexOf(".xlsx") > 0) workbook = new XSSFWorkbook(fs); 否則 if (filePath.IndexOf(".xls") > 0) workbook = new 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);
}
}
嘗試這個..
類 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);
}
string strResultJson = JsonConvert.SerializeObject(list_data);
File.WriteAllText(@"jsonClass.json",strResulJson) ;
控制台 .WriteLine(strResulJson);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.