NPOI导出word文档中插入公式总结
1. XWPFOMath类
XWPFDocument doc = new XWPFDocument();
//创建新行
XWPFParagraph p = doc.CreateParagraph();
//创建空的公式
XWPFOMath math = p.CreateOMath();
通过XWPFParagraph的扩展方法创建
方法名 | 备注 |
---|---|
CreateAcc(); | 创建XWPFAcc类,实现字符在文字上面的类 |
CreateF(); | 创建XWPFF类,实现除法相关的类 |
CreateNary(); | 创建XWPFNary类,实现积分相关的内容 |
CreateRad(); | 创建XWPFRad类,实现根号的操作 |
CreateRun(); | 创建XWPFSharedRun类,实现添加内容操作 |
CreateSSub(); | 创建XWPFSSub类,实现构件下标操作 |
CreateSSup(); | 创建XWPFSSup类,实现上标操作 |
2. XWPFAcc类
定 义:上标类
AccPr属性
控制上标的内容
代码:
下标的内容通过“Element.CreateRun().SetText(“需要写入的内容”);”来添加
var acc = math.CreateAcc();
acc.AccPr = "i";
acc.Element.CreateRun().SetText("X");
3. XWPFF类
定义:创建除法符号
FractionType属性:设置除线的形式是个枚举值
Denominator属性:设置分母内容
Numerator属性:设置分子内容
代码
XWPFF f = math.CreateF();
f.FractionType = ST_FType.skw;
f.Denominator.CreateRun().SetText("n");
var nary = f.Numerator.CreateNary().SetSumm();
nary.Superscript.CreateRun().SetText("n");
nary.Subscript.CreateRun().SetText("i=1");
var ssub = nary.Element.CreateSSub();
ssub.Element.CreateRun().SetText("X");
ssub.Subscript.CreateRun().SetText("i");
4. XWPFNary类
定义:积分类( ⋀ 、∫、∑、⋃符号)。通过方法来添加
//// 摘要:// Sets ⋀ charpublic XWPFNary SetAnd();//// 摘要:// Sets ∫ charpublic XWPFNary SetIntegral();//// 摘要:// Sets ∑ charpublic XWPFNary SetSumm();//// 摘要:// Sets ⋃ charpublic XWPFNary SetUnion();
Superscript属性
添加符号上字符
Subscript属性
定义:添加符号下字符
代码:
XWPFNary nary = f.Numerator.CreateNary().SetSumm();
nary.Superscript.CreateRun().SetText("n");
nary.Subscript.CreateRun().SetText("i=1");
5. XWPFRad类
定义,实现根号的操作
Degree属性
设置n次方,也可以设置公式
代码:
var rad = math.CreateRad();
rad.Degree.CreateRun().SetText("2");var f = rad.Element.CreateF();
f.FractionType = ST_FType.noBar;
f.Denominator.CreateRun().SetText("n-1");var nary = f.Numerator.CreateNary().SetSumm();
nary.Superscript.CreateRun().SetText("n");
nary.Subscript.CreateRun().SetText("i=1");var ssup = nary.Element.CreateSSup();
ssup.Element.CreateRun().SetText("(");var ssub = ssup.Element.CreateSSub();
ssub.Element.CreateRun().SetText("X");
ssub.Subscript.CreateRun().SetText("i");
ssup.Element.CreateRun().SetText("-");var acc = ssup.Element.CreateAcc();
acc.AccPr = "¯";
acc.Element.CreateRun().SetText("X");
ssup.Element.CreateRun().SetText(")");
ssup.Superscript.CreateRun().SetText("2");
6. XWPFSSub类
设置下标类
Subscript属性
设置下标的内容
Element属性
设置主内容
7. XWPFSSup类
设置上标类
Superscript属性
设置上标内容
Element属性
设置主内容
8. 封装公式类
public static class FormulaHelpMethod{/// <summary>/// 添加内容/// </summary>/// <param name="math"></param>/// <param name="value"></param>public static void Add(this MathContainer math, string value){math.CreateRun().SetText(value);}/// <summary>/// 添加上标签内容/// </summary>/// <param name="math"></param>/// <param name="value"></param>/// <param name="up"></param>/// <returns></returns>public static XWPFSSup AddUp(this MathContainer math, string value, string up){XWPFSSup sup = math.CreateSSup();sup.Superscript.CreateRun().SetText(up);sup.Element.CreateRun().SetText(value);return sup;}/// <summary>/// 添加下标签内容/// </summary>/// <param name="math"></param>/// <param name="value"></param>/// <param name="bottom"></param>/// <returns></returns>public static XWPFSSub AddButtom(this MathContainer math, string value, string bottom){XWPFSSub sub = math.CreateSSub();sub.Element.CreateRun().SetText(value);sub.Subscript.CreateRun().SetText(bottom);return sub;}/// <summary>/// 添加上下标/// </summary>/// <param name="math"></param>/// <param name="value"></param>/// <param name="up"></param>/// <param name="bottom"></param>public static void AddUpButtom(this MathContainer math, string value, string up, string bottom){XWPFSSup sup = math.CreateSSup();XWPFSSub sub = sup.Element.CreateSSub();sup.Superscript.CreateRun().SetText(up);sub.Element.CreateRun().SetText(value);sub.Subscript.CreateRun().SetText(bottom);}/// <summary>/// 添加特殊符号/// </summary>/// <param name="math"></param>/// <param name="value"></param>/// <param name="up"></param>/// <param name="bottom"></param>/// <param name="addCharacter">是否添加特殊符号,0为不添加,1为⋀,2为∫,3为∑,4为⋃</param>/// <returns></returns>public static XWPFNary AddNary(this MathContainer math, string value, string up, string bottom, int addCharacter){XWPFNary nary = math.CreateNary();switch (addCharacter){case 0:break;case 1:nary = nary.SetAnd();break;case 2:nary = nary.SetIntegral();break;case 3:nary = nary.SetSumm();break;case 4:nary = nary.SetUnion();break;}nary.Element.CreateRun().SetText(value);nary.Superscript.CreateRun().SetText(up);nary.Subscript.CreateRun().SetText(bottom);return nary;}/// <summary>/// 添加分数内容/// </summary>/// <param name="math"></param>/// <param name="numerator">分子</param>/// <param name="denominator">分母</param>/// <param name="sT_F"></param>/// <returns></returns>public static XWPFF AddDivision(this MathContainer math, string numerator, string denominator, ST_FType sT_F){XWPFF wPFF = math.CreateF();wPFF.FractionType = sT_F;wPFF.Denominator.CreateRun().SetText(denominator);wPFF.Numerator.CreateRun().SetText(numerator);return wPFF;}/// <summary>/// 添加分数内容/// </summary>/// <param name="math"></param>/// <param name="numeratorAction"></param>/// <param name="denominator"></param>/// <param name="sT_F"></param>/// <returns></returns>public static XWPFF AddDivision(this MathContainer math,Action<XWPFOMathArg> numeratorAction, string denominator, ST_FType sT_F){XWPFF wPFF = math.CreateF();wPFF.FractionType = sT_F;numeratorAction?.Invoke(wPFF.Numerator);wPFF.Denominator.CreateRun().SetText(denominator);return wPFF;}/// <summary>/// 添加分数内容/// </summary>/// <param name="math"></param>/// <param name="numerator"></param>/// <param name="denominatorAction"></param>/// <param name="sT_F"></param>/// <returns></returns>public static XWPFF AddDivision(this MathContainer math, string numerator, Action<XWPFOMathArg> denominatorAction, ST_FType sT_F){XWPFF wPFF = math.CreateF();wPFF.FractionType = sT_F;denominatorAction?.Invoke(wPFF.Denominator);wPFF.Numerator.CreateRun().SetText(numerator);return wPFF;}/// <summary>/// 添加分数内容,第一个参数为分子,第二个参数为分母/// </summary>/// <param name="math"></param>/// <param name="numeratorDenominatorAction"></param>/// <param name="sT_F"></param>/// <returns></returns>public static XWPFF AddDivision(this MathContainer math, Action<XWPFOMathArg,XWPFOMathArg> numeratorDenominatorAction, ST_FType sT_F){XWPFF wPFF = math.CreateF();wPFF.FractionType = sT_F;numeratorDenominatorAction?.Invoke(wPFF.Numerator,wPFF.Denominator);return wPFF;}/// <summary>/// 添加根号/// </summary>/// <param name="math"></param>/// <param name="value"></param>/// <param name="degree"></param>/// <returns></returns>public static XWPFRad AddRad(this MathContainer math, string value, string degree){XWPFRad rad = math.CreateRad();rad.Element.CreateRun().SetText(value);rad.Degree.CreateRun().SetText(degree);return rad;}/// <summary>/// 添加等号/// </summary>/// <param name="math"></param>public static void AddEqual(this MathContainer math){math.Add("=");}/// <summary>/// 添加等号/// </summary>/// <param name="math"></param>/// <param name="value"></param>public static void AddEqual(this MathContainer math,string value){math.Add($"={value}");}/// <summary>/// 添加加号/// </summary>/// <param name="math"></param>public static void AddPlus(this MathContainer math){math.Add("+");}/// <summary>/// 添加减号/// </summary>/// <param name="math"></param>public static void AddMinus(this MathContainer math){math.Add("-");}/// <summary>/// 添加乘号/// </summary>/// <param name="math"></param>public static void AddMultiply(this MathContainer math){math.Add("×");}}
总结
上面是在项目中使用的代码总结,希望可以帮助到大家~~~
NPOI导出word文档中插入公式总结相关推荐
- word文档中插入公式的技巧--利用表格
word文档中插入公式的技巧--利用表格 本人愚笨之前在word中加公式的时候,由于需要公式的编号右对齐,且公式居中.我就会在插入了公式之后打上一堆的空格,这样既费事儿,又弄的公式参差不齐,不是十分的 ...
- 【Python】批量导出word文档中的图片、嵌入式文件
Python 批量导出word文档中的图片.嵌入式文件 需求 学生试卷中的题目有要提交截图的,也有要提交文件的,为了方便学生考试,允许单独交或者嵌入Word中提交,那么事后如何整理学生的答案?单独提交 ...
- C# 如何在Word文档中插入艺术字
C# 如何在Word文档中插入艺术字 在Word文档中添加艺术字效果可以让文档的排版更加美观.本篇文章主要介绍如何使用C#和Spire.Doc组件在Word文档中插入艺术字. Spire.Doc支持多 ...
- 解决:word文档中插入照片有一部分不显示
今天在word文档中插入照片的时候,有一部分被隐藏不显示 出现该问题的原因是段落设置的问题,行距是固定值 解决办法: 将行距改为1.5倍行距就可以了
- WORD文档中插入图片(1)
(转) 在WORD文档中插入图片有两种方法:一种是添加到文档的文本层(嵌入型版式):一种是添加到文档的图形层(其他版式). 文本层的图片是不可以设置版式的,而图形层的版式是可以设置的,但只能是 ...
- C#操作Word(五)——在Word文档中插入图片
实例说明 在Word文档的使用过程中,我们可以轻松的向文档中插入图片信息,那么,怎样使用C#向Word文档中插入图片呢?本实例将会介绍一种方法,可以方便的实现上述功能.实例运行效果如图1所示. 图 ...
- (转) 如何在Excel和Word文档中插入GIF动画
Office Excel 和Word文档中插入图片很容易做到,但是要插入GIF动画,并且还要能让这个动画动起来,就不能依照普通的"插入 → 图片 → 来自文件"的方法了,否则的话即 ...
- C# Word文档中插入、提取图片,文字替换图片
Download Files: http://www.c-sharpcorner.com/UploadFile/26b237/image-operations-using-word-document- ...
- 计算机word插图教案,《在WORD文档中插入图片》教学设计
一.教学设计思想 现代教育理论提出"学生是教学活动的主体,老师是教学活动的主导".教师的主导作用体现在组织.指导.帮助和促进学生的学习,充分发挥学生的主动性.积极性.创造性和合作性 ...
最新文章
- 32位postman_谷歌浏览器下载安装postman教程(详细)
- 算法分析的目的_掌握这些数学函数,你会在算法效率的分析时经常用到
- 【NLP实战】Task1 数据集探索
- java类加载-ClassLoader双亲委派机制
- 微前端架构在容器平台的应用
- python选项卡中文详细说明_pycharm窗口选项卡管理
- 摈弃 Windows 低效率的工作方式,发掘 Linux 身上的 UNIX 气质
- Flowmill :为分布式应用程序世界构建的网络可观察性解决方案
- 二阶龙格库塔公式推导_二阶常系数齐次线性方程通解推导(涉及常数变易法和欧拉公式)...
- Java中使用native2ascii工具处理.properties文件
- C专家编程--随记(二)
- Linux使用Firefox播放视频
- 数据挖掘 NLP 之 文本挖掘 文本处理 通用流程
- Neo4j图数据库和GDS图算法应用
- 运气和技术的另类平衡————对大逃杀及战棋类游戏大热的原因思考(正片)
- 高效时间管理的18个黄金法则
- 双离合档把上按钮作用_求双离合档位详细解释
- 关于Fragment与ViewPager交互遇到的问题
- 四川麻将出现天胡与地胡的概率
- 私有云大展拳脚 云计算发展趋势已明朗