unity 读写Word,需要先导入dll

using NPOI.XWPF.UserModel;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class ReadWrite : MonoBehaviour {
    /// <summary>
    /// 存放word中需要替换的关键字以及对应要更改的内容
    /// </summary>
    public Dictionary<string, string> DicWord = new Dictionary<string, string>();

    private string path = Application.streamingAssetsPath + "/1.docx";
    private string targetPath = Application.streamingAssetsPath + "/3.docx";

    public Text text;

    private void Start()
    {
        if (!File.Exists(path))
        {
            File.Create(path).Dispose();
        }



        DicWord.Add("     1", "00000");
        DicWord.Add("     2", "11111");
        DicWord.Add("     3", "33333");
        DicWord.Add("   0  ", "oh God");
        //Read();
        //WriteDoc();
        ReplaceKeyword();
    }



    private void ReplaceKeyword()
    {
        using (FileStream stream = File.OpenRead(path))
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            XWPFDocument doc = new XWPFDocument(fs);

            foreach (var para in doc.Paragraphs)
            {
                string oldText = para.ParagraphText;
                if (oldText != "" && oldText != string.Empty && oldText != null)
                {
                    string tempText = para.ParagraphText;

                    foreach (KeyValuePair<string, string> kvp in DicWord)
                    {                     
                        if (tempText.Contains(kvp.Key))
                        {
                            tempText = tempText.Replace(kvp.Key, kvp.Value);                         
                        }
                    }
                    para.ReplaceText(oldText, tempText);
                    Debug.Log(tempText);
                    Debug.Log(para.ParagraphText);
                }
            }

            //遍历表格      
            var tables = doc.Tables;
            foreach (var table in tables)
            {
                foreach (var row in table.Rows)
                {
                    foreach (var cell in row.GetTableCells())
                    {
                        foreach (var para in cell.Paragraphs)
                        {
                            string oldText = para.ParagraphText;
                            if (oldText != "" && oldText != string.Empty && oldText != null)
                            {
                                //记录段落文本
                                string tempText = para.ParagraphText;
                                foreach (KeyValuePair<string, string> kvp in DicWord)
                                {
                                    if (tempText.Contains(kvp.Key))
                                    {
                                        tempText = tempText.Replace(kvp.Key, kvp.Value);

                                        //替换内容
                                        para.ReplaceText(oldText, tempText);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //生成指定文件
            //XWPFDocument doc0 = doc;
            FileStream output = new FileStream(targetPath, FileMode.Create);
            //将文档信息写入文件
            doc.Write(output);

            //关闭释放
            fs.Close();
            fs.Dispose();
            output.Close();
            output.Dispose();

            System.Diagnostics.Process.Start(targetPath);//打开指定文件
        }
    }


    private void WriteDoc()
    {
        FileStream file = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

        XWPFDocument doc = new XWPFDocument(file);

        XWPFParagraph p2 = doc.CreateParagraph();
        p2.Alignment = ParagraphAlignment.CENTER;//居中

        XWPFRun r2 = p2.CreateRun();   //插入一行
        r2.SetText("000casdasda000sdasd");
        //r2.SetColor("00FFFF");
        //r2.SetTextPosition(1);
        //r2.SetUnderline(UnderlinePatterns.Single);
        //r2.SetStrike(value: true);
        //r2.SetFontFamily("宋体", FontCharRange.None);

        //插入一张照片
        XWPFParagraph p3 = doc.CreateParagraph();
        p3.Alignment = ParagraphAlignment.CENTER;
        XWPFRun r6 = p3.CreateRun();
        XWPFTable table = doc.CreateTable(1, 4);//创建1*4的表

        table.SetColumnWidth(0, 6 * 256);
        table.SetColumnWidth(1, 10 * 256);
        table.SetColumnWidth(2, 6 * 256);
        table.SetColumnWidth(3, 10 * 256);


        table.GetRow(0).GetCell(0).SetText("11111");
        table.GetRow(0).GetCell(0).SetColor("00FFFF");
        table.GetRow(0).GetCell(1).SetText("11111");
        table.GetRow(0).GetCell(2).SetText("11111");
        table.GetRow(0).GetCell(3).SetText("11111");
        table.GetRow(0).GetCell(3).SetColor("00FFFF");


        doc.Write(file);
        file.Close();
        System.Diagnostics.Process.Start(targetPath);
    }


    void Read()
    {
        FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        XWPFDocument word = new XWPFDocument(file);

        Debug.Log(word.Paragraphs.Count);
        foreach (XWPFParagraph paragraph in word.Paragraphs)
        {
            string message = paragraph.ParagraphText;//获取段落内容
            Debug.Log(message);
            text.text += message;
        }
        file.Close();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值