更新实验报告导出功能

This commit is contained in:
shenjianxing 2025-03-06 13:28:21 +08:00
parent fd9c20c8a8
commit b2fd885924
6 changed files with 166 additions and 17 deletions

View File

@ -1,17 +1,15 @@
using Aspose.Words;
using Aspose.Words.Replacing;
using QFramework;
using System.IO;
using UnityEngine;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
public class AsposeHelper : MonoBehaviour
{
public static void Writer(string json, Action<DialogResult> callback = null)
public static void Writer(string json, Action callback = null)
{
// 加载Word文档
Document doc = new Document(Global.reportDemoPath);
@ -24,25 +22,32 @@ public class AsposeHelper : MonoBehaviour
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();
// 替换文本
SaveWithDialog(doc, callback);
//SaveWithDialog(doc, callback);
//Debug.Log("文档处理完成,新文档已保存到: " + outputFilePath);
}
private static void SaveWithDialog(Document doc, Action<DialogResult> callback)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "WordÎĵµ|*.docx";
var result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
doc.Save(dialog.FileName);
}
callback?.Invoke(result);
}
//private static void SaveWithDialog(Document doc, Action<DialogResult> callback)
//{
// SaveFileDialog dialog = new SaveFileDialog();
// dialog.Filter = "WordÎĵµ|*.docx";
// var result = dialog.ShowDialog();
// if (result == DialogResult.OK)
// {
// doc.Save(dialog.FileName);
// }
// callback?.Invoke(result);
//}
}

View File

@ -0,0 +1,65 @@
using UnityEngine;
using System.Runtime.InteropServices;
using System;
/// <summary>
/// 文件控制脚本
/// </summary>
public class ChinarFileController : MonoBehaviour
{
/// <summary>
/// 打开项目
/// </summary>
public static string OpenProject()
{
string originalDir = Environment.CurrentDirectory;
string filepath = string.Empty;
OpenFileDlg pth = new OpenFileDlg();
pth.structSize = Marshal.SizeOf(pth);
// 修正过滤器格式(移除末尾多余的竖线)
//pth.filter = @"All Files (*.*)|*.jpg";
pth.file = new string(new char[256]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
pth.initialDir = Application.dataPath.Replace("/", "\\") + "\\Resources";
pth.title = "打开项目";
pth.defExt = "dat";
// 简化Flags设置保留关键选项
pth.flags = 0x00080000 | 0x00001000 | 0x00000800; // OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST
if (OpenFileDialog.GetOpenFileName(pth))
{
filepath = pth.file;
}
Environment.CurrentDirectory = originalDir;
return filepath;
}
/// <summary>
/// 保存文件项目
/// </summary>
public static string SaveProject(string ext)
{
string filepath = string.Empty;
SaveFileDlg pth = new SaveFileDlg();
pth.structSize = Marshal.SizeOf(pth);
pth.filter = "All files (*.*)|*.*";
pth.file = new string(new char[256]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
pth.initialDir = Application.dataPath; //默认路径
pth.title = "保存项目";
pth.defExt = ext;
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (SaveFileDialog.GetSaveFileName(pth))
{
filepath = pth.file; //选择的文件路径;
}
return filepath;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ff1bbdf3a991ec45bbf4e8be1ca5b91
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,57 @@
using System.Runtime.InteropServices;
using System;
/// <summary>
/// 文件日志类
/// </summary>
// [特性(布局种类.有序,字符集=字符集.自动)]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class ChinarFileDlog
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileDlg : ChinarFileDlog
{
}
public class OpenFileDialog
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
}
public class SaveFileDialog
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] SaveFileDlg ofd);
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SaveFileDlg : ChinarFileDlog
{
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9824f58b2cf6cc64ab2ef5d8ec6153b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -40,7 +40,7 @@ namespace QFramework.Example
loader.LoadAsync();
#elif UNITY_STANDALONE_WIN
DownLoad.interactable = false;
AsposeHelper.Writer(GetScoreDataJson(), result =>
AsposeHelper.Writer(GetScoreDataJson(), () =>
{
DownLoad.interactable = true;
});