【Excel数据转换成知识图谱Json】

using Excel;
using ForceDirectedDiagram.Scripts.ForceDirectedDiagram;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEngine;

public class MyExcel : MonoBehaviour
{
    private ForceDiagramSourceDto dto;

    private Dictionary<string, int> groupDict;

    private Dictionary<string, string> idDict;
    void Start()
    {
        groupDict = new Dictionary<string, int>();
        idDict = new Dictionary<string, string>();
        dto = new ForceDiagramSourceDto();
        ReadExcel("/Test.xlsx");
    }

    public void ReadExcel(string xmlName)
    {
        FileStream stream = File.Open(Application.dataPath + xmlName, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本

        DataSet result = excelReader.AsDataSet();

        if (stream != null)
        {
            stream.Close();
        }

        int[] counts = GetCount(result.Tables[0]);
        int rows = counts[0];
        int columns = counts[1];
        int groupId = 0;
        for (int i = 2; i < rows; i++)
        {
            NodeDto node = new NodeDto();
            node.id = result.Tables[0].Rows[i][0].ToString();
            node.label = result.Tables[0].Rows[i][1].ToString();
            node.description = result.Tables[0].Rows[i][2].ToString();
            node.groupname = result.Tables[0].Rows[i][3].ToString();
            idDict[node.label] = node.id;
            if (groupDict.ContainsKey(node.groupname))
            {
                int id = 0;
                groupDict.TryGetValue(node.groupname, out id);
                node.group = id;
                node.subgroup = id;
            }
            else
            {
                groupDict[node.groupname] = groupId;
                node.group = groupId;
                node.subgroup = groupId;
                groupId++;
            }
            node.label = result.Tables[0].Rows[i][1].ToString();
            dto.nodes.Add(node);
        }
        for (int i = 2; i < rows; i++)
        {
            if (result.Tables[0].Rows[i][4]!=null)
            {
                LinkDto link = new LinkDto();
                link.source = result.Tables[0].Rows[i][0].ToString();
                string target = result.Tables[0].Rows[i][4].ToString();
                if (idDict.ContainsKey(target))
                {
                    link.target = idDict[target];
                    link.length = 1;
                    dto.links.Add(link);
                }
            }
        }
        string json = JsonUtility.ToJson(dto);
       // Debug.Log(json);
        var path = Application.streamingAssetsPath + "/A.json";
        File.WriteAllText(path, json);
    }

    private int[] GetCount(DataTable dt)
    {
        int i = dt.Rows.Count;
        for (int m = 0; m < dt.Rows.Count; m++)
        {
            if (string.IsNullOrEmpty(dt.Rows[m][0].ToString()))
            {
                i = m;
                break;
            }
        }

        int j = dt.Columns.Count;
        for (int n = 0; n < dt.Columns.Count; n++)
        {
            if (string.IsNullOrEmpty(dt.Rows[0][n].ToString()))
            {
                j = n;
                break;
            }
        }
        return new int[] { i, j };
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Excel是一种通用的电子表格软件,用于管理数据和进行数据分析。虽然Excel主要是用于处理和计算数值数据,但它也可以用作知识图谱工具,帮助我们将复杂的信息关系整理和可视化。 首先,Excel提供了一种直观且易于使用的界面,使得构建知识图谱变得简单。我们可以利用Excel的表格功能将相关的数据整理成一组层次清晰的信息表,每一列代表一个属性或关系。通过使用Excel的筛选、排序和条件格式化等功能,我们可以根据自己的需要对数据进行组织和呈现,从而形成一个结构化的知识图谱。 其次,Excel还提供了一些图表和图形功能,用于可视化和展示数据。我们可以利用Excel的图表工具将数据以不同的图表类型展示,例如线图、饼图、柱图等,以便更直观地理解和分析数据之间的关系。通过调整图表的样式和布局,我们可以提高知识图谱的可读性和易懂性。 此外,Excel具有强大的计算和数据处理功能,可实现自动化的数据分析和关联。我们可以使用Excel的公式和函数来计算复杂的关系和指标,例如计算某一属性的权重、计算不同属性之间的相关性等。这些功能可以使我们更深入地理解和分析数据之间的关系,从而形成更精确和有用的知识图谱。 总之,尽管Excel不是专门设计用于构建知识图谱的工具,但它提供了强大的数据管理、可视化和分析功能,可以用于构建简单的知识图谱。通过充分利用Excel的各种功能,我们可以更好地掌握和展示复杂的信息关系,提高对数据的理解和分析能力。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值