Java POI的学习---Word文档

1)POI介绍
Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 .NET的开发人员则可以利用NPOI (POI for .NET) 来存取 Microsoft Office文档的功能。
2)jar下载
注意:需要访问网址:http://poi.apache.org/download.html , 下载一个Apache POI软件包。这里下载最新版本:poi-bin-5.2.0-20220106.tgz解压并将全部.jar文件导入。
3)导入所需要的jar包

实战:
一、如何使用java创建一个空的word文档?

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class CreateWordDocument {
    public static void main(String[] args) throws Exception {

        // new 空白的文档
        XWPFDocument document = new XWPFDocument();

        // 在文件系统中编写文档并给文档命名为:createdocument.docx
        FileOutputStream out = new FileOutputStream(new File("createdocument.docx"));

        document.write(out);
        out.close();

        System.out.println("createdocument.docx written successully");
    }
}

二、如何使用java在word文档中写一个段落?

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class ParagraphInWord {
    public static void main(String[] args) throws Exception {

        // new 一个空白文档
        XWPFDocument document = new XWPFDocument();

        // 在文件系统中编写文档,命名为:createparagraph.docx
        FileOutputStream out = new FileOutputStream(new File("createparagraph.docx"));

        // 创建段落
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(
                "其实,人在脆弱时都想有份停靠,心在无助时都想要个依靠。若有人能解读忧伤,陪伴彷徨,看穿逞强。又何必死撑着非要坚强。小时候脸皮薄,别人两句硬话就要哭出泪来。后来慢慢面子坚硬起来,各种冷嘲热讽可以假装听不见。其实大多数时候,我们没资格优雅的活着。什么时候能厚着脸皮去生活了,你就真懂了生活。你要逼自己优秀,然后骄傲的生活,余生还长,何必慌张,以后的你,会为自己所做的努力,而感到庆幸,别在最好的年纪选择了安逸。新的一天,加油。");

        document.write(out);
        out.close();
        System.out.println("createparagraph.docx written successfully");
    }
}

三、如何使用Java对Word文档中的文本应用边框?

package com.silin;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class BordersToText {
   public static void main(String[] args)throws Exception {

      // new 一个空白文档
      XWPFDocument document = new XWPFDocument();

      // 在文件系统中编写文档,命名
      FileOutputStream out = new FileOutputStream(
         new File("applyingborder.docx"));

      // 创建段落
      XWPFParagraph paragraph = document.createParagraph();

      // 将底部边框设置为段落
      paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);

      //将左边框设置为段落
      paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);

      //将右边框设置为段落
      paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);

      // 将顶部边框设置为段落
      paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);

      XWPFRun run = paragraph.createRun();
      run.setText("其实,人在脆弱时都想有份停靠,心在无助时都想要个依靠。若有人能解读忧伤,陪伴彷徨,看穿逞强。又何必死撑着非要坚强。小时候脸皮薄,别人两句硬话就要哭出泪来。后来慢慢面子坚硬起来,各种冷嘲热讽可以假装听不见。其实大多数时候,我们没资格优雅的活着。什么时候能厚着脸皮去生活了,你就真懂了生活。你要逼自己优秀,然后骄傲的生活,余生还长,何必慌张,以后的你,会为自己所做的努力,而感到庆幸,别在最好的年纪选择了安逸。新的一天,加油。"
      );
      document.write(out);
      out.close();

      System.out.println("applyingborder.docx written successully");
   }
}

