NPOI WPFOMath 在word计算书中使用数学公式
Imports NPOI
Imports NPOI.OpenXmlFormats.Wordprocessing
Imports NPOI.XWPF.Usermodel
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'新建文档
Dim MyDoc As XWPFDocument = New XWPFDocument() '还需要引用ICSharpCode.SharpZipLib.dll
Dim SectPr As CT_SectPr = New CT_SectPr()
SectPr.pgSz.w = CType(11906, ULong) '设置宽度(这里是一个ulong类型)
SectPr.pgSz.h = CType(16838, ULong) '设置高度(这里是一个ulong类型)
MyDoc.Document.body.sectPr = SectPr '设置页面的尺寸
'设置页边距
SectPr.pgMar.left = CType(1200, ULong) '左边距
SectPr.pgMar.right = CType(1200, ULong) '右边距
SectPr.pgMar.top = "1200" '上边距
SectPr.pgMar.bottom = "1200" '下边距
Dim paragraph As XWPFParagraph = Mydoc.CreateParagraph()
paragraph.Alignment = Alignment
paragraph.IndentationFirstLine = CType(100, Integer)
Dim run1 As XWPFRun = paragraph.CreateRun()
run1.IsBold = IsBold
run1.SetText(NeiRong)
run1.FontSize = FontSize
run1.SetColor(Color)
run1.SetFontFamily(FontName, FontCharRange.None)
Dim math As XWPFOMath = paragraph.CreateOMath()
math.CreateRun().SetText("i=")
Dim Rad As XWPFRad = math.CreateRad()
Rad.Degree.CreateRun().SetText("2") '指数为空也不能去掉
Dim f As XWPFF = Rad.Element.CreateF()
f.FractionType = OpenXmlFormats.Shared.ST_FType.lin
f.Denominator.CreateRun().SetText("A")
f.Numerator.CreateRun().SetText("I")
math.CreateRun().SetText("=" & Format(I, "0.0000") & "mm")
End Sub
‘下标’
Sub XiaBiao(Math As XWPFOMath, ZiMu As String, Biao As String)
Dim ssub As XWPFSSub = Math.CreateSSub()
ssub.Element.CreateRun().SetText(ZiMu)
ssub.Subscript.CreateRun().SetText(Biao)
End Sub
‘上标
Sub ShangBiao(Math As XWPFOMath, ZiMu As String, Biao As String)
Dim ssup As XWPFSSup = Math.CreateSSup()
ssup.Element.CreateRun().SetText(ZiMu)
ssup.Superscript.CreateRun().SetText(Biao)
End Sub
‘分数
Sub FenShu(math As XWPFOMath, FenZi As String, FenMu As String)
Dim f As XWPFF = math.CreateF()
'.lin 为左右型
'.bar 为上下型
'.skw 为倾斜型
'.noBar 为没有分数线
f.FractionType = OpenXmlFormats.Shared.ST_FType.bar
f.Denominator.CreateRun().SetText(FenMu)
f.Numerator.CreateRun().SetText(FenZi)
End Sub