C# 学习 txt --> excel txt --> json
在Unity3D 中使用,需要配置环境,\Assets\Plugins

Txt --> Excel
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using LitJson; using UnityEngine; public class DataNode { public string CopyName; public string CopyRace; public string CopyProfession; } public class DataCenter { public List<DataNode> List; public DataCenter() { List =new List<DataNode>(); } } public class ToJson : MonoBehaviour { private string _txtPath; private string _jsonPath; // Use this for initialization void Start () { _jsonPath = Application.streamingAssetsPath + "/CopyInfo.json"; _txtPath = Application.streamingAssetsPath + "/CopyInfo.txt"; TextToJson(); ReadJsonFromJsonPath(); } private void TextToJson() { DataCenter dc = new DataCenter(); using (StreamReader reader = new StreamReader(_txtPath, Encoding.UTF8)) { string temp = string.Empty; while (!string.IsNullOrEmpty(temp = reader.ReadLine())) { string[] infos = temp.Split('_'); DataNode _node = new DataNode(); _node.CopyName = infos[0]; _node.CopyRace = infos[1]; _node.CopyProfession = infos[2]; dc.List.Add(_node); } } string jsonData = JsonMapper.ToJson(dc.List); File.WriteAllText(_jsonPath,jsonData); } private void ReadJsonFromJsonPath() { string jsonData = File.ReadAllText(_jsonPath); List<DataNode> nodes = JsonMapper.ToObject<List<DataNode>>(jsonData); Debug.Log(nodes.Count); } // Update is called once per frame void Update () { } }
Txt --> Json
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; using Excel; using OfficeOpenXml; using Excel.Core; using System.Data; using OfficeOpenXml.Style; using UnityEditor; using LitJson; using UnityEngine.Analytics; class CopyInfo { public string CopyName; public string CopyRace; public string CopyProfession; public CopyInfo(string name, string race, string profession) { CopyName = name; CopyRace = race; CopyProfession = profession; } } public class ToExcel : MonoBehaviour { private List<GameObject> _copyList; private List<CopyInfo> _copyInfoList; private string _excelPath; private string _path; // Use this for initialization void Start () { _path = Application.streamingAssetsPath + "/CopyInfo.txt"; _excelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx"; _copyList = new List<GameObject>(); _copyInfoList = new List<CopyInfo>(); ReadFileToList(_copyInfoList,_path); WriteExcel(_copyInfoList); } private void ReadFileToList(List<CopyInfo> list, string path) { using (StreamReader reader = new StreamReader(path,Encoding.UTF8)) { string tempstr = string.Empty; while (!string.IsNullOrEmpty(tempstr = reader.ReadLine())) { string[] tmpInfos = tempstr.Split('_'); list.Add(new CopyInfo(tmpInfos[0], tmpInfos[1], tmpInfos[2])); } } } private void WriteExcel(List<CopyInfo> list) { FileInfo excelInfo = new FileInfo(_excelPath); if (excelInfo.Exists) { excelInfo.Delete(); excelInfo = new FileInfo(_excelPath); } using (ExcelPackage package = new ExcelPackage(excelInfo)) { ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); sheet.Cells[1, 1].Value = "名字"; sheet.Cells[1, 2].Value = "种族"; sheet.Cells[1, 3].Value = "职业"; for (int i = 0; i < _copyInfoList.Count; i++) { sheet.Cells[2 + i, 1].Value = _copyInfoList[i].CopyName; sheet.Cells[2 + i, 2].Value = _copyInfoList[i].CopyRace; sheet.Cells[2 + i, 3].Value = _copyInfoList[i].CopyProfession; } sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center; sheet.Cells.Style.Font.Bold = false; sheet.Cells.Style.Font.Name = "黑体"; sheet.Cells.Style.Font.Size = 15; sheet.Cells.AutoFitColumns(30,50); package.Save(); } AssetDatabase.Refresh(); } }

浙公网安备 33010602011771号