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 ⋀ char
public XWPFNary SetAnd();
//
// 摘要:
// Sets ∫ char
public XWPFNary SetIntegral();
//
// 摘要:
// Sets ∑ char
public XWPFNary SetSumm();
//
// 摘要:
// Sets ⋃ char
public 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("×");
}
}
总结
上面是在项目中使用的代码总结,希望可以帮助到大家~~~