Java 在Word中創建多級項目符號列表和編號列表


本文分享通過Java程序代碼在Word中創建多級項目符號列表和編號列表的方法。程序運行環境如下:

關於如何導入jar包:

在Java程序中導入jar包。如下方式為下載jar包到本地后,解壓,手動將本地該jar包lib文件夾下的Spire.doc.jar導入java程序的方法

 

jar包導入結果如圖:

 

完成jar導入后,在程序中鍵入如下Java代碼:

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class MultiLevelList {
    public static void main(String[] args) {
        //創建一個Document類的實例
        Document document = new Document();
        //添加Section
        Section sec = document.addSection();

        //添加段落
        Paragraph paragraph = sec.addParagraph();
        paragraph.appendText("Lists");
        paragraph.applyStyle(BuiltinStyle.Title);
        paragraph = sec.addParagraph();
        paragraph.appendText("Numbered List: ").getCharacterFormat().setBold(true);

        //創建編號列表樣式
        ListStyle numberList = new ListStyle(document, ListType.Numbered);//編號列表
        numberList.setName("numberList");
        numberList.getLevels().get(1).setNumberPrefix("\u0000.");
        numberList.getLevels().get(1).setPatternType(ListPatternType.Arabic);
        numberList.getLevels().get(2).setNumberPrefix("\u0000.\u0001.");
        numberList.getLevels().get(2).setPatternType(ListPatternType.Arabic);

        //創建符號列表樣式
        ListStyle bulletList= new ListStyle(document, ListType.Bulleted);//符號列表
        bulletList.setName("bulletList");

        //添加列表樣式
        document.getListStyles().add(numberList);
        document.getListStyles().add(bulletList);

        //添加段落並應用列表樣式
        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 1");
        paragraph.getListFormat().applyStyle(numberList.getName());

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2");
        paragraph.getListFormat().applyStyle(numberList.getName());

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.1");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(1);

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.2");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(1);

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.2.1");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(2);
        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.2.2");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(2);
        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.2.3");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(2);

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.3");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(1);

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 3");
        paragraph.getListFormat().applyStyle(numberList.getName());

        paragraph = sec.addParagraph();
        paragraph.appendText("Bulleted List:").getCharacterFormat().setBold(true);

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 1");
        paragraph.getListFormat().applyStyle(bulletList.getName());
        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2");
        paragraph.getListFormat().applyStyle(bulletList.getName());

        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.1");
        paragraph.getListFormat().applyStyle(bulletList.getName());
        paragraph.getListFormat().setListLevelNumber(1);
        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 2.2");
        paragraph.getListFormat().applyStyle(bulletList.getName());
        paragraph.getListFormat().setListLevelNumber(1);
        paragraph = sec.addParagraph();
        paragraph.appendText("List Item 3");
        paragraph.getListFormat().applyStyle(bulletList.getName());

        //保存文檔
        document.saveToFile("MultiLevelList.docx", FileFormat.Docx);
        document.dispose();
    }
}

執行程序,生成Word結果文檔。代碼中的文檔路徑為IDEA程序項目文件夾路徑,如本次路徑為:F:\IDEAProject\List_Doc\MultiLevelList.docx,Word中的多級項目列表效果如圖:

 

—End—

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM