java格式化word文档_Java如何格式化word文档中的文本?

本文档介绍了如何在Java编程中利用Apache POI库来格式化Word文档中的文本。开发者需要下载Apache POI软件包,导入相应的jar文件,并通过示例代码展示如何设置加粗、斜体、文本位置、删除线、字体大小和下标等样式。
摘要由CSDN通过智能技术生成

在Java编程中,如何格式化word文档中的文本?

注意:需要访问网址: , 下载一个Apache POI软件包。这里下载最新版本:poi-bin-3.17-20170915.tar.gz解压并将全部.jar文件导入。

需要导入全部包,如下图所示 –

890a64b758ab8d1640a572acfe66e244.png

以下是格式化word文档中的文本的程序。

package com.yiibai; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.VerticalAlign; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class FormatTextInWord { public static void main(String[] args) throws Exception { // Blank Document XWPFDocument document = new XWPFDocument(); // Write the Document in file system FileOutputStream out = new FileOutputStream(new File("fontstyle.docx")); // create paragraph XWPFParagraph paragraph = document.createParagraph(); // Set Bold an Italic XWPFRun paragraphOneRunOne = paragraph.createRun(); paragraphOneRunOne.setBold(true); paragraphOneRunOne.setItalic(true); paragraphOneRunOne.setText("Font Style"); paragraphOneRunOne.addBreak(); // Set text Position XWPFRun paragraphOneRunTwo = paragraph.createRun(); paragraphOneRunTwo.setText("Font Style two"); paragraphOneRunTwo.setTextPosition(100); // Set Strike through and Font Size and Subscript XWPFRun paragraphOneRunThree = paragraph.createRun(); paragraphOneRunThree.setStrike(true); paragraphOneRunThree.setFontSize(20); paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT); paragraphOneRunThree.setText(" Different Font Styles"); document.write(out); out.close(); System.out.println("fontstyle.docx written successully"); } }

执行上面示例代码,得到以下结果 –

fontstyle.docx written successully

生成的World文档,得到以下结果 –

5a709ff2fa66dcc2e45df5344bae4d10.png

¥ 我要打赏 纠错/补充 收藏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值