【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518

报错显示

System.TypeLoadException: Could not resolve type with token 01000080 from typeref (expected class 'ICSharpCode.SharpZipLib.Zip.UseZip64' in assembly 'ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73')
at NPOI.OpenXml4Net.OPC.OPCPackage.Save (System.IO.Stream outputStream) [0x00006] in <99616821a9154f2ca86e81f2f10656d7>:0
at NPOI.POIXMLDocument.Write (System.IO.Stream stream) [0x00096] in :0
at WordCreat_Script.createParagraph (NPOI.XWPF.UserModel.ParagraphAlignment _alignment, System.Int32 _fontSize, System.String _color, System.String _content) [0x00044] in E:\Unity20221231\SqliteTest\Assets_VR_HBT_HR_Scripts\Create_WordDoc_Scripts\WordCreat_Script.cs:38
UnityEngine.Debug:LogError (object)
WordCreat_Script:createParagraph (NPOI.XWPF.UserModel.ParagraphAlignment,int,string,string) (at Assets/_VR_HBT_HR_Scripts/Create_WordDoc_Scripts/WordCreat_Script.cs:42)
WordCreat_Script:Start () (at Assets/_VR_HBT_HR_Scripts/Create_WordDoc_Scripts/WordCreat_Script.cs:17)

报错起因

想使用Unity借助NPOI写文档 

Unity 之 实现读取代码写进Word文档功能实现 -- 软著脚本生成工具_unity 读取word-CSDN博客

然后出现了报错显示

 解决方法

找到报错原因

C#使用NPOI进行Excel导入时ICSharpCode.SharpZipLib版本冲突-CSDN博客

解决过程

1.打开vs的nuget程序包,下载一个东西

【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第1张图片

 【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第2张图片

然后继续一些安装流程,安装完成后

2.在解决方案资源管理器中,输入字样进行搜索,并打开对应文件

【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第3张图片 

 3.找到这个,并修改以下字样【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第4张图片

 4.然后还需要导入一个东西

ICSharpCode.SharpZipLib.dll0.86.0.518和0.84.0.0资源-CSDN文库icon-default.png?t=N7T8https://download.csdn.net/download/u012958722/10381599?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-download-2%7Edefault%7ECTRLIST%7ERate-1-10381599-blog-111513918.235%5Ev38%5Epc_relevant_anti_t3&depth_1-utm_source=distribute.pc_relevant.none-task-download-2%7Edefault%7ECTRLIST%7ERate-1-10381599-blog-111513918.235%5Ev38%5Epc_relevant_anti_t3&utm_relevant_index=2

 下载后,将该文件夹拖入Unity的Plugins文件夹下。

【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第5张图片

若有冲突报错,删掉之前那个版本的 

【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第6张图片 

运行成功

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NPOI.XWPF.UserModel;
using System.IO;
using System;
public class WordCreat_Script : MonoBehaviour
{
    /// 
    /// 文件路径
    /// 
    private const string filePath = @"C:/Users/Administrator/Desktop";

    /// 
    /// 文件名称
    /// 
    private string fileName = "david.docx";

    private string path;

    /// 
    /// word文档
    /// 
    private XWPFDocument doc = new XWPFDocument();

    private void Start()
    {
        //缝合路径
        path = Path.Combine(filePath, fileName);
        StartCoroutine(a());
    }
    IEnumerator a()
    {
        yield return new WaitForSeconds(1);
        CreateParagraph(ParagraphAlignment.CENTER, 20, "000000", "VR心理辅助治疗系统体验报告");
    }
    /// 
    /// 创建段落
    /// 
    /// 对齐方式
    /// 字体大小
    /// 字体颜色(16进制)
    /// 内容
    private void CreateParagraph(ParagraphAlignment _alignment, int _fontSize,
        string _color, string _content)
    {
        XWPFParagraph paragraph = doc.CreateParagraph();
        paragraph.Alignment = _alignment;
        XWPFRun run = paragraph.CreateRun();
        run.FontSize = _fontSize;
        run.SetColor(_color);
        run.FontFamily = "宋体";
        run.SetText(_content);
        FileStream fs = new FileStream(path, FileMode.Create);
        doc.Write(fs);
        fs.Close();
        fs.Dispose();
        Debug.Log("写入成功");
    }

}

 【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第7张图片

 【已解决】Unity 使用NPOI 写word文档报错:System.TypeLoadException:……0.86.0.518_第8张图片

你可能感兴趣的:(unity,word,c#)