【Unity开发小技巧】Unity数据导出Excel表格

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

【Unity开发小技巧】Unity数据导出Excel表格


 【Unity开发小技巧】Unity数据导出Excel表格

 一:设置数据类

public class RoadPointXlsData
{
    public string name;
    public string id;
    public string x;
    public string y;
    public string z;
};

二:创建并设置数据

  void Start()
    {
        RoadPointXlsData pointXlsData;
        List<RoadPointXlsData> listInfos = new List<RoadPointXlsData>();
        for (int i = 0; i < 12; i++)
        {
            pointXlsData = new RoadPointXlsData();
            pointXlsData.id = "P" + (i + 1);
            pointXlsData.name = "Tower1111";
            pointXlsData.x = 15 * (i + 1) + "";
            pointXlsData.y = 15 * (i + 2) + "";
            pointXlsData.z = 15 * (i + 3) + "";
            listInfos.Add(pointXlsData);
        }

        string path = Application.streamingAssetsPath + "/Hsj" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".xls";
        ExcelMaker(path, listInfos);
    }

三:设置excel表格的属性导出表格

public void ExcelMaker(string name, List<RoadPointXlsData> listInfo)
    {
        XlsDocument xls = new XlsDocument();//新建一个xls文档  
        xls.FileName = name;//设定文件名  
        //Add some metadata (visible from Excel under File -> Properties)  
        xls.SummaryInformation.Author = "Hsj"; //填加xls文件作者信息  
        xls.SummaryInformation.Subject = "临时道路路径点信息存储";//填加文件主题信息  

        string sheetName = "临时道路001";
        Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName);//填加名为"chc 实例"的sheet页  
        Cells cells = sheet.Cells;//Cells实例是sheet页中单元格(cell)集合  
        int rowNum = listInfo.Count;
        int rowMin = 1;
        int row = 0;

        for (int i = 0; i < rowNum + 1; i++)
        {
            if (i == 0)
            {
                //根据具体的物体信息 .需要重新写  
                cells.Add(1, 1, "ID");
                cells.Add(1, 2, "关联塔");
                cells.Add(1, 3, "X");
                cells.Add(1, 4, "Y");
                cells.Add(1, 5, "Z");
            }
            else
            {
                cells.Add(rowMin + i, 1, listInfo[row].id);
                cells.Add(rowMin + i, 2, listInfo[row].name);
                cells.Add(rowMin + i, 3, listInfo[row].x);
                cells.Add(rowMin + i, 4, listInfo[row].y);
                cells.Add(rowMin + i, 5, listInfo[row].z);
                row++;
            }
        }
        xls.Save();
        Debug.Log("导出excel成功");
    }

欢迎加入Unity业内qq交流群:956187480

上一篇:PowerDesigner导出表结构到Excel


下一篇:python python-docx办公自动化操作word