97 lines
2.8 KiB
C#
Raw Normal View History

2025-02-27 19:15:03 +08:00
using Aspose.Words;
using Aspose.Words.Replacing;
using UnityEngine;
using Newtonsoft.Json.Linq;
2025-02-28 19:22:44 +08:00
using System;
2025-03-06 13:28:21 +08:00
using System.IO;
2025-02-27 19:15:03 +08:00
public class AsposeHelper : MonoBehaviour
{
//public static void Writer(string json, Action callback = null)
//{
// // <20><><EFBFBD><EFBFBD>Word<72>ĵ<EFBFBD>
// Document doc = new Document(Global.reportDemoPath);
// JObject jObject = JObject.Parse(json);
// foreach (JProperty property in jObject.Properties())
// {
// string key = property.Name;
// string value = property.Value.ToString();
// doc.Range.Replace($"{{{key}}}", $"{value}", new FindReplaceOptions());
// }
// string filePath = ChinarFileController.SaveProject(Path.GetFileName(Global.reportDemoPath).Split('.')[1]);
// if (string.IsNullOrEmpty(filePath) == false)
// {
// doc.Save(filePath);
// }
// callback?.Invoke();
// // <20><EFBFBD>ı<EFBFBD>
// //SaveWithDialog(doc, callback);
// //Debug.Log("<22>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD>ĵ<EFBFBD><C4B5>ѱ<EFBFBD><D1B1>浽: " + outputFilePath);
//}
2025-03-06 13:28:21 +08:00
public static void Writer(string json, Action callback = null)
2025-02-27 19:15:03 +08:00
{
// <20><><EFBFBD><EFBFBD>Word<72>ĵ<EFBFBD>
Document doc = new Document(Global.reportDemoPath);
JToken jToken = JToken.Parse(json);
TraverseAndReplace(jToken, doc);
2025-03-06 13:28:21 +08:00
string filePath = ChinarFileController.SaveProject(Path.GetFileName(Global.reportDemoPath).Split('.')[1]);
if (!string.IsNullOrEmpty(filePath))
2025-03-06 13:28:21 +08:00
{
doc.Save(filePath);
}
callback?.Invoke();
}
private static void TraverseAndReplace(JToken jToken, Document doc)
{
if (jToken.Type == JTokenType.Object)
{
foreach (JProperty property in ((JObject)jToken).Properties())
{
if (property.Value.Type == JTokenType.Object || property.Value.Type == JTokenType.Array)
{
TraverseAndReplace(property.Value, doc);
}
else
{
string key = property.Name;
string value = property.Value.ToString();
doc.Range.Replace($"{{{key}}}", $"{value}", new FindReplaceOptions());
}
}
}
else if (jToken.Type == JTokenType.Array)
{
foreach (JToken item in jToken)
{
TraverseAndReplace(item, doc);
}
}
}
2025-02-27 19:15:03 +08:00
2025-03-06 13:28:21 +08:00
//private static void SaveWithDialog(Document doc, Action<DialogResult> callback)
//{
// SaveFileDialog dialog = new SaveFileDialog();
// dialog.Filter = "Word<72>ĵ<EFBFBD>|*.docx";
// var result = dialog.ShowDialog();
// if (result == DialogResult.OK)
// {
// doc.Save(dialog.FileName);
// }
// callback?.Invoke(result);
//}
2025-02-27 19:15:03 +08:00
}