前言
作者今天接到一个工作,需要把一个从其他地方接收过来的json数据观察规律,因为数据比较多,不可能一个一个观察记录,作者想的是把接收过来的数据全部保存到表格中,因为表格处理数据还是比较方便观察的,很省事。废话不多说了,直接上代码。
准备资源
需要一个dll,这里提供下载链接:
org.in2bits.MyXls.Dll
解压密码:fuck
代码
首先把dll导入到自己的项目中,这里就不用做过多的解释了。
接着直接放代码:
using org.in2bits.MyXls;
using QF.Extensions;
using UnityEngine;
namespace QFramework.MFO
{
public class JsonToExcelSystem
{
//保留当前创建的xls文档
private XlsDocument newXls;
//保留当前操作的对应的sheet中的单元格
private Cells newCells;
//当前sheet所添加的行
private int nowRow;
/// <summary>
/// 创建新的Xls文件
/// </summary>
public void CreateExcelFile(string filePath,string fileName,
string author = "LianBai",string subject = "Test")
{
XlsDocument xls = new XlsDocument(); //创建一个新的xls文档
xls.FileName = filePath+fileName; //设置文件名字(路径+名字)
xls.SummaryInformation.Author = author; //设置xls文件作者信息
xls.SummaryInformation.Subject = subject; //设置xls文件主题信息
newXls = xls;
//Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName); //添加一个sheet页面
}
/// <summary>
/// 添加一个新的sheet,并赋予开头
/// </summary>
public void AddNewSheet(string sheetName, params string[] param)
{
if (newXls == null)
{
Debug.LogError("LianBai:Please create new xls");
}
else
{
Worksheet sheet = newXls.Workbook.Worksheets.AddNamed(sheetName); //添加一个新的sheet
newCells = sheet.Cells; //修改当前操作的单元格
int index = 1;
param.ForEach(tipName => //遍历添加标头
{
newCells.Add(1, index, tipName);
index++;
});
nowRow = 2; //初始化从第二行添加数据
}
}
/// <summary>
/// 往创建的sheet里添加数据
/// </summary>
public void AddData(params object[] param)
{
if (newXls == null || newCells == null) //判断文件是否被创建
{
Debug.LogError("LianBai:Please create new xls or sheet");
}
else
{
int index = 1;
param.ForEach(data => //遍历添加数据
{
newCells.Add(nowRow, index, data);
index++;
});
nowRow++; //添加一行后往下添加
}
}
/// <summary>
/// 保存成文件
/// </summary>
public void SaveXls()
{
newXls.Save();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
上述代码都有注释,也就不做过多的解释了。
案例
下面来看看作者自己写的一个案例:
先看看json数据:
[
{
"playerid": 2,
"playericoname": "PlayerIcon_2",
"playerprefabname": "Player_2"
},
{
"playerid": 3,
"playericoname": "PlayerIcon_3",
"playerprefabname": "Player_3"
},
{
"playerid": 4,
"playericoname": "PlayerIcon_4",
"playerprefabname": "Player_4"
},
{
"playerid": 5,
"playericoname": "PlayerIcon_5",
"playerprefabname": "Player_5"
}
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
接着看作者写的案例代码,其中josn读取利用的是QF框架中的,QF是一个非常强大的框架:
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace QFramework.MFO
{
public class MyJsonToExcelExample : MonoBehaviour
{
private JsonToExcelSystem myJsonToExcelSystem = new JsonToExcelSystem();
private string path;
private string fileName = "LianBai"; //文件名字
private List<PlayerData> posData = new List<PlayerData>(); //用来保存json数据
// Start is called before the first frame update
void Start()
{
path = Application.dataPath + "/Prints/"; //文件保存路径
var jsonText = ResLoadManage.Instance.mResLoader.LoadSync<TextAsset>("playeritem").text; //读取本地的json文件
posData = jsonText.FromJson<List<PlayerData>>(); //保存json数据
myJsonToExcelSystem.CreateExcelFile(path,fileName); //创建文件
myJsonToExcelSystem.AddNewSheet("lianbai","ID","IocName","PrefabName"); //创建sheet并且赋值表头
posData.ForEach(data => //遍历数据
{
myJsonToExcelSystem.AddData(data.playerid,data.playericoname,data.playerprefabname); //把数据写入到表格中
});
myJsonToExcelSystem.SaveXls(); //保存文件
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
运行后的结果如下:

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 841774407@qq.com