unity NOPI 创建word文档
导入NOPI 所需要的DLL
ICSharpCode.SharpZipLib.dll,NPOI.dll,NPOI.OOXML.dll,NPOI.OpenXml4Net.dll,NPOI.OpenXmlFormats.dll
打包还需导入I18N.CJK.dll,I18N.dll,I18N.West.dll
如果导入Dll出错,设置如下图所示:
新建脚本-及保存路径
我这边保存到StreamingAssets/Word下面
代码如下:
using NPOI.XWPF.UserModel;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;public class CreateWord : MonoBehaviour
{private string filePath;//保存路径private string fileName = "test.docx";//文件名称private string path;//最终合成路径private XWPFDocument doc = new XWPFDocument();//新建word文档void Start(){filePath = Application.streamingAssetsPath + @"/Word";//设置路径path = Path.Combine(filePath, fileName);//组合路径CreateTestPara("测试文档");}//创建方法private void CreateTestPara(string _content){XWPFParagraph paragraph = doc.CreateParagraph();//设置段落paragraph.Alignment = ParagraphAlignment.CENTER;//设置段落对齐方式paragraph.SetNumID("1");//设置段落编号XWPFRun run = paragraph.CreateRun();//设置文本对象run.FontSize = 20;//设置字体大小run.SetColor("33CC00");//设置字体颜色run.FontFamily = "宋体";//设置字体格式run.SetText(_content);//设置字体内容FileStream fs = new FileStream(path, FileMode.Create);//通过FileStream创建文件doc.Write(fs);//将文档写入文档fs.Close();fs.Dispose();Debug.Log("创建成功");}
}
输出文档展示
Dlll链接
链接:https://pan.baidu.com/s/1ym17mOyiVq5CVPZi8q-DjQ
提取码:eyu3
XWPFParagraph详解和XWPFRun详解可参考
XWPFParagraph详解:
链接: https://blog.csdn.net/u010728594/article/details/98884389
XWPFRun详解
链接: http://www.mamicode.com/info-detail-1990096.html