Apache Poi 读写 word 文档

这篇博客详细介绍了如何使用Apache Poi库进行Word文档的读写操作,包括创建内容和读取文档的步骤。
摘要由CSDN通过智能技术生成

使用 Apache Poi 读写 word 文档

写操作:

package com.poi.word;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

/**
 * 写入 word 
 * @author LGF 2015-01-03
 *
 */
public class WriterWord {
	public static void main(String[] args) throws IOException {
		//创建文档对象
		XWPFDocument doc = new XWPFDocument();
		//创建 XWPFWordExtractor 传入 文档
		XWPFWordExtractor we = new XWPFWordExtractor(doc);
		//创建一个段落
		XWPFParagraph paragraph = doc.createParagraph();
		//一个XWPFRun代表具有相同属性的一个区域。  
		XWPFRun run = paragraph.createRun();
		//执行 10 遍写入 word 文档
		for (int i = 0; i < 10; i++) {
			run = doc.createParagraph().createRun();
			run.setColor("ff0000");
			run.setFontFamily("宋体");
			run.setFontSize(15);
			run.setText("第 "+i+" hello world ...  ");
		}
		//创建一个表格
		XWPFTable table = doc.createTable(10, 3);
//	    table.addNewCol(); //给表格增加一列
//	    table.createRow(); //给表格新增一行
		List<XWPFTableRow> rows = table.getRows();
		for (XWPFTableRow r : rows) {
			List<XWPFTableCell> cs = r.getTableCells();
			for (int i = 0; i < cs.size(); i++) {
				XWPFTableCell c = cs.get(i);
				c.setText(i+" *_* " + new Date());
			}
		}
		//写入
		doc.write(new FileOutputStream("src/test.doc"));
		//释放资源
		we.close();
		System.out.println("success");
		
	}
}

 

读操作:

package com.poi.word;

import java.io.InputStream;
import java.util.List;

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

/**
 * 读取 word 文档
 * @author LGF 2015-01-03
 *
 */
public class ReadWord {
	public static void main(String[] args) throws Exception {
		ReadWord rw = new ReadWord();
		//获取输入流
		InputStream input = rw.getInputStream("test.doc");
		//创建一个 XWPFDocument 对象 传入 输入流
		XWPFDocument doc = new XWPFDocument(input);
		//获取所有的段落
		List<XWPFParagraph> paragraphs = doc.getParagraphs();
		System.out.println("----------段落---------");
		for (XWPFParagraph p : paragraphs) {
			System.out.println(p.getText());
		}
		//获取所有的表格
		List<XWPFTable> tables = doc.getTables();
		System.out.println("----------表格---------");
		for (XWPFTable t : tables) {
			List<XWPFTableRow> rows = t.getRows();
			for (XWPFTableRow r : rows) {
				List<XWPFTableCell> c = r.getTableCells();
				for (XWPFTableCell tc : c) {
					String text = tc.getText();
					System.out.print(text + " --> ");
				}
				System.out.print("\n");
			}
		}
		//创建一个 XWPFWordExtractor 对象 传入 文档对象
		XWPFWordExtractor we = new XWPFWordExtractor(doc);
		//获取文档中的字符
		System.out.println("\n----------全部字符---------");
		String text = we.getText();
		System.out.println(text);
		//释放资源
		we.close();
		input.close();
		
	}
	
	/**
	 * 获取 class path 中的文件流
	 * @param name 名称
	 * @return InputStream 
	 */
	public InputStream getInputStream(String name){
		return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值