Exemplo n.º 1
0
    /// <summary>
    /// 存储文件
    /// </summary>
    private void Save()
    {
        if (_table == null)
        {
            Debug.LogError("The table is null.");
            return;
        }
        string tableContent = _table.GetContent();

        if (!Directory.Exists(_savePath))
        {
            Debug.Log("未找到路径, 已自动创建");
            Directory.CreateDirectory(_savePath);
        }
        string fullFileName = _savePath + _fileName + EXTENSION;

        StreamWriter sw;

        sw = File.CreateText(fullFileName);
        //sw = new StreamWriter(fullFileName, false, Encoding.UTF8);
        sw.Write(tableContent);
        sw.Close();
        sw.Dispose();

        _table        = null;
        _display.text = "Save.";
    }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            CSV_Parser parser = new CSV_Parser("table.csv", ';', "TEST");

            Console.WriteLine(parser.TableName + Environment.NewLine + parser.Table);
            Console.WriteLine();

            var res  = parser.Table.Find(new string[] { "Address", "Age", "Name" }, new object[] { (object)"Banghok", (object)31, (object)"Bil" }); // res - null exeption on WriteToFile
            var res2 = parser.Table.Find("Age", (object)"31");
            var res3 = parser.Table.Find("Age", (object)31);
            var res4 = parser.Table.Find("Address", (object)"LA");

            //CSV_Table.WriteToFile(res);
            Console.WriteLine(String.Format("Find result :\n{0}", res));

            var             res11  = parser.Table.Find("Age", (object)31);
            var             res21  = res11.Find("Address", (object)"LA");
            var             rowVal = parser.Table.GetColumnsFromRow(18);
            List <object[]> row    = new List <object[]>();

            for (int i = 0; i < 4; i++)
            {
                row.Add(new object[] { rowVal[i] });
            }
            var head  = parser.Table.ColNames;
            var types = CSV_Table.GetColumnTypesCsvFormat(parser.Table);
            var res31 = new CSV_Table(head, row, types);

            CSV_Table.WriteToFile(res31, "OneRowTable");

            Console.WriteLine(res31);
            Console.ReadKey();
        }
Exemplo n.º 3
0
        public void SearchTest()
        {
            var             test   = new CSV_Parser("table.csv", ';');
            var             res1   = test.Table.Find("Age", (object)31);
            var             res2   = test.Table.Find("Address", (object)"LA");
            var             rowVal = test.Table.GetColumnsFromRow(18);
            List <object[]> row    = new List <object[]>();

            row.Add(rowVal);
            var head = test.Table.ColNames;
            var res3 = new CSV_Table(head, row, CSV_Table.GetColumnTypesCsvFormat(test.Table));

            Assert.AreEqual(res2, res3);
        }
Exemplo n.º 4
0
    /// <summary>
    /// 通过数据表名字和数据表文本内容构造一个数据表对象
    /// </summary>
    /// <param name="tableName"> 数据表名字 </param>
    /// <param name="tableContent"> 数据表文本内容 </param>
    /// <returns> 数据表对象 </returns>
    public static CSV_Table CreateTable(string tableName, string tableContent)
    {
        string content = tableContent.Replace("\r", "");

        string[] lines = content.Split('\n');
        if (lines.Length < 2)
        {
            Debug.LogError("The csv file is not csv table format.");
            return(null);
        }

        string keyLine = lines[0];

        string[] keys = keyLine.Split(',');

        CSV_Table table = new CSV_Table(tableName, keys);

        Debug.Log(lines.Length);

        //从第二行开始读
        for (int i = 1; i < lines.Length; i++)
        {
            string[] values = lines[i].Split(',');
            string   major  = values[0].Trim();
            Dictionary <string, string> tempAttributeDic = new Dictionary <string, string>();

            for (int j = 1; j < values.Length; j++)
            {
                string key   = keys[j].Trim();
                string value = values[j].Trim();
                //需要转为utf-8,不然乱码重复值
                tempAttributeDic.Add(key, value);
            }
            CSVDataObject dataObj = new CSVDataObject(major, tempAttributeDic, keys);
            table[dataObj.ID] = dataObj;
        }

        return(table);
    }
Exemplo n.º 5
0
    /// <summary>
    /// 加载文件
    /// </summary>
    private void Load()
    {
        if (!Directory.Exists(_loadPath))
        {
            Debug.LogError("The file not be found in this path. path:" + _loadPath);
            return;
        }

        string       fullFileName = _loadPath + _fileName + EXTENSION;
        StreamReader sr;

        sr = File.OpenText(fullFileName);
        string content = sr.ReadToEnd();

        sr.Close();
        sr.Dispose();

        _table = CSV_Table.CreateTable(_fileName, content);

        // 添加测试
        Test();
    }