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;
public
class
DataNode
{
public
string CopyName;
public
string CopyPosition;
public
string CopyRotation;
}
public
class
DataCenter
{
public
List<DataNode> List;
public
DataCenter()
{
List =
new
List<DataNode>();
}
}
public
class
JsonConvert : MonoBehaviour {
private
string _txtPath;
private
string _jsonPath;
private
string _excelPath;
void Start ()
{
_jsonPath = Application.streamingAssetsPath +
"/CopyInfo.json"
;
_txtPath = Application.streamingAssetsPath +
"/CopyInfo.txt"
;
_excelPath = Application.streamingAssetsPath +
"/CopyInfo.json"
;
ReadTextToJson();
ReadJsonFromJsonPath();
WriteExcel(_excelPath);
}
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();
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);
}
}
string jsonData = JsonMapper.ToJson(dc.List);
File.WriteAllText(_jsonPath,jsonData);
}
private
void WriteExcel(string path)
{
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);
}
}
Debug.LogError(dc.List.
Count
);
FileInfo excelInfo =
new
FileInfo(path);
if
(excelInfo.Exists)
{
excelInfo.
Delete
();
excelInfo =
new
FileInfo(path);
}
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();
}
}
}