简体   繁体   中英

Converting excel data to json data in a specific format using C#

I was able to read the data contained in my excel file using Npoi. But what I want is to convert them to json in the format you see in the 1st picture, how can I do this?

Code: String filePath = @"D:\\Feedback4eDosyaları\\DiscoveryDilPaketi.xlsx"; try { IWorkbook workbook = null; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (filePath.IndexOf(".xlsx") > 0) workbook = new XSSFWorkbook(fs); else 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);
        }

    }

try this..

class 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) ;

Console .WriteLine(strResulJson);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM