JAVA POI处理WORD

关键字:POI WORD HWPF

WORD文本组成:文本、图、表和宏。HWPF主要关注文本。

1.
File Information Block (FIB): 文件信息块

2.
文本提取  text extraction
基本文本提取 : org.apache.poi.hwpf.extractor.WordExtractor
getText() : TEXT
getParagraphText() : TEXT
getTextFromPieces() : YMMV

特殊文本提取 : org.apache.poi.hwpf.HWPFDocument
getRange()

输入流: HWPFDocument

3.
页眉和页脚:
a. 创建 org.apache.poi.hwpf.HWPFDocument
b. 创建 org.apache.poi.hwpf.usermodel.HeaderStores

4.
插入文本
Range 范围 Paragraph 段落 CharacterRun ?
insertBefore()
insertAfter()
delete()

5.
读取WORD文档
a. 首先是 File Information Block (FIB):它指定文件的大小,位置,文本开始位置。
b. 开始读取文本块
non-unicode 块 与 unicode 块 的偏移值需要通过掩码来区分。

6.
文本样式
a. 段落样式
b. 字条样式

7.
官方单元测试代码
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/

8.
示例
本程序主要是提取WORD的表格到EXCEL.
运行方法: wordTable2Excel.需要c:\test.doc存在.

WordTable.java

package cn.bisoft.java.poi;

import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.junit.Test;

public class WordTable {
	
	/**
	 * 转换WORD中的表格内容到EXCEL
	 */
	@Test
	public void wordTable2Excel()
	{
		String docName = "c:\\test.doc";
		
		/** 1. 读取WORD表格内容 */
		HWPFDocument doc = null;
		
		try {
			doc = new HWPFDocument(new FileInputStream(docName));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		String text = doc.getRange().text();
		
		/** 2. 放置分隔符:将不可见字符使用空格(32)替换 */
		char[] charArray = text.toCharArray();
		
		for(int i = 0; i < charArray.length; i++)
		{
			if (charArray[i] <= 31)
			{
				charArray[i] = 32;
			}
		}
		
		text = String.valueOf(charArray);
		
		/** 3. 将内容用空格切片 */
		String[] textArray = text.trim().replaceAll("[ ]+", " ").split(" ");
		
		/** 4.将数据加载到WORD表格模型对象 */
		WordTableModel wtm = new WordTableModel();
		
		for(int i = 0; i < textArray.length; i++)
		{
			String s = textArray[i].trim();
			
			// 行首单元格正则匹配
			if (s.matches("^[a-zA-Z]$") || s.matches("^[0-9]{2}$") || s.matches("^[0-9]{3}$") || s.matches("^[0-9]{4}$"))
			{
				// 换行 
				wtm.resetMap();
			}
			
			// 加载数据, 每行内容按顺序加载 到相应单元格
			wtm.autoMap(s.trim());
			
		}
		
		// 结束加载
		wtm.endAutoMap();
		
		/** 5. 保存到EXCEL文件*/
		wtm.save2Excel("c:\\test.xls", "sheet1");
		
	}
}





WordTableModel
package cn.bisoft.java.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import cn.bisoft.java.poi.util.ExcelUtil;

public class WordTableModel {

	public static  String SINGLE_WORD = "^[a-z][A-Z]$";
	public static  String SINGLE_NUMBER = "^[0-9]$";
	public static  String SINGLE_VAR = "^[0-9a-zA-Z_]$";
	public static  String PSTN = "^[0-9]{4}[- ]?[0-9]{7}$"; // 固定号码
	public static  String MSISDN = "^1[0-9]{10}$"; // 移动号码
	public static  String IDCARD = "(^\\d{15}$)|(^\\d{17}([0-9]|[Xx])$)"; //身份证
	public static  String EMAIL = "^\\w{3,}@\\w.{3,}\\w{2,}$"; // 电子邮件
	
	private String[][] table = new String[100000][1000];
	private int curColumn = 0;
	private int curRow = 0;

	public void autoMap(String value)
	{
		table[curRow][curColumn] = value;
		curColumn++;
	}
	
	public void resetMap()
	{
		curColumn = 0;
		curRow++;
	}
	
	public void endAutoMap()
	{
		curRow++;
	}
	
	/**
	 * @param fileName
	 */
	public void save2Excel(String fileName, String sheetName)
	{
		Sheet sheet = ExcelUtil.createSheet(fileName, sheetName);
		
		for(int i = 0; i < curRow; i++)
		{
			Row row = sheet.createRow(i);
			
			for(int j = 0; j < curColumn; j++)
			{
				Cell cell = row.createCell(j);
				cell.setCellValue(table[i][j]);
			}
		}
		
		ExcelUtil.saveSheet(fileName, sheet);
	}
	
}






ExcelUtil

package cn.bisoft.java.poi.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelUtil {
	
	public static Sheet createSheet(String fileName, String sheetName)
	{
		Workbook wb = new HSSFWorkbook();
		FileOutputStream fileOut = null;
		Sheet sheet = null;
		
		try {
			fileOut = new FileOutputStream(fileName);
			sheet = wb.createSheet(sheetName);
			wb.write(fileOut);
			fileOut.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return sheet;
	}
	
	public static void saveSheet(String fileName, Sheet sheet)
	{
		FileOutputStream fileOut = null;
		
		try {
			fileOut = new FileOutputStream(fileName);
			sheet.getWorkbook().write(fileOut);
		    fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}








你可能感兴趣的:(java,apache,SVN)