Unity扩展之文件的读写

在这里插入图片描述

//========================================================
// 描 述: 很高兴见到你!小王同学,又是精彩的一天,我们一起加油吧!^<>^
// 作 者: 爱吃苹果的小王同学 
// 时 间: 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);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值