poi操作word 2003/doc

HWPFDocument中的要素

Range 表示一个范围(内容:Section,Paragraph,CharacterRun)
Section 文档中的一个小节,文档由多个小节组成(内容:Paragraph)
Paragraph 段落(内容:CharacterRun)
CharacterRun 具有相同属性的文本
Table 表格
TableRow 表格对应的行
TableCell 表格对应的单元格
***Section、Paragraph、CharacterRun和Table都继承自Range***

maven引用

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-scratchpad</artifactId>
	<version>3.17</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.0</version>
</dependency>
<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.0</version>
</dependency>

通过WordExtractor读取文本

//读取文本
InputStream is = new FileInputStream("d://04.doc");
WordExtractor extractor = new WordExtractor(is);
String text = extractor.getText();//抓取文本,里面有许多空格和换行
//抓取文本,提取的结果也可能包含了一些其他的crud比特
String textFromPieces = extractor.getTextFromPieces();
/**
 * 输出书签信息
 * @param bookmarks
 */
private void printBookmarks(Bookmarks bookmarks) {
	int count = bookmarks.getBookmarksCount();
	System.out.println("书签数量:" + count);
	Bookmark bookmark;
	for (int i = 0; i < count; i++) {
		bookmark = bookmarks.getBookmark(i);
		System.out.println("书签" + (i + 1) + "的名称是:" + bookmark.getName());
		System.out.println("开始位置:" + bookmark.getStart());
		System.out.println("结束位置:" + bookmark.getEnd());
	}
}

/**
 * 输出SummaryInfomation
 * @param info
 */
private void printSummary(SummaryInformation info) {
	System.out.println(info.getAuthor());// 作者
	System.out.println(info.getCharCount());// 字符统计
	System.out.println(info.getPageCount());// 页数
	System.out.println(info.getTitle());// 标题
	System.out.println(info.getSubject());// 主题
}

通过HWPFDocument读文件

InputStream is = new FileInputStream("d://04.doc");
HWPFDocument hwpfDocument = new HWPFDocument(is);
// 抓取文本,提取的结果也可能包含了一些其他的crud比特
String text1 = hwpfDocument.getDocumentText();
System.err.println(text1);

Range range = hwpfDocument.getRange();
String text2 = range.text();// 抓取文本,里面有许多空格和换行
System.err.println(text2);
/**
 * 打印段落
 * @param range
 */
private void printParagraph(Range range) {
	int num = range.numParagraphs();
	for (int i = 0; i < num; i++) {
		Paragraph para = range.getParagraph(i);
		if (para.isInList()) {
			System.out.println("list: " + para.text());
		}
	}
}
/**
 * 打印小节
 * @param range
 */
private void printSection(Range range) {
	int secNum = range.numSections();
	System.out.println(secNum);
	for (int i = 0; i < secNum; i++) {
		Section section = range.getSection(i);
		System.out.println(section.getMarginLeft());
		System.out.println(section.getMarginRight());
		System.out.println(section.getMarginTop());
		System.out.println(section.getMarginBottom());
		System.out.println(section.getPageHeight());
		System.out.println(section.text());
	}
}

通过HWPFDocument写文件

public static void main(String[] args) throws IOException {
	String wordMouldFile = "d://tmp//module.doc";
	Map<String, String> replaceMap = new HashMap<String, String>();
	String outputFile = "d://tmp//导出.doc";
	replaceMap.put("title1", "标题一");
	replaceMap.put("content1", "内容一");
	replaceMap.put("title2", "标题二");
	replaceMap.put("content2", "内容二");
	HWPFDocumentUtil hwpfDocumentUtil = new HWPFDocumentUtil(wordMouldFile, replaceMap);
	hwpfDocumentUtil.exportWord(new File(outputFile));
	String readWordText = HWPFDocumentUtil.readWordText(outputFile);
	System.err.println(readWordText);
}

在这里插入图片描述

/**
 * 模板导出Word文档(2003/doc)
 * @author Administrator
 *
 */
public class HWPFDocumentUtil {
	/** Word文档模板 */
	private String wordMouldFile;
	private Map<String, String> replaceMap;
	public HWPFDocumentUtil(String wordMouldFile, Map<String, String> replaceMap) {
		super();
		this.wordMouldFile = wordMouldFile;
		this.replaceMap = replaceMap;
	}

	public static String readWordText(String filename) throws IOException {
		if (StringUtils.isBlank(filename)) {
			return "";
		}
		File file = new File(filename);
		if (!file.exists()) {
			throw new FileNotFoundException(filename);
		}
		
		InputStream is = null;
		HWPFDocument hwpfDocument = null;
		try {
			is = new FileInputStream(filename);
			hwpfDocument = new HWPFDocument(is);
			Range range = hwpfDocument.getRange();
			String text = range.text();
			return StringUtils.trimToEmpty(text);
		} catch (IOException e) {
			throw e;
		} finally {
			IOUtils.closeQuietly(is);
			hwpfDocument.close();
			hwpfDocument=null;
		}
	}

	
	public void exportWord(File outFile) {
		InputStream is = null;
		HWPFDocument doc = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(wordMouldFile);
			doc = new HWPFDocument(is);
			Range range = doc.getRange();//获取文档内容区域
			Set<String> keySet = replaceMap.keySet();
			for(String key : keySet) {
				String value = replaceMap.get(key);
				range.replaceText("$" + key, value);
			}
			os = new FileOutputStream(outFile);
			// 把doc输出到输出流中
			doc.write(os);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				doc.close();
				doc = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
			IOUtils.closeQuietly(is);
			IOUtils.closeQuietly(os);
		}
	}
	...[get set 忽略]
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26264237

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值