リストは、情報を効率的に組織するための非常に効果的な方法です。この機能は、情報を明確かつ直感的に表示し、その論理的関係を示すことができるため、読者の注意を引き付けます。具体的なニーズに応じて、Word ドキュメントに番号付きリスト、箇条書きリスト、または多レベルリストを追加することができます。この記事では、Spire.Doc for Java を使用して Word 文書に次のリストを挿入する方法を示します。 Java で Word に番号付きリストを挿入する Java で Word に箇条書きリストを挿入する Java で Word に複数レベル番号付きリストを挿入する Java で Word に複数レベル混合型リストを挿入する Spire.Doc for Java をインストールします まず、Spire.Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>11.4.2</version> </dependency> </dependencies> Java で Word に番号付きリストを挿入する Spire.Doc for Java には、番号付きリストや箇条書きリストを作成するための ListStyle クラスが用意されています。次に、Paragraph.getListFormat().applyStyle() メソッドを使用して、リストスタイルを段落に適用できます。番号付きリストを作成する手順は次のとおりです。 Document オブジェクトを作成します。 Document.addSection() メソッドを使用してセクションを追加します。 ListStyle クラスのインスタンスを作成し、リストのタイプを 「Numbered」 に指定します。 ListStyle.getLevels().get(index) メソッドを使用して、リストの特定のレベルを取得します。 ListLevel.setPatternType() メソッドを使用して番号タイプを設定します。 Document.getListStyles().add() メソッドを使用して、リストのスタイルをドキュメントに追加します。 Section.addParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。 Paragraph.getListFormat().applyStyle() メソッドを使用して、特定の段落にリストのスタイルを適用します。 Paragraph.getListFormat().setListLevelNumber() メソッドを使用してリストのレベルを指定します。 Document.saveToFile() メソッドを使用して、結果文書を保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListPatternType; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertNumberedList { public static void main(String[] args) { //Documentオブジェクトを作成する Document document = new Document(); //セクションを追加する Section section = document.addSection(); //番号付きリストのスタイルを作成する ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.setName("numberedList"); listStyle.getLevels().get(0).setPatternType(ListPatternType.Decimal_Half_Width); listStyle.getLevels().get(0).setTextPosition(20); document.getListStyles().add(listStyle); //段落を追加する Paragraph paragraph = section.addParagraph(); paragraph.appendText("Web開発に必要なスキル:"); paragraph.getFormat().setAfterSpacing(5); //段落を追加して番号付きリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("HTML"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); //4つの段落を追加し、番号付きリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("CSS"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("JavaScript"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("Python"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("MySQL"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); //結果文書を保存する document.saveToFile("output/NumberedList.docx", FileFormat.Docx); document.close(); } } Java で Word に箇条書きリストを挿入する 箇条書きの作成は、番号付きリストを作成するプロセスと似ています。しかし、リストのスタイルを作成するときに、 [Bulleted] として指定してください。また、箇条書き記号も設定してください。次は詳細な手順です。 Document オブジェクトを作成します。 Document.addSection() メソッドを使用してセクションを追加します。 ListStyle クラスのインスタンスを作成し、リストの種類を 「Bulleted」に指定します。 ListStyle.getLevels().get(index) メソッドを使用してリストの特定のレベルを取得します。 ListLevel.setBulletCharacter() メソッドを使用して箇条書き符号を設定します。 Document.getListStyles().add() メソッドを使用して、リストのスタイルをドキュメントに追加します。 Section.addParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。 Paragraph.getListFormat().applyStyle() メソッドを使用して、特定の段落にリストのスタイルを適用します。 Paragraph.getListFormat().setListLevelNumber() メソッドを使用してリストレベルを指定します。 Document.saveToFile() メソッドを使用して、結果文書を保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertBulletedList { public static void main(String[] args) { //Documentオブジェクトを作成する Document document = new Document(); //セクションを追加する Section section = document.addSection(); //箇条書きリストのスタイルを作成する ListStyle listStyle = new ListStyle(document, ListType.Bulleted); listStyle.setName("bulletedList"); listStyle.getLevels().get(0).setBulletCharacter("u00B7"); listStyle.getLevels().get(0).getCharacterFormat().setFontName("Symbol"); listStyle.getLevels().get(0).setTextPosition(20); document.getListStyles().add(listStyle); //段落を追加する Paragraph paragraph = section.addParagraph(); paragraph.appendText("Web開発に必要なスキル:"); paragraph.getFormat().setAfterSpacing(5); //段落を追加して箇条書きリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("HTML"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); //5つの段落を追加し、箇条書きリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("CSS"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("JavaScript"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("Python"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("MySQL"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("PHP"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); //結果文書を保存する document.saveToFile("output/BulletedList.docx", FileFormat.Docx); document.close(); } } Java で Word に複数レベル番号付きリストを挿入する 多レベルリストは2つ以上のレベルで構成されています。各レベルは、ListStyle.getLevels().get(index) メソッドを使用してアクセスできます。ListLevel オブジェクトを介して、特定のレベルの番号の種類やプレフィックスを設定することができます。次は詳細な手順です。 Document オブジェクトを作成します。 Document.addSection() メソッドを使用してセクションを追加します。 ListStyle クラスのインスタンスを作成し、リストの種類を 「Numbered」 に指定します。 ListStyle.getLevels().get(index) メソッドを使用してリストの特定のレベルを取得し、番号の種類やプレフィックスを設定します。 Document.getListStyles().add() メソッドを使用して、リストスタイルをドキュメントに追加します。 Section.addParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。 Paragraph.getListFormat().applyStyle() メソッドを使用して、特定の段落にリストスタイルを適用します。 Paragraph.getListFormat().setListLevelNumber() メソッドを使用してリストレベルを指定します。 Document.saveToFile() メソッドを使用して、結果文書を保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListPatternType; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertMultilevelNumberedList { public static void main(String[] args) { //Documentオブジェクトを作成する Document document = new Document(); //セクションを追加する Section section = document.addSection(); //番号付きリストスタイルを作成し、各レベルのプレフィックスとパターンタイプを指定する ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.setName("nestedStyle"); listStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic); listStyle.getLevels().get(0).setTextPosition(20); listStyle.getLevels().get(1).setNumberPrefix("u0000."); listStyle.getLevels().get(1).setPatternType(ListPatternType.Arabic); listStyle.getLevels().get(2).setNumberPrefix("u0000.u0001."); listStyle.getLevels().get(2).setPatternType(ListPatternType.Arabic); document.getListStyles().add(listStyle); //段落を追加する Paragraph paragraph = section.addParagraph(); paragraph.appendText("多レベル番号付きリスト:"); paragraph.getFormat().setAfterSpacing(5f); //段落を追加して番号付けリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("The first item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(0); //5つの段を追加し、番号付けリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("The second item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("The first sub-item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("The second sub-item"); paragraph.getListFormat().continueListNumbering(); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph = section.addParagraph(); paragraph.appendText("A sub-sub-item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("The third item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(0); //結果文書を保存する document.saveToFile("output/MultilevelNumberedList.docx", FileFormat.Docx); document.close(); } } Java で Word に複数レベル混合型リストを挿入する 多レベルリストで数字と記号を同時に使用したい場合があります。この機能を実現するには、番号付きリストスタイルと箇条書きリストスタイルを作成し、それらを異なる段落に適用するだけです。詳細な手順は次のとおりです。 Document オブジェクトを作成します。 Document.addSection() メソッドを使用してセクションを追加します。 番号付きリスト スタイルと箇条書きリスト スタイルを作成します。 Section.addParagraph() メソッドを使用して、ドキュメントに複数の段落を追加します。 Paragraph.getListFormat().applyStyle() メソッドを使用して、異なる段落に異なるリストスタイルを適用します。 Document.saveToFile() メソッドを使用して、結果文書を保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListPatternType; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertMultilevelMixedTypeList { public static void main(String[] args) { //Documentオブジェクトを作成する Document document = new Document(); //セクションを追加する Section section = document.addSection(); //番号付きリストスタイルを作成する ListStyle numberedListStyle = new ListStyle(document, ListType.Numbered); numberedListStyle.setName("numberedStyle"); numberedListStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic); numberedListStyle.getLevels().get(0).setTextPosition(20); numberedListStyle.getLevels().get(1).setPatternType(ListPatternType.Low_Letter); document.getListStyles().add(numberedListStyle); //箇条書きリストスタイルを作成する ListStyle bulletedListStyle = new ListStyle(document, ListType.Bulleted); bulletedListStyle.setName("bulletedStyle"); bulletedListStyle.getLevels().get(2).setBulletCharacter("u002A"); bulletedListStyle.getLevels().get(2).getCharacterFormat().setFontName("Symbol"); document.getListStyles().add(bulletedListStyle); //段落を追加する Paragraph paragraph = section.addParagraph(); paragraph.appendText("多レベル混合型リスト:"); paragraph.getFormat().setAfterSpacing(5f); //段落を追加して番号付きリストのスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("The first item"); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph.getListFormat().setListLevelNumber(0); //5つの段落を追加し、異なるリストスタイルを適用する paragraph = section.addParagraph(); paragraph.appendText("The first sub-item"); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("The second sub-item"); paragraph.getListFormat().setListLevelNumber(1); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph = section.addParagraph(); paragraph.appendText("The first sub-sub-item"); paragraph.getListFormat().applyStyle("bulletedStyle"); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("The second sub-sub-item"); paragraph.getListFormat().applyStyle("bulletedStyle"); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("The second item"); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph.getListFormat().setListLevelNumber(0); //結果文書を保存する document.saveToFile("output/MultilevelMixedList.docx", FileFormat.Docx); document.close(); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。