0
Ich verwende Apache Word POI 3.16. Bitte helfen Sie, die Combobox in Word-Datei zu erstellen.Wie erstellt Combobox in Word-Datei mit Apache Word POI?
Vielen Dank.
Ich verwende Apache Word POI 3.16. Bitte helfen Sie, die Combobox in Word-Datei zu erstellen.Wie erstellt Combobox in Word-Datei mit Apache Word POI?
Vielen Dank.
Für XWPF
(Office OpenXML, *.docx
) ist dies mit den zugrunde liegenden Objekten von ooxml-schemas-1.3.jar
möglich.
Beispiel:
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
public class CreateWordComboboxField {
public static void main(String[] args) throws Exception {
/*
needs the full ooxml-schemas-1.3.jar as mentioned in https://poi.apache.org/faq.html#faq-N10025
*/
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Combobox field: ");
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun ctSdtRun = paragraph.getCTP().addNewSdt();
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtComboBox ctSdtComboBox = ctSdtRun.addNewSdtPr().addNewComboBox();
org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtListItem ctSdtListItem = ctSdtComboBox.addNewListItem();
ctSdtListItem.setDisplayText("please select an option");
ctSdtListItem.setValue("please select an option");
ctSdtListItem = ctSdtComboBox.addNewListItem();
ctSdtListItem.setDisplayText("one");
ctSdtListItem.setValue("one");
ctSdtListItem = ctSdtComboBox.addNewListItem();
ctSdtListItem.setDisplayText("two");
ctSdtListItem.setValue("two");
ctSdtListItem = ctSdtComboBox.addNewListItem();
ctSdtListItem.setDisplayText("three");
ctSdtListItem.setValue("three");
ctSdtRun.addNewSdtContent().addNewR().addNewT().setStringValue("please select an option");
run = paragraph.createRun();
run.setText(" after combobox field...");
paragraph = document.createParagraph();
document.write(new FileOutputStream("CreateWordComboboxField.docx"));
document.close();
}
}
Aber fragen Sie mich nicht für HWPF
(binär *.doc
-Format). Dieses schreckliche altmodische Format ist nichts, womit ich mich beschäftigen würde.
Vielen Dank @Axel Richter. Es funktioniert. Danke vielmals. –