1. 程式人生 > >TXT轉json,轉Excel

TXT轉json,轉Excel

轉Excel

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;


/// <summary>
/// 儲存資訊的 
/// </summary>
class CopyInfo { public string CopyName; public Vector3 CopyPosition; public Quaternion CopyRotation; public CopyInfo(string name, Vector3 postion, Vector3 rotation) { CopyName = name; CopyPosition = postion; CopyRotation = Quaternion.Euler(rotation)
; } } public class CreateManager : MonoBehaviour { private List<CopyInfo> _copyInfoList; private string _excelPath; private string _path; //1. 讀檔案 //2. 解析檔案資訊 //3. 例項化 //4. 例項化6額 //4.1 在3s中銷燬一個並且新增一個 void Start () { _path = Application.streamingAssetsPath +
"/CopyInfo.txt"; _excelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx"; _copyInfoList = new List<CopyInfo>(); // //Application.streamingAssetsPath 資料夾 streamingAssets ReadFileToList(_copyInfoList,_path); WriteExcel(_excelPath,_copyInfoList); } // Update is called once per frame void Update () { } // 訪問型別 返回值 方法名稱 方法引數 private void ReadFileToList(List<CopyInfo> list, string path) { using (StreamReader reader = new StreamReader(path,Encoding.UTF8)) { string tmpStr = string.Empty; while ( ! string.IsNullOrEmpty(tmpStr = reader.ReadLine())) { string[] tmpInfos = tmpStr.Split('_'); list.Add(new CopyInfo(tmpInfos[0], StrToV3(tmpInfos[1]), StrToV3(tmpInfos[2]))); } Debug.LogError(list.Count); } } //strng --> v3 //1,2,3 private Vector3 StrToV3(string str) { Vector3 tmp = new Vector3(); string[] infos = str.Split(','); tmp.Set( System.Convert.ToSingle(infos[0]), System.Convert.ToSingle(infos[1]), System.Convert.ToSingle(infos[2]) ); return tmp; } private void WriteExcel(string path, List<CopyInfo> list) { FileInfo excelInfo = new FileInfo(path); if (excelInfo.Exists) { excelInfo.Delete(); excelInfo = new FileInfo(path); } //開始shiyong Excel using (ExcelPackage package = new ExcelPackage(excelInfo)) { ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); // 添加了一個工作表 sheet.Cells[1, 1].Value = "CopyName"; sheet.Cells[1, 2].Value = "CopyPosition"; sheet.Cells[1, 3].Value = "CopyRotation"; 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].CopyPosition; sheet.Cells[2 + i, 3 ].Value = _copyInfoList[i].CopyRotation; } 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 = 28; sheet.Cells.AutoFitColumns(50, 150); package.Save(); } AssetDatabase.Refresh(); } }

轉json

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
using UnityEngine;
using Excel;
using Excel.Core;
using OfficeOpenXml.Style;
using OfficeOpenXml;




/// <summary>
/// 該class用於json的時候不能有建構函式
/// </summary>
public class DataNode//自定義類來承接一會讀出來的資料分類
{
    public string CopyName;
    public string CopyPosition;
    public string CopyRotation;
}

public class DataCenter//自定義類包含List列表來新增一會讀取出來的的資料資訊
{
    public List<DataNode> List;

   public DataCenter()
    {
        List =new List<DataNode>();
    }
}

public class JsonConvert : MonoBehaviour {

	// Use this for initialization
    private string _txtPath;//TXT檔案路徑
    private string _jsonPath;//轉換後寫入的json路徑
    private string _excelPath;
    
	void Start ()
	{
        _jsonPath = Application.streamingAssetsPath + "/CopyInfo.json";//定義路徑
	    _txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
        _excelPath = Application.streamingAssetsPath + "/CopyInfo.json";
	   // Json的解析是很快的 網路
        ReadTextToJson();//讀取TXT檔案並轉化為Json
	    ReadJsonFromJsonPath();//讀取Json檔案
        WriteExcel(_excelPath);

    }
	
	// Update is called once per frame
	void Update () {
		
	}

    void ReadJsonFromJsonPath()
    {
        //               讀取全部(檔案路徑)
      string jsondata =  File.ReadAllText(_jsonPath);
        List<DataNode> node = JsonMapper.ToObject<List<DataNode>>(jsondata);//固定格式
        Debug.LogError(node.Count);
    }
    void ReadTextToJson()
    {
        DataCenter dc = new DataCenter();//例項化dc,待會用其List
        //讀檔案固定格式
        using (StreamReader reader = new StreamReader(_txtPath,Encoding.UTF8))
        {
            string tmpStr = string.Empty;
            while ( !string.IsNullOrEmpty(tmpStr = reader.ReadLine()))
            {
                string[] infos = tmpStr.Split('_');
                DataNode _node = new DataNode();//例項化呼叫其屬性
                _node.CopyName = infos[0];//把讀取的內容賦值
                _node.CopyPosition = infos[1];
                _node.CopyRotation = infos[2];
                dc.List.Add(_node);//把內容新增進列表
            }
        }
        //資料讀取完畢 開始寫入json 傳遞的List<>
        string jsonData = JsonMapper.ToJson(dc.List);
        File.WriteAllText(_jsonPath,jsonData);

    }
    private void WriteExcel(string path)
    {
        DataCenter dc = new DataCenter();//例項化dc,待會用其List
        //讀檔案固定格式
        using (StreamReader reader = new StreamReader(_txtPath, Encoding.UTF8))
        {
            string tmpStr = string.Empty;
            while (!string.IsNullOrEmpty(tmpStr = reader.ReadLine()))
            {
                string[] infos = tmpStr.Split('_');
                DataNode _node = new DataNode();//例項化呼叫其屬性
                _node.CopyName = infos[0];//把讀取的內容賦值
                _node.CopyPosition = infos[1];
                _node.CopyRotation = infos[2];
                dc.List.Add(_node);//把內容新增進列表
            }
        }
        Debug.LogError(dc.List.Count);
        FileInfo excelInfo = new FileInfo(path);
        if (excelInfo.Exists)
        {
            excelInfo.Delete();
            excelInfo = new FileInfo(path);

        }

        //開始使用 Excel 
        using (ExcelPackage package = new ExcelPackage(excelInfo))
        {
            ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); // 添加了一個工作表
            sheet.Cells[1, 1].Value = "CopyName";
            sheet.Cells[1, 2].Value = "CopyPosition";
            sheet.Cells[1, 3].Value = "CopyRotation";

            for (int i = 0; i < dc.List.Count; i++)
            {
                sheet.Cells[2 + i, 1].Value = dc.List[i].CopyName;
                sheet.Cells[2 + i, 2].Value = dc.List[i].CopyPosition;
                sheet.Cells[2 + i, 3].Value = dc.List[i].CopyRotation;
            }
            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 = 28;
            sheet.Cells.AutoFitColumns(50, 150);

            package.Save();
        }


        
    }
}