如何读取txt文件写入Json文件 + txt文件转Excal文件

怎么写入Json文件?

Json文件的解析式非常快的 一般联网游戏的数据都是通过Json文件类型传递的

1.首先定义class class必须有一个List<> list list存储类型为string类型
2.然后进行txt数据的读取 并写rulist
3.Json.mapper.tojson(定义的class 对象)
4.file.writealltext(_jsonpath,jsondate) -->路径,内容

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
using UnityEngine;


/// <summary>
/// 该class用于json的时候不能有构造函数
/// </summary>
public class DataNode
{
    public string CopyName;
    public string CopyPosition;
    public string CopyRotation;
}

public class DataCenter  //先定义一个class class有一个List<>
{
    public List<DataNode> List;

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

public class JsonConvert : MonoBehaviour {

	// Use this for initialization
    private string _txtPath;     //定义两个文件的路径
    private string _jsonPath;
    
	void Start ()
	{
        _jsonPath = Application.streamingAssetsPath + "/CopyInfo.json";
	    _txtPath = Application.streamingAssetsPath + "/CopyInfo.txt";
	   // Json的解析是很快的 网络
        ReadTextToJson();
	    ReadJsonFromJsonPath();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    void ReadJsonFromJsonPath()  //读取Json文件
    {
      string jsondata =  File.ReadAllText(_jsonPath);
        //依靠这种格式  解析出来对应的列表
        List<DataNode> node = JsonMapper.ToObject<List<DataNode>>(jsondata);
        Debug.LogError(node.Count);
    }
    void ReadTextToJson()  //读取TXT文件并写成Json文件
    {
        DataCenter dc = new DataCenter();
        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); //添加到定义的List<>
            }
        }
        //数据读取完毕 开始写入json 传递的List<>
        string jsonData = JsonMapper.ToJson(dc.List); //转化为Json
        File.WriteAllText(_jsonPath,jsonData); //写入(路径,内容)
    }
}

1.对数据进行操作的必须使用system.data(和版本一致)的这个dll文件

2. D:\unity\Editor\Data\Mono\lib\mono\2.0

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




public class ToExcel : MonoBehaviour {

    private List<CopyInfo> _copyInfoList;
    private string _excelPath;
    private string _path;
    // Use this for initialization
    void Start () {
        _copyInfoList = new List<CopyInfo>();
        _path = Application.streamingAssetsPath + "/CopyInfo.txt";
        _excelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx";
        ReadFileToList(_copyInfoList, _path);
        WriteExcel(_excelPath, _copyInfoList);
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    

    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 < list.Count; i++)
            {
                sheet.Cells[2 + i, 1].Value = list[i].CopyName;
                sheet.Cells[2 + i, 2].Value = list[i].CopyPosition;
                sheet.Cells[2 + i, 3].Value = 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();
        }
        AssetDatabase.Refresh();
    }
    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])));
            }
        }
    }
    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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43140883/article/details/83246674
今日推荐