-
Java 在Word中创建多级项目符号列表和编号列表
本文分享通过Java程序代码在Word中创建多级项目符号列表和编号列表的方法。程序运行环境如下:
- IntelliJ IDEA 2018(JDK 1.8.0)
- Word 2013
- Word Jar包:Free Spire.Doc for Java
关于如何导入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中的多级项目列表效果如图:
出处:https://www.cnblogs.com/Yesi/p/15127031.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比