简体   繁体   English

使用C#将excel数据转换为特定格式的json数据

[英]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.我能够使用 Npoi 读取我的 excel 文件中包含的数据。 But what I want is to convert them to json in the format you see in the 1st picture, how can I do this?但是我想要的是将它们以您在第一张图片中看到的格式转换为 json,我该怎么做?

Code: String filePath = @"D:\\Feedback4eDosyaları\\DiscoveryDilPaketi.xlsx";代码:String filePath = @"D:\\Feedback4eDosyaları\\DiscoveryDilPaketi.xlsx"; try { IWorkbook workbook = null;尝试 { IWorkbook 工作簿 = null; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (filePath.IndexOf(".xlsx") > 0) workbook = new XSSFWorkbook(fs); if (filePath.IndexOf(".xlsx") > 0) workbook = new XSSFWorkbook(fs); else if (filePath.IndexOf(".xls") > 0) workbook = new HSSFWorkbook(fs);否则 if (filePath.IndexOf(".xls") > 0) workbook = new HSSFWorkbook(fs); Console.WriteLine(filePath.IndexOf(".xlsx")); Console.WriteLine(filePath.IndexOf(".xlsx")); Console.WriteLine(filePath.IndexOf(".xls")); Console.WriteLine(filePath.IndexOf(".xls")); ISheet sheet = workbook.GetSheetAt(0); ISheet sheet = workbook.GetSheetAt(0); int rowCount = sheet.LastRowNum; 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{类 JsonClass{

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

} }

int rowCount = sheet.LastRowNum; int rowCount = sheet.LastRowNum;

List list_data = new List(); 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); string strResultJson = JsonConvert.SerializeObject(list_data);

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

Console .WriteLine(strResulJson);控制台 .WriteLine(strResulJson);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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