所需附件:
「Excel读写Dll」等文件 https://www.aliyundrive.com/s/RDkpPrmp17u
json格式
{"key":{"a":·12,"b":"abc","e":["1","1","3","4","4","rf","ad"]}
下面的类会在start函数中执行读取解析操作
//功能:读取对应路径下的文件夹,获取文件夹(包括子文件夹)中的图片名称,大小,宽高,相对路径
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using LitJson;
using org.in2bits.MyXls;
public class ReadFilesSaveToExcel : MonoBehaviour
{
/// <summary>
/// excel每一行作为一个类的对象存储
///
/// </summary>
[System.Serializable]
public class Item
{
public string a;
public string b;
public string[] e;
}
private const string m_FilePath = @"Assets\Resources\Levels\";//图片文件夹路径
private const string ExcelPath = "E:/Excel/Excel.xlsx";//Excel的存放路径,需提前创建好Excel
private List<Item> itemInfos = new List<Item>();
private void Start()
{
LoadFolder();
}
/// <summary>
/// 解析json文件
/// </summary>
/// <param name="fileName"></param>
private void ParseJson(string fileName)
{
string url = m_FilePath + fileName;
if (!File.Exists(url))
return;
string jsonData = ReadData(url);
Debug.Log($"{fileName}");
JsonData js = JsonMapper.ToObject(jsonData);
//将json集合转换list
List< string> keyList = new List<string>();
foreach (var item in js.Keys)
{
keyList.Add(item.ToString());
}
for (int i = 0; i < js.Count; i++)
{
var mean = js[i];
Item item = new Item();
item.b = mean["b"].ToString();
JsonData data = mean["e"];
if(data.Count >0 )
{
item.e = new string[data.Count];
for (int j = 0; j < data.Count; j++)
{
string[] sa = data[j].ToString().Split(',');
item.e[j] = sa[sa.Length-1];
}
string key = keyList[i];
item.a = key;
itemInfos.Add(item);
}
}
}
//读取文件
public string ReadData(string fileName)
{
//string类型的数据常量
string readData;
//获取到路径
string fileUrl = fileName;
//读取文件
using (StreamReader sr = File.OpenText(fileUrl))
{
//数据保存
readData = sr.ReadToEnd();
sr.Close();
}
//返回数据
return readData;
}
public void LoadFolder()
{
FileInfo fileInfo = new FileInfo(ExcelPath);
//if (File.Exists(m_TexturePath))//判断单个文件是否存在
if (Directory.Exists(m_FilePath))//判断文件夹是否存在
{
DirectoryInfo directory = new DirectoryInfo(m_FilePath);
FileInfo[] files = directory.GetFiles("*", SearchOption.AllDirectories);//查找改路径下的所有文件夹,包含子文件夹
//FileInfo[] files = directory.GetFiles("*", SearchOption.TopDirectoryOnly);//只查找当前文件夹,不查找子文件夹
//FileInfo[] files = directory.GetFiles("*");//默认是SearchOption.TopDirectoryOnly,只查找当前文件夹,不查找子文件夹
for (int i = 0; i < files.Length; i++)
{
if (!files[i].Name.EndsWith(".json"))
{
continue;
}
ParseJson(files[i].Name);
Debug.Log("files[i].Name:" + files[i].Name);
}
ExcelMaker();
}
}
//创建excel文件
public void ExcelMaker()
{
XlsDocument xls = new XlsDocument();//新建一个xls文档
xls.FileName = @"D:\Levels.xls";//设定文件名
//Add some metadata (visible from Excel under File -> Properties)
xls.SummaryInformation.Author = "haibo"; //填加xls文件作者信息
xls.SummaryInformation.Subject = "Levels";//填加文件主题信息
string sheetName = "Sheet0";
Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);//填加名为"chc 实例"的sheet页
Cells cells = sheet.Cells;//Cells实例是sheet页中单元格(cell)集合
int rowNum = itemInfos.Count;
int rowMin = 1;
int row = 0;
int x = 0;
foreach (var item in itemInfos)
{
if (x == 0)
{
//根据具体的物体信息 .需要重新写
cells.Add(1, 1, "Level");
cells.Add(1, 2, "Letters");
cells.Add(1, 3, "Answer");
}
else
{
cells.Add(rowMin + x, 1, item.a);
cells.Add(rowMin + x, 2, item.b);
string s = string.Empty;
for (int i = 0; i < item.e.Length; i++)
{
s += item.e[i]+"|";
}
cells.Add(rowMin + x, 3, s);
row++;
}
x++;
}
xls.Save();
}
}
执行完毕后会在D盘根目录创建Excel文件
网友评论