数据储存为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