在unity中编写程序实现打开Word文件

编程知识 更新时间:2023-04-07 16:24:41

最近正在学习了一些关于在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文件

本文发布于:2023-04-07 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/0975c1bbbae099a567e92a3f1ff09233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   程序   unity   Word

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 53945文章数
  • 14阅读数
  • 0评论数