//========================================================
// 描 述: 很高兴见到你!小王同学,又是精彩的一天,我们一起加油吧!^<>^
// 作 者: 爱吃苹果的小王同学
// 时 间: 2018/10/16 15:24:54
// 版 本: V 1.0
//========================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using LitJson;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using UnityEditor;
using Excel;
public class UserInfo
{
public string UserName;
public string UserPosition;
public string UserRotation;
}
public class InfoManager
{
public List<UserInfo> userInfosList;
public InfoManager()
{
userInfosList = new List<UserInfo>();
}
}
public class ReadTxtWriteToJson : MonoBehaviour {
private string _txtPath;
private string _jsonPath;
private string _ExcelPath;
private string _jsonFromExcelPath;
// Use this for initialization
void Start () {
_txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
_jsonPath = Application.streamingAssetsPath + "/CopyInfo.json";
_ExcelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx";
_jsonFromExcelPath = Application.streamingAssetsPath + "/CopyInfoFromExcel.json";
//ReadTxtToJson();
//ReadJsonWriteToExcel();
//ReadExcelWriteToJson();
}
/// <summary>
/// 读Txt 写入 Json
/// </summary>
void ReadTxtToJson()
{
InfoManager iman = new InfoManager();
using (StreamReader reader = new StreamReader(_txtPath,Encoding.UTF8))
{
string temp = string.Empty;
while (!string.IsNullOrEmpty(temp = reader.ReadLine()))
{
string[] str = temp.Split('_');
UserInfo info = new UserInfo();
info.UserName = str[0];
info.UserPosition = str[1];
info.UserRotation = str[2];
iman.userInfosList.Add(info);
}
}
string jsonInfo = JsonMapper.ToJson(iman.userInfosList);
File.WriteAllText(_jsonPath, jsonInfo);
}
/// <summary>
/// 读Json 写入 Excel
/// </summary>
void ReadJsonWriteToExcel()
{
//读Json 文件
string jsonInfo = File.ReadAllText(_jsonPath);
List<UserInfo> infosList = JsonMapper.ToObject<List<UserInfo>>(jsonInfo);
//写入 Excel
FileInfo info = new FileInfo(_ExcelPath);
if (info.Exists)
{
info.Delete();
info = new FileInfo(_ExcelPath);
}
using (ExcelPackage package = new ExcelPackage(info))
{
ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo");
sheet.Cells[1, 1].Value = "Name";
sheet.Cells[1, 2].Value = "Position";
sheet.Cells[1, 3].Value = "Rotation";
for (int i = 0; i < infosList.Count; i++)
{
sheet.Cells[2 + i, 1].Value = infosList[i].UserName;
sheet.Cells[2 + i, 2].Value = infosList[i].UserPosition;
sheet.Cells[2 + i, 3].Value = infosList[i].UserRotation;
}
sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
sheet.Cells.Style.Font.Bold = true;
sheet.Cells.Style.Font.Name = "宋体";
sheet.Cells.Style.Font.Size = 20;
sheet.Cells.AutoFitColumns(30, 100);
package.Save();
}
AssetDatabase.Refresh();
}
/// <summary>
/// 读一个Excel文件 写入Json文件中
/// </summary>
void ReadExcelWriteToJson()
{
//读Excel
InfoManager man = new InfoManager();
FileStream stream = new FileStream(_ExcelPath, FileMode.Open, FileAccess.Read);
IExcelDataReader dataReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (dataReader.Read() == false)
{
return;
}
while (dataReader.Read())
{
UserInfo userInfo = new UserInfo();
userInfo.UserName = dataReader.GetString(0);
userInfo.UserPosition = dataReader.GetString(1);
userInfo.UserRotation = dataReader.GetString(2);
man.userInfosList.Add(userInfo);
}
//man.userInfosList.RemoveAt(0);
Debug.LogError(man.userInfosList.Count);
//写入Json
string JsonInfo = JsonMapper.ToJson(man.userInfosList);
File.WriteAllText(_jsonFromExcelPath, JsonInfo);
}
}
Unity扩展之文件的读写
最新推荐文章于 2024-06-06 19:41:56 发布