zoukankan      html  css  js  c++  java
  • unity3d 导出 Excel

    我在unity里需要导出成Excel格式,试了一些方法,其中用c#的com组件的我还没成功不知道该怎么在unity里调用,(如果哪位大哥用别的方法在unity里成功了,可以交流下,最好给我一个小demo(849288321@qq.com),谢谢啦.),不过后来找到了这个org.in2bits.MyXls  ,需要导入这个dll(网上有很多),然后用着还挺好用,我这写的一个小例子仅供参考,

    using UnityEngine;
    using System.Collections;
    using org.in2bits.MyXls;
    using System.Collections.Generic;
    public class TestInfo
    {
        public string name;
        public string id;
        public string num;
    };
    public class ExcelMakerManager  {
    
        public static ExcelMakerManager eInstance;
        public static ExcelMakerManager CreateExcelMakerManager() 
        {
            if(eInstance==null)
            {
                eInstance = new ExcelMakerManager();
            }
            return eInstance;
        }
        //链表为 物体信息 .
        public void ExcelMaker(string name, List<TestInfo> listInfo)
        {
            XlsDocument xls = new XlsDocument();//新建一个xls文档
            xls.FileName = name;// @"D:	ests.xls";//设定文件名
    
            //Add some metadata (visible from Excel under File -> Properties)
            xls.SummaryInformation.Author = "xyy"; //填加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, "ID");
                    cells.Add(1, 3, "数量");
                }
                else
                {
                    cells.Add(rowMin + x, 1, listInfo[row].id);
                    cells.Add(rowMin + x, 2, listInfo[row].name);
                    cells.Add(rowMin + x, 3, listInfo[row].num);
                    row++;
                }
            }
            xls.Save();
        }
    }
    

    然后下面是调用上面的这个方法

    using UnityEngine;
    using System.Collections;
    using System.IO;
    using org.in2bits.MyXls;
    using System;
    using System.Collections.Generic;
    
    public class test : MonoBehaviour
    {
    
        string path;
        TestInfo test1;
        TestInfo test2;
        TestInfo test3;
        List<TestInfo> listInfos;
        // Use this for initialization
        void Start()
        {
            ExcelMakerManager.CreateExcelMakerManager();
    
            //                               --测试数据 
            test1 = new TestInfo();
            test1.id = "one";
            test1.name = "test1";
            test1.num = "x";
    
            test2 = new TestInfo();
            test2.id = "two";
            test2.name = "test2";
            test2.num = "22";
    
            test3 = new TestInfo();
            test3.id = "tree";
            test3.name = "test3";
            test3.num = "333";
    
            listInfos = new List<TestInfo>();
            listInfos.Add(test1);
            listInfos.Add(test2);
            listInfos.Add(test3);
            //                     --测试数据
            // ManagerExcel.CreateE();
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
        void OnGUI()
        {
            if (GUI.Button(new Rect(100, 0, 100, 100), "aa"))
            {
                PrintExcel();
                Debug.Log("aaaa");
            }
        }
        public void PrintExcel()
        {
            if (!Directory.Exists(Application.dataPath + "/Prints"))
            {
                Directory.CreateDirectory(Application.dataPath + "/Prints");
            }
            path = Application.dataPath + "/Prints/Excel_"
                             + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".xls";
            ExcelMakerManager.eInstance.ExcelMaker(path, listInfos);
        }
    }
    
    
    
    
    

    至于改字体啥的网上都有相应的例子.可以自己去看看...这里就不多说了.

    结果如图:



  • 相关阅读:
    6. Flask请求和响应
    5. Flask模板
    FW:Software Testing
    What is the difference between modified duration, effective duration and duration?
    How to push master to QA branch in GIT
    FTPS Firewall
    Query performance optimization of Vertica
    (Forward)5 Public Speaking Tips That'll Prepare You for Any Interview
    (转)The remote certificate is invalid according to the validation procedure
    Change
  • 原文地址:https://www.cnblogs.com/james1207/p/3283426.html
Copyright © 2011-2022 走看看