Unity中获取数据转换为Excel格式文件储存

数据储存为Excel文件方法:

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;
    }

    /// <summary>
    /// 链表为 excel信息 .
    /// </summary>
    /// <param name="name"> 文件夹名称</param>
    /// <param name="listInfo">存储的excel信息</param>
    public void ExcelMaker(string name, List<TestInfo> listInfo)
    {
        XlsDocument xls = new XlsDocument();              //新建一个xls文档
        xls.FileName = name;                              //设定文件名

        xls.SummaryInformation.Author = "lan";           //填加xls文件作者信息(excel->属性->详细信息)
        xls.SummaryInformation.Subject = "lanTest";         //填加文件主题信息(excel->属性->详细信息)

        string sheetName = "L1";
        Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName);    //excel中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)
            {
                //excel 中表单头部信息,可自动变更过
                cells.Add(1, 1, "name");
                cells.Add(1, 2, "ID");
                cells.Add(1, 3, "num");
            }
            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();
    }
}

 

外部调用储存Excel方法

using UnityEngine;
using System.Collections;
using System.IO;
using org.in2bits.MyXls;
using System;
using System.Collections.Generic;

public class excelTest : MonoBehaviour {

    string path;
    TestInfo test1;
    TestInfo test2;
    TestInfo test3;
    List<TestInfo> listInfos;

    void Start()
    {
        ExcelMakerManager.CreateExcelMakerManager();

        //--测试数据 
        test1 = new TestInfo();
        test1.id = "one";
        test1.name = "lan1";
        test1.num = "x11";

        test2 = new TestInfo();
        test2.id = "two";
        test2.name = "lan2";
        test2.num = "x22";

        test3 = new TestInfo();
        test3.id = "tree";
        test3.name = "lan3";
        test3.num = "x333";

        listInfos = new List<TestInfo>();
        listInfos.Add(test1);
        listInfos.Add(test2);
        listInfos.Add(test3);
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(100, 0, 100, 100), "aa"))
        {
            PrintExcel();
        }
    }

    /// <summary>
    /// 测试存储--文件存储位置Assets/Prints文件夹内
    /// </summary>
    public void PrintExcel()
    {
        Debug.Log("-- 存储Excel");
        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);
    }

 

标题

 

链接:https://pan.baidu.com/s/1Zqf3RSL2xWJF_UFct7whWw 
提取码:13wg

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值