报错显示
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.找到这个,并修改以下字样
<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文件夹下。
若有冲突报错,删掉之前那个版本的
运行成功
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("写入成功");
}
}