美文网首页
Unity-csv(Excel)

Unity-csv(Excel)

作者: 玄策丶 | 来源:发表于2019-06-17 17:33 被阅读0次

一、解析csv文件

using UnityEngine;
using System.Collections;
using UnityEngine;
public class Date_csv : MonoBehaviour 
{
    //public TextAsset ta; //要解析的csv文件
    private string[][] _ArryDate;//用来存放数据的数组
    void Awake ()
    {
        TextAsset ta = Resources.Load(
            "MyData/MycsvData", typeof(TextAsset)) as TextAsset;

        //读取每一行的内容,放到数组中,有多少行,数组的容量就有多大
        string[] lineArray = ta.text.Split('\n');
        //创建二维数组
        _ArryDate = new string[lineArray.Length][];
        //把数据放到二维数组中
        for (int i = 0; i < lineArray.Length; i++)
        {
            _ArryDate[i] = lineArray[i].Split(',');
        }
    }//end_Start

    public float GetDateByIDAndName(int id, string name)
    {
        if (_ArryDate.Length<=0)
        {
            return float.Parse("");
        }
        //行
        int nRow = _ArryDate.Length;
        //列
        int nCol = _ArryDate[0].Length;
        for (int i = 7; i < nRow; i++)
        {
            //将int类型的ID转换成string类型
            //string strID = string.Format("{0}", id);
            string strID = id.ToString();
            //判断id
            if (_ArryDate[i][0] == strID)
            {
                print("id匹配成功");
                for (int j = 0; j < nCol; j++)
                {
                    //判断列的名字,因为列的名字就是表头的名字,所以是[0][j]
                    if (_ArryDate[0][j]==name )
                    {
                        return float.Parse(_ArryDate[i][j]);
                    }
                }
            }
        }
        return 0;
    }
}

float f = Date_csv.instance .GetDateByIDAndName(2, "attack_power");

二、生成Excel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using org.in2bits.MyXls;

public class TestInfo
{
    public string time;
    public string X;
    public string Y;
};
public class ExcelMakerManager : MonoBehaviour 
{
    public static ExcelMakerManager instance;
    public static ExcelMakerManager CreateExcelMakerManager()
    {
        if (instance == null)
        {
            instance = new ExcelMakerManager();
        }
        return instance;
    }
    //链表为 物体信息 .
    public void ExcelMaker(string name, List<TestInfo> listInfo)
    {
        XlsDocument xls = new XlsDocument();//新建一个xls文档
        xls.FileName = name;// @"D:\tests.xls";//设定文件名

        xls.SummaryInformation.Author = "L"; //填加xls文件作者信息
        xls.SummaryInformation.Subject = "test";//填加文件主题信息

        string sheetName = "Sheet0";
        Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);//填加名为"chc 实例"的sheet页
        Cells cells = sheet.Cells;//Cells实例是sheet页中单元格(cell)集合

        int rowNum = listInfo.Count;
        int rowMin = 1;
        int row = 0;

        for (int x = 0; x < rowNum + 1; x++)
        {
            if (x == 0)
            {
                //根据具体的物体信息 .需要重新写
                cells.Add(1, 1, "时间戳");
                cells.Add(1, 2, "北纬");
                cells.Add(1, 3, "东经");
            }
            else
            {
                cells.Add(rowMin + x, 1, listInfo[row].time);
                cells.Add(rowMin + x, 2, listInfo[row].X);
                cells.Add(rowMin + x, 3, listInfo[row].Y);
                row++;
            }
        }
        xls.Save();
    }
}

TestInfo test1;
public List<TestInfo> listInfos = new List<TestInfo>();

test1 = new TestInfo();
test1.time = string.Format("{0:d2}:{1:d2}:{2:d2}", shi + 8, fen, miao);
test1.X = words[2].ToString();
test1.Y = words[4].ToString();
listInfos.Add(test1);
//导出Excel
        ExcelMakerManager.CreateExcelMakerManager();
        if (!Directory.Exists(Application.dataPath + "/Excels"))
        {
            Directory.CreateDirectory(Application.dataPath + "/Excels");
        }
        path = Application.dataPath + "/Excels/Excel_"
                         + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".xls";
        ExcelMakerManager.instance.ExcelMaker(path,listInfos);

相关文章

网友评论

      本文标题:Unity-csv(Excel)

      本文链接:https://www.haomeiwen.com/subject/qfdzfctx.html