Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Apache POI处理docx文件中的复选框_Java_Checkbox_Apache Poi_Docx - Fatal编程技术网

Java 使用Apache POI处理docx文件中的复选框

Java 使用Apache POI处理docx文件中的复选框,java,checkbox,apache-poi,docx,Java,Checkbox,Apache Poi,Docx,你能帮帮我吗? 我需要通过ApachePOI在我的MS Worddocx模板中填写复选框。“开发人员”选项卡->控件->复选框已插入复选框,并位于段落->“w:sdt”标记内(不在段落->运行内) 我尝试了段落.getCTP().getFldSimpleList(),但它返回0个字段 那么,还有其他方法可以访问复选框吗 XML部分: <w:p w:rsidR="00C81ACC" w:rsidRDefault="00C81ACC" w:rsidP="004658AE"> &l

你能帮帮我吗? 我需要通过ApachePOI在我的MS Worddocx模板中填写复选框。“开发人员”选项卡->控件->复选框已插入复选框,并位于段落->“w:sdt”标记内(不在段落->运行内)

我尝试了
段落.getCTP().getFldSimpleList()
,但它返回0个字段

那么,还有其他方法可以访问复选框吗

XML部分:

<w:p w:rsidR="00C81ACC" w:rsidRDefault="00C81ACC" w:rsidP="004658AE">
    <w:pPr>
        <w:spacing w:line="276" w:lineRule="auto"/>
        <w:ind w:left="383" w:hanging="383"/>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
            <w:sz w:val="18"/>
            <w:szCs w:val="18"/>
        </w:rPr>
    </w:pPr>
    <w:sdt>
        <w:sdtPr>
            <w:rPr>
                <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
                <w:sz w:val="18"/>
                <w:szCs w:val="18"/>
            </w:rPr>
            <w:id w:val="615721754"/>
            <w14:checkbox>
                <w14:checked w14:val="0"/>
                <w14:checkedState w14:val="2612" w14:font="MS Gothic"/>
                <w14:uncheckedState w14:val="2610" w14:font="MS Gothic"/>
            </w14:checkbox>
        </w:sdtPr>
        <w:sdtContent>
            <w:r>
                <w:rPr>
                    <w:rFonts w:ascii="MS Gothic" w:eastAsia="MS Gothic" w:hAnsi="MS Gothic" w:cs="Arial" w:hint="eastAsia"/>
                    <w:sz w:val="18"/>
                    <w:szCs w:val="18"/>
                </w:rPr>
                <w:t>☐</w:t>
            </w:r>
        </w:sdtContent>
    </w:sdt>
    <w:r>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
            <w:sz w:val="18"/>
            <w:szCs w:val="18"/>
        </w:rPr>
        <w:t xml:space="preserve"> Pass</w:t>
    </w:r>
</w:p>

☐
通过

到目前为止,apache poi不支持这一点。而且由于它使用了扩展的
XML
from
w14
名称空间,所以即使是底层的
ooxml模式
类也不支持这一点。这些模式类是根据2007年发布的
officeopenxml
XML
模式生成的。来自
w14
名称空间的扩展
XML
更晚,不是
officeopenxml
的一部分

所以,如果想要支持这一点,就需要在非常低的
XML
级别上工作。但是对于复选框这样一个简单的事情,可以在这里显示为一个示例

以下代码包含一个类的工作草稿
W14Checkbox
。这提供了一种静态方法来检查给定的
CTSdtRun
是否包含
w14:checkbox
。如果是这种情况,则可以从该
CTSdtRun
创建
W14Checkbox
对象。然后,该对象提供
getChecked
setChecked
方法

请注意,在
setChecked
中,不仅需要设置布尔值
w14:checkbox/w14:checked
,还需要设置
ctsdtcontrun
的相应文本值。这可以是Unicode字符“投票箱”(U+2610)表示未选中,也可以是Unicode字符“投票箱”(U+2612)表示已选中

完整示例:

import java.io.*;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import org.apache.xmlbeans.*;
import javax.xml.namespace.QName;

public class WordFillCheckBox {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument(new FileInputStream("source.docx"));

  for (XWPFParagraph paragraph : document.getParagraphs()) { //go through all paragraphs
   for (CTSdtRun sdtRun : paragraph.getCTP().getSdtList()) {
    if (W14Checkbox.isW14Checkbox(sdtRun)) {
     W14Checkbox w14Checkbox = new W14Checkbox(sdtRun);
     System.out.println(w14Checkbox.getChecked());
     if (w14Checkbox.getChecked()) w14Checkbox.setChecked(false); else w14Checkbox.setChecked(true);
     System.out.println(w14Checkbox.getChecked());
    }
   }
  }

  FileOutputStream out = new FileOutputStream("result.docx");
  document.write(out);
  out.close();
  document.close();

 }

 static class W14Checkbox {
  CTSdtRun sdtRun = null;
  CTSdtContentRun sdtContentRun = null;
  XmlObject w14CheckboxChecked = null;

  W14Checkbox(CTSdtRun sdtRun) {
   this.sdtRun = sdtRun;
   this.sdtContentRun = sdtRun.getSdtContent();
   String declareNameSpaces = "declare namespace w14='http://schemas.microsoft.com/office/word/2010/wordml'";
   XmlObject[] selectedObjects = sdtRun.getSdtPr().selectPath(declareNameSpaces + ".//w14:checkbox/w14:checked");
   if (selectedObjects.length > 0) {  
    this.w14CheckboxChecked  = selectedObjects[0];
   }
  }
  CTSdtContentRun getContent() {
   return this.sdtContentRun;
  }
  XmlObject getW14CheckboxChecked() {
   return this.w14CheckboxChecked;
  }
  boolean getChecked() {
   XmlCursor cursor = this.w14CheckboxChecked.newCursor();
   String val = cursor.getAttributeText(new QName("http://schemas.microsoft.com/office/word/2010/wordml", "val", "w14"));
   return "1".equals(val) || "true".equals(val);
  }
  void setChecked(boolean checked) {
   XmlCursor cursor = this.w14CheckboxChecked.newCursor();
   String val = (checked)?"1":"0";
   cursor.setAttributeText(new QName("http://schemas.microsoft.com/office/word/2010/wordml", "val", "w14"), val);
   cursor.dispose();
   CTText t = this.sdtContentRun.getRArray(0).getTArray(0);
   String content = (checked)?"\u2612":"\u2610";
   t.setStringValue(content);
  }

  static boolean isW14Checkbox(CTSdtRun sdtRun) {
   CTSdtPr sdtPr = sdtRun.getSdtPr();
   String declareNameSpaces = "declare namespace w14='http://schemas.microsoft.com/office/word/2010/wordml'";
   XmlObject[] selectedObjects = sdtPr.selectPath(declareNameSpaces + ".//w14:checkbox");
   if (selectedObjects.length > 0) return true;  
   return false;
  }
 }
}

注意:这只是一个工作草案,需要进一步开发以备生产使用。

大家好,我也有一个类似的问题,但如果未选中此代码,请选中该复选框,反之亦然,我需要根据某些条件选中该复选框,例如假设有三个复选框a、B、,C根据某些条件,我需要选中复选框,但我找不到任何唯一的属性,通过这些属性我可以确定它是复选框A、B或C