【已解决】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 <a0e722754d7f4b6eab4924ea5702b802>: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程序包,下载一个东西

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

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

3.找到这个,并修改以下字样

cs 复制代码
<bindingRedirect oldVersion="0.0.0.0-0.86.0.518" newVersion="0.86.0.518" />

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

ICSharpCode.SharpZipLib.dll0.86.0.518和0.84.0.0资源-CSDN文库https://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文件夹下。

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

运行成功

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

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

    private string path;

    /// <summary>
    /// word文档
    /// </summary>
    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心理辅助治疗系统体验报告");
    }
    /// <summary>
    /// 创建段落
    /// </summary>
    /// <param name="_alignment">对齐方式</param>
    /// <param name="_fontSize">字体大小</param>
    /// <param name="_color">字体颜色(16进制)</param>
    /// <param name="_content">内容</param>
    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("写入成功");
    }

}

相关推荐
龚子亦2 小时前
Unity使用Vuforia插件进行AR开发
unity·游戏引擎·ar
Damon小智3 小时前
C#进阶-在Ubuntu上部署ASP.NET Core Web API应用
linux·nginx·c#·asp.net·.net·.net core
csdn_aspnet3 小时前
C# 或 .NetCore 如何使用 NPOI 导出图片到 Excel 文件
c#·excel·.netcore
PassionY4 小时前
Unity-Mirror网络框架-从入门到精通之AdditiveScenes 示例
unity·unet·photon·networkmanager·mirror·多人联网·ngo
bug终结者10245 小时前
WPF连接USB相机,拍照,视频 示例
数码相机·c#·wpf
tpen6 小时前
C# WinForm 检查目标主机的端口是否可连接
c#
JxHillMan6 小时前
C# 多线程
c#
深度混淆7 小时前
C#,图论与图算法,任意一对节点之间最短距离的弗洛伊德·沃肖尔(Floyd Warshall)算法与源程序
算法·c#·图论
大熊程序猿7 小时前
playwright 模拟登录
c#
A_nanda7 小时前
c#13新特性
开发语言·c#