四、如何使用Java将表格添加到word文档中?

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class TablesToWord {
    public static void main(String[] args) throws Exception {

        // new 一个空白文档
        XWPFDocument document = new XWPFDocument();

        // 在文件系统中编译文档,命名
        FileOutputStream out = new FileOutputStream(new File("create_table.docx"));

        // 创建表
        XWPFTable table = document.createTable();

        table.setWidth(1000);

        // 创建第一行
        XWPFTableRow tableRowOne = table.getRow(0);

        tableRowOne.getCell(0).setText("1 x 1");
        tableRowOne.addNewTableCell().setText("2 x 1");
        tableRowOne.addNewTableCell().setText("3 x 1");

        // 创建第二行
        XWPFTableRow tableRowTwo = table.createRow();

        tableRowTwo.getCell(0).setText("1 x 2");
        tableRowTwo.getCell(1).setText("2 x 2");
        tableRowTwo.getCell(2).setText("3 x 2");

        // 创建第三行
        XWPFTableRow tableRowThree = table.createRow();

        tableRowThree.getCell(0).setText("1 x 3");
        tableRowThree.getCell(1).setText("2 x 3");
        tableRowThree.getCell(2).setText("3 x 3");

        document.write(out);
        out.close();

        System.out.println("create_table.docx written successully");
    }
}

五、如何使用Java格式化word文档中的文本?

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 {

        // new 一个空白文档
        XWPFDocument document = new XWPFDocument();

        // 在文件系统中编译文档,命名
        FileOutputStream out = new FileOutputStream(new File("fontstyle.docx"));

        // 创建段落
        XWPFParagraph paragraph = document.createParagraph();

        // 设置粗体和斜体
        XWPFRun paragraphOneRunOne = paragraph.createRun();

        paragraphOneRunOne.setBold(true); //粗体
        paragraphOneRunOne.setItalic(true); //斜体
        paragraphOneRunOne.setText("Font Style");
        paragraphOneRunOne.addBreak();

        // 设置文本的位置
        XWPFRun paragraphOneRunTwo = paragraph.createRun();

        paragraphOneRunTwo.setText("Font Style two");
        paragraphOneRunTwo.setTextPosition(100);

        // 设置删除线、和字体大小和下标
        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");
    }
}

六、如何使用Java对齐文档中的文本?

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class AlignTextInWord {
    public static void main(String[] args) throws Exception {

        // new 一个空白文档
        XWPFDocument document = new XWPFDocument();

        // 在文件系统中编译一个文档,命名
        FileOutputStream out = new FileOutputStream(new File("alignparagraph.docx"));

        // 创建段落
        XWPFParagraph paragraph = document.createParagraph();

        // 将对齐段落设置为右对齐
        paragraph.setAlignment(ParagraphAlignment.RIGHT);

        XWPFRun run = paragraph.createRun();
        run.setText("其实,人在脆弱时都想有份停靠,心在无助时都想要个依靠。若有人能解读忧伤,陪伴彷徨,看穿逞强。又何必死撑着非要坚强。小时候脸皮薄,别人两句硬话就要哭出泪来。后来慢慢面子坚硬起来,各种冷嘲热讽可以假装听不见。其实大多数时候,我们没资格优雅的活着。什么时候能厚着脸皮去生活了,你就真懂了生活。你要逼自己优秀,然后骄傲的生活,余生还长,何必慌张,以后的你,会为自己所做的努力,而感到庆幸,别在最好的年纪选择了安逸。新的一天,加油。");

        // 创建另一个段落
        paragraph = document.createParagraph();

        // 将对齐段落设置为居中
        paragraph.setAlignment(ParagraphAlignment.CENTER);

        run = paragraph.createRun();
        run.setText("\r\n其实,人在脆弱时都想有份停靠,心在无助时都想要个依靠。若有人能解读忧伤,陪伴彷徨,看穿逞强。又何必死撑着非要坚强。小时候脸皮薄,别人两句硬话就要哭出泪来。后来慢慢面子坚硬起来,各种冷嘲热讽可以假装听不见。其实大多数时候,我们没资格优雅的活着。什么时候能厚着脸皮去生活了,你就真懂了生活。你要逼自己优秀,然后骄傲的生活,余生还长,何必慌张,以后的你,会为自己所做的努力,而感到庆幸,别在最好的年纪选择了安逸。新的一天,加油。。 ");

        document.write(out);
        out.close();

        System.out.println("alignparagraph.docx written successfully");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值