Ver Fonte

修改导入导出部分bug

宋悦 há 8 anos atrás
pai
commit
42985cbadf

+ 12 - 5
cqb-comm-utils/src/main/java/com/qmth/cqb/utils/word/DocxProcessUtil.java

@@ -25,6 +25,7 @@ import org.docx4j.dml.wordprocessingDrawing.Inline;
 import org.docx4j.finders.ClassFinder;
 import org.docx4j.jaxb.Context;
 import org.docx4j.math.CTOMath;
+import org.docx4j.math.CTOMathPara;
 import org.docx4j.openpackaging.exceptions.Docx4JException;
 import org.docx4j.openpackaging.io3.stores.PartStore;
 import org.docx4j.openpackaging.io3.stores.ZipPartStore;
@@ -131,13 +132,14 @@ public final class DocxProcessUtil {
      * @param p
      * @return
      */
-    public static boolean isNotText(P p) {
+    public static boolean isText(P p) {
         List<Object> mathList = getAllElementFromObject(p, CTOMath.class);
+        List<Object> mathParaList = getAllElementFromObject(p, CTOMathPara.class);
         List<Object> drawList = getAllElementFromObject(p, Drawing.class);
-        if (mathList.size() > 0 || drawList.size() > 0) {
-            return true;
-        } else {
+        if (mathParaList.size() > 0 || mathList.size() > 0 || drawList.size() > 0) {
             return false;
+        } else {
+            return true;
         }
     }
 
@@ -275,6 +277,11 @@ public final class DocxProcessUtil {
                         "http://schemas.openxmlformats.org/officeDocument/2006/math", "oMath", CTOMath.class);
                 byte[] imgByte = convertMathml2Img(omml2mml(omml));
                 pContent.set(index, newImage(wordMLPackage, imgByte, "math" + (index), "math" + (index)));
+            }else if(child.getClass().equals(CTOMathPara.class)){
+                String omml = XmlUtils.marshaltoString(child, true, true, Context.jc,
+                        "http://schemas.openxmlformats.org/officeDocument/2006/math", "oMathPara", CTOMathPara.class);
+                byte[] imgByte = convertMathml2Img(omml2mml(omml));
+                pContent.set(index, newImage(wordMLPackage, imgByte, "math" + (index), "math" + (index)));
             }
             index++;
         }
@@ -770,12 +777,12 @@ public final class DocxProcessUtil {
         int index = 0;
         for (Relationship relationship : relationships) {
             String tmpId = relationship.getId();
+            String tmp = getRldNum() + (++index);
             for (Object obj : blips) {
                 if (obj.getClass().equals(CTBlip.class)) {
                     CTBlip ctBlip = (CTBlip) obj;
                     String tmpEmbed = ctBlip.getEmbed();
                     if (tmpId.equals(tmpEmbed)) {
-                        String tmp = getRldNum() + (++index);
                         relationship.setId(tmp);
                         ctBlip.setEmbed(tmp);
                     }

+ 44 - 16
cqb-paper/src/main/java/com/qmth/cqb/paper/service/ExportPaperService.java

@@ -231,14 +231,28 @@ public class ExportPaperService {
         String tmpStr = DocxProcessUtil.BODY_HEADER + quesBodyWordMl + DocxProcessUtil.BODY_TAIL;
         Body body = (Body)XmlUtils.unmarshalString(tmpStr,
                 Context.jc,Body.class);
-        P p = (P)body.getContent().get(0);
-        List<Object> pContent = p.getContent();
-        R run = new R();
-        Text text = new Text();
-        text.setValue(num+". ");
-        run.getContent().add(text);
-        pContent.add(0,run);
-        return DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(p));
+        List<Object> pList = body.getContent();
+        int index = 0;
+        for(Object pObj:pList){
+            if(index > 0){
+                break;
+            }
+            P p = (P) pObj;
+            List<Object> pContent = p.getContent();
+            R run = new R();
+            Text text = new Text();
+            text.setValue(num+". ");
+            run.getContent().add(text);
+            pContent.add(0,run);
+            index++;
+        }
+        StringBuffer pWordMl = new StringBuffer();
+        for(Object pObj:pList){
+            if(pObj instanceof P){
+                pWordMl.append(DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj)));
+            }
+        }
+        return pWordMl.toString();
     }
 
     /**
@@ -252,14 +266,28 @@ public class ExportPaperService {
         String tmpStr = DocxProcessUtil.BODY_HEADER + optionWordMl + DocxProcessUtil.BODY_TAIL;
         Body body = (Body)XmlUtils.unmarshalString(tmpStr,
                 Context.jc,Body.class);
-        P p = (P)body.getContent().get(0);
-        List<Object> pContent = p.getContent();
-        R run = new R();
-        Text text = new Text();
-        text.setValue(num+". ");
-        run.getContent().add(text);
-        pContent.add(0,run);
-        return DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(p));
+        List<Object> pList = body.getContent();
+        int index = 0;
+        for(Object pObj:pList){
+            if(index > 0){
+                break;
+            }
+            P p = (P) pObj;
+            List<Object> pContent = p.getContent();
+            R run = new R();
+            Text text = new Text();
+            text.setValue(num+". ");
+            run.getContent().add(text);
+            pContent.add(0,run);
+            index++;
+        }
+        StringBuffer pWordMl = new StringBuffer();
+        for(Object pObj:pList){
+            if(pObj instanceof P){
+                pWordMl.append(DocxProcessUtil.formatPWordMl(XmlUtils.marshaltoString(pObj)));
+            }
+        }
+        return pWordMl.toString();
     }
 
     /**

+ 3 - 3
cqb-paper/src/main/java/com/qmth/cqb/paper/service/ImportPaperService.java

@@ -461,7 +461,7 @@ public class ImportPaperService {
             P pBody = (P) pList.get(i);
             String tmpText = DocxProcessUtil.getPText(pBody);
 
-            if (StringUtils.isEmpty(tmpText)) {
+            if (StringUtils.isEmpty(tmpText) && DocxProcessUtil.isText(pBody)) {
                 // 跳过空白段落
                 continue;
             } else if (tmpText.matches("^\\d{1,}\\.[\\s\\S]*")) {
@@ -518,7 +518,7 @@ public class ImportPaperService {
             P pOption = (P) pList.get(i);
             String tmpText = DocxProcessUtil.getPText(pOption);
 
-            if (StringUtils.isEmpty(tmpText)) {
+            if (StringUtils.isEmpty(tmpText) && DocxProcessUtil.isText(pOption)) {
                 // 跳过空白段落
                 continue;
             } else if (tmpText.matches("^[a-zA-Z]\\.[\\s\\S]*")) {
@@ -583,7 +583,7 @@ public class ImportPaperService {
         for (i = index; i < pList.size(); i++) {
             P pAnswer = (P) pList.get(i);
             String tmpText = DocxProcessUtil.getPText(pAnswer);
-            if (StringUtils.isEmpty(tmpText)) {
+            if (StringUtils.isEmpty(tmpText) && DocxProcessUtil.isText(pAnswer)) {
                 // 跳过空白段落
                 continue;
             } else if (tmpText.startsWith("[答案]")) {