当前位置:网站首页>Unity-写入Word
Unity-写入Word
2022-07-04 07:43:00 【小太阳每天都要很努力】
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using NPOI.XWPF.UserModel;
using ICSharpCode.SharpZipLib.Zip;
using NPOI.OpenXmlFormats.Wordprocessing;
public class wordRead : MonoBehaviour
{
private void Start()
{
//打开一个文件,如果没有则创建,并设置访问权限
XWPFDocument doc = new XWPFDocument();
FileStream file = new FileStream("D://liuyu.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
Debug.Log(file.CanWrite);//查看是否支持写入
//创建段落,设置段落对齐方式
XWPFParagraph p2 = doc.CreateParagraph();
XWPFParagraph p3 = doc.CreateParagraph();
p2.Alignment = ParagraphAlignment.CENTER;
//插入一行
XWPFRun r2 = p2.CreateRun();
SetFont(r2, "000000", "shiyanbaogao", "宋体", 16, false, true);
//插入一张照片
p3.Alignment = ParagraphAlignment.CENTER;
XWPFRun r6 = p3.CreateRun();
SetTu(r6, 200, 200, "qq");//插入一张图片
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("D://liuyu.docx");
}
/// <summary>
/// 对文字进行操作
/// </summary>
/// <param name="aRun">段落对象</param>
/// <param name="color">颜色</param>
/// <param name="neirong">文字内容</param>
/// <param name="ziti">字体</param>
/// <param name="size">字号</param>
/// <param name="isb">是否加粗</param>
/// <param name="isxie">是否斜体</param>
public static void SetFont(XWPFRun aRun,string color,string neirong,string ziti,int size,bool isb,bool isxie)
{
aRun.IsBold = isb;
aRun.FontFamily = ziti;
aRun.FontSize = size;
aRun.SetText(neirong);
aRun.SetColor(color);
aRun.IsItalic = isxie;
}
/// <summary>
/// 加载图片并设计
/// </summary>
/// <param name="r2">段落对象</param>
/// <param name="sizeX">X的值</param>
/// <param name="sizeY">Y的值</param>
/// <param name="TuName">图片名称</param>
public static void SetTu(XWPFRun r2,float sizeX,float sizeY,string TuName)
{
//插入图片
var widthEmus = (int)(sizeX*9525);
var heightEmus = (int)(sizeY * 9525);
try
{
//using(FileStream picData=new FileStream("D://user/" + TuName + ".png", FileMode.Open, FileAccess.Read))
using (FileStream picData = new FileStream("E://UnityProgramme/word/" + TuName + ".png", FileMode.Open, FileAccess.Read))
{
r2.AddPicture(picData, (int)PictureType.PNG ,TuName+".png", widthEmus, heightEmus);
Debug.Log("png");
}
}
catch
{
try
{
// using (FileStream picData = new FileStream("D://user/" + TuName + ".jpg", FileMode.Open, FileAccess.Read))
using (FileStream picData = new FileStream("E://UnityProgramme/word/" + TuName + ".jpeg", FileMode.Open, FileAccess.Read))
{
r2.AddPicture(picData, (int)PictureType.JPEG, TuName + ".jpeg", widthEmus, heightEmus);
Debug.Log("jpeg");//两种都可以
}
Debug.Log("PNG没找到JPG"+TuName);
}
catch
{
}
}
}
}
参考
边栏推荐
- Rhcsa day 3
- Go learning notes - constants
- How to write a summary of the work to promote the implementation of OKR?
- OKR vs. KPI 一次搞清楚这两大概念!
- A real penetration test
- Zephyr 学习笔记1,threads
- Do you know about autorl in intensive learning? A summary of articles written by more than ten scholars including Oxford University and Google
- ZABBIX monitoring system deployment
- Leetcode(215)——数组中的第K个最大元素
- 谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
猜你喜欢

NPM run build error

Zephyr study notes 2, scheduling
![[C language] open the door of C](/img/e0/2f107966423d6492c39995c77a445e.jpg)
[C language] open the door of C

BUUCTF(3)

Oceanbase is the leader in the magic quadrant of China's database in 2021
![[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)](/img/5c/afb0d43665a8b46579dc604d983790.jpg)
[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)

Detailed introduction to the big changes of Xcode 14

I was pressed for the draft, so let's talk about how long links can be as efficient as short links in the development of mobile terminals

JVM中堆概念

Distributed transaction management DTM: the little helper behind "buy buy buy"
随机推荐
博客停更声明
One of the general document service practice series
2022 - 021arts: début du deuxième semestre
[Android reverse] function interception (use cache_flush system function to refresh CPU cache | refresh CPU cache disadvantages | recommended time for function interception)
This monitoring system can monitor the turnover intention and fishing all, and the product page has 404 after the dispute appears
How to reset IntelliSense in vs Code- How to reset intellisense in VS Code?
Introduction to sap commerce cloud B2B organization function
手写简易版flexible.js以及源码分析
Docker install MySQL
SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
NPM run build error
Project 1 household accounting software (goal + demand description + code explanation + basic fund and revenue and expenditure details record + realization of keyboard access)
Common components of flask
人生规划(Flag)
zabbix監控系統自定義監控內容
MySQL error resolution - error 1261 (01000): row 1 doesn't contain data for all columns
时序数据库 InfluxDB 2.2 初探
Unity 从Inspector界面打开资源管理器选择并记录文件路径
Guoguo took you to write a linked list, and the primary school students said it was good after reading it