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:\tests.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);
    }
}

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

结果如图:



 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值