最近正在学习了一些关于在unity中读取Word、Excel,然后在其中写入内容或输入内容,在接下来几天,会每天整理和大家分享一下,欢迎大家的交流。
读取Word文件
程序运行前,要提前导入NOPI文件,如果没有这些文件的,我放个百度云链接,自取:
链接:https://pan.baidu/s/1OLRjK5p7L4FFJq7J_Sj_Hw
提取码:z0r8
我的这个word、excel用到的.dll文件一般都有,我存了好久的。麻烦点个赞好么!
在unity中的Assets文件夹中创建Plugins文件夹,将前面下载的.dll拖入这个文件夹即可。
```csharp
using NPOI.XSSF.UserModel;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
public class Read : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
const string SAVEPATH = "C://Users/Administrator/Desktop";//文件的路径,我的放在了桌面
string fileName = "English.docx";//文件名称
string fileContent = "内容";
//如果路径存在
if (Directory.Exists(SAVEPATH))
{
StreamReader sr;//数据流
FileInfo fi = new FileInfo(SAVEPATH + "/" + fileName);
//如果文件存在
if (fi.Exists)
{
sr = fi.OpenText();
fileContent = sr.ReadToEnd();
sr.Close();
sr.Dispose();
Debug.Log("success");
}
else
{
Debug.Log("不存在文件" + SAVEPATH + fileName);
}
}
else
{
Debug.Log("不存在目录" + SAVEPATH);
}
}
}
运行后的结果如下:
读取成功:
未读到文件:
如果需要打开文件,则只需要在上面代码中的第二个if语句中加入这一条代码
System.Diagnostics.Process.Start("C://Users/Administrator/Desktop/English.docx");
更多推荐
在unity中编写程序实现打开Word文件
发布评论