在Excel上将Excel导入Json格式

我需要问与父母关系导入Excel表单问题。

下面我有一个示例表

在这里输入图像说明

输出我想要那样

[{"Code":"888800","Title":"Category","Value":"123","Children":[]}, {"Code":"100000","Title":"Category1","Value":"123","Children": [ {"Code":"120000","Title":"SubCategory","Value":"123","Children":[]}, {"Code":"352000","Title":"SubCategory2","Value":"123","Children":[]}, {"Code":"200000","Title":"SubCategory3","Value":"123","Children":[]}, ]}, {"Code":"212101","Title":"Category3","Value":"123","Children": [ {"Code":"213100","Title":"SubCategory4","Value":"123","Children":[]}, {"Code":"213200","Title":"SubCategory4","Value":"123","Children":[]} ]} ] 

下面我有我的代码,但我不知道我的错误点在哪里。 当我创buildJson序列化值和子部分不正确

  int sheetname = 0; class Position { public Position() { this.Children = new List<Position>(); } public string Code { get; set; } public string Title { get; set; } public string Value { get; set; } public List<Position> Children { get; set; } } Position Parsing(string value) { string pattern = "\\((?<Code>[\\w]+)\\)(?<Title>.+)"; var regex = new Regex(pattern); var match = regex.Match(value); if (match.Success) { return new Position() { Code = match.Groups["Code"].Value, Title = match.Groups["Title"].Value, Value = match.Groups["Value"].Value }; } else { return null; } } protected void Button1_Click(object sender, EventArgs e) { string sSourceConstr = ""; string fileName = Path.GetFileName(fuExcel.PostedFile.FileName); if (fuExcel.FileName == string.Empty) { JSBuilder.AlertMessage(this, "ERROR"); } string fileExtension = Path.GetExtension(fuExcel.PostedFile.FileName); string fileLocation = Server.MapPath("~/" + fileName); fuExcel.SaveAs(fileLocation); if (fuExcel.HasFile) { if (fileExtension == ".xls") { sSourceConstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; } else if (fileExtension == ".xlsx") { sSourceConstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; } try { IWorkbook workbook = default(IWorkbook); using (FileStream file = new FileStream(fileLocation, FileMode.Open)) { workbook = WorkbookFactory.Create(file); List<string> items = new List<string>(); Position posnTop = null; List<Position> posnList = new List<Position>(); ISheet sheet = workbook.GetSheetAt(0); for (int row = 1; row <= sheet.LastRowNum; row++) { if (sheet.GetRow(row) != null) { DataTable dt = new DataTable(); IRow excelRow = default(IRow); excelRow = sheet.GetRow(1); for (int Cel = 1; Cel <= excelRow.LastCellNum - 1; Cel++) { string industry = sheet.GetRow(1).GetCell(Cel).ToString(); string IndustryCode = sheet.GetRow(2).GetCell(Cel).ToString(); for (int k = 4; k <= sheet.LastRowNum; k++) { items.AddRange(sheet.GetRow(k).GetCell(Cel - 1).ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)); foreach (var item in items) { if (!string.IsNullOrEmpty(item) && item.Length > 1) { if ("\t".Equals(item.Substring(0, 1))) { //child var posn = this.Parsing(item.Trim()); if (posn != null) { posnTop.Children.Add(posn); } } else { var posn = this.Parsing(item); if (posn != null) { posnTop = posn; posnList.Add(posn); } } if (sheet.GetRow(k).GetCell(Cel) != null) { string value = sheet.GetRow(k).GetCell(Cel).ToString(); } } } } } TextBox txtMsg = new TextBox(); this.txtMsg.Text = Newtonsoft.Json.JsonConvert.SerializeObject(posnList); #region information Import to SQLSERVER //////////////////// //import sql part// //////////////////// #endregion } else { JSBuilder.AlertMessage(this, true, "import succesfull"); } } } } catch (Exception ex) { Response.Write(ex); } } }