天天看點

java 用程式生成公式在docx文檔中

package cn.com.eduedu.jee.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.officeDocument.x2006.math.CTOMath;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTRad;
import org.openxmlformats.schemas.officeDocument.x2006.math.CTR;
import org.openxmlformats.schemas.officeDocument.x2006.math.STStyle;
public class MSDocxCreateFormulaInDocx {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        XWPFDocument doc= new XWPFDocument();

          XWPFParagraph paragraph = doc.createParagraph();
          XWPFRun run=paragraph.createRun();  
          run.setText("The Formula: ");

          CTOMath cTOMath = paragraph.getCTP().addNewOMath();
          CTR cTR = cTOMath.addNewR();
          cTR.addNewRPr().addNewSty().setVal(STStyle.P);
          cTR.addNewT2().setStringValue("a²+b²=c²");

          run=paragraph.createRun();  
          run.setText(" text after the formula");

          paragraph = doc.createParagraph();
          run=paragraph.createRun();  
          run.setText("The Formula: ");

          cTOMath = paragraph.getCTP().addNewOMath();
          CTRad cTRad = cTOMath.addNewRad();
          cTR = cTRad.addNewDeg().addNewR();
          cTR.addNewRPr().addNewSty().setVal(STStyle.P);
          cTR.addNewT2().setStringValue("2");
          cTR = cTRad.addNewE().addNewR();
          cTR.addNewRPr().addNewSty().setVal(STStyle.P);
          cTR.addNewT2().setStringValue("a²+b²");

          run=paragraph.createRun();  
          run.setText(" text after the formula");  

          doc.write(new FileOutputStream("d:\\WordFormula.docx"));
          doc.close();
    }
}
           

轉載的,在哪轉的找不到了..抱歉